Python sklearn:缩放期间的Numpy Conversion Error

时间:2015-08-05 18:12:22

标签: python csv numpy statistics scikit-learn

我正在尝试对CSV文件中的数据执行PCA分析,但在尝试扩展数据时,我不断收到一个奇怪的警告。

def prepare_data(filename):
    df=pd.read_csv(filename,index_col=0)
    df.dropna(axis=0,how='any',inplace=True)
    return df

def perform_PCA(df):
    threshold = 0.3
    component = 1 #Second of two right now
    pca = decomposition.PCA(n_components=2)
    print df.head()
    scaled_data = preprocessing.scale(df)
    #pca.fit(scaled_data)
    #transformed = pca.transform(scaled_data)
    #pca_components_df = pd.DataFrame(data = pca.components_,columns = df.columns.values)

这是我不断发出的警告。

C:\Users\mbellissimo\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\utils\validation.py:498: UserWarning: The scale function assumes floating point values as input, got int64
  "got %s" % (estimator, X.dtype))
C:\Users\mbellissimo\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\preprocessing\data.py:145: DeprecationWarning: Implicitly casting between incompatible kinds. In a future numpy release, this will raise an error. Use casting="unsafe" if this is intentional.
  Xr -= mean_
C:\Users\mbellissimo\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\preprocessing\data.py:153: UserWarning: Numerical issues were encountered when centering the data and might not be solved. Dataset may contain too large values. You may need to prescale your features.
  warnings.warn("Numerical issues were encountered "
C:\Users\mbellissimo\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\preprocessing\data.py:158: DeprecationWarning: Implicitly casting between incompatible kinds. In a future numpy release, this will raise an error. Use casting="unsafe" if this is intentional.
  Xr -= mean_1
C:\Users\mbellissimo\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\preprocessing\data.py:160: DeprecationWarning: Implicitly casting between incompatible kinds. In a future numpy release, this will raise an error. Use casting="unsafe" if this is intentional.
  Xr /= std_
C:\Users\mbellissimo\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\preprocessing\data.py:169: UserWarning: Numerical issues were encountered when scaling the data and might not be solved. The standard deviation of the data is probably very close to 0.
  warnings.warn("Numerical issues were encountered "
C:\Users\mbellissimo\AppData\Local\Continuum\Anaconda\lib\site-packages\sklearn\preprocessing\data.py:174: DeprecationWarning: Implicitly casting between incompatible kinds. In a future numpy release, this will raise an error. Use casting="unsafe" if this is intentional.
  Xr -= mean_2

CSV文件中的所有值都是数字。这就是的样子

TOOLS/TEST EQUIPMENT  WIN PRODUCTIVITY/UTILITY  \
HouseholdID
144748819                       0                         0
144764123                       0                         0
144765100                       0                         0
144765495                       2                         0
144765756                       0                         2

有人可以告诉我为什么我会收到此警告以及如何解决此问题?

1 个答案:

答案 0 :(得分:6)

我明白了。我不得不将我的Dataframe转换为numpy Matrix,然后将类型定义为float。

numpyMatrix = df.as_matrix().astype(float)
scaled_data = preprocessing.scale(numpyMatrix)