简体   繁体   English

Python sklearn:缩放期间出现Numpy转换错误

[英]Python sklearn: Numpy Conversion Error during scaling

I am trying to perform a PCA Analysis on Data in a CSV File but I keep getting a weird warning when I attempt to scale the data. 我正在尝试对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)

This is the warning I keep getting. 这是我不断得到的警告。

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

All the values in the CSV file are numbers. CSV文件中的所有值都是数字。 This is what the head looks like 这是头部的样子

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

Can somebody please tell me why I am getting this warning and how I can fix it? 有人可以告诉我为什么收到此警告以及如何解决此警告吗?

I figured it out. 我想到了。 I had to convert my Dataframe into a numpy Matrix and then define the type as float. 我必须将我的数据框转换为numpy矩阵,然后将类型定义为float。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM