Skip to main content
 首页 » 编程设计

python sklearn : Numpy Conversion Error during scaling

2025年05月04日14傻小

我正在尝试对 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 

有人可以告诉我为什么会收到此警告以及如何修复它吗?

请您参考如下方法:

我想通了。我必须将我的 Dataframe 转换成一个 numpy 矩阵,然后将类型定义为 float。

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