【问题标题】:Too many indices for array while using sklearn imputer class使用 sklearn imputer 类时数组索引过多
【发布时间】:2020-01-23 21:03:11
【问题描述】:

我正在练习机器学习中的数据集,在获得缺失值的同时,我使用了 imputer 类,但它给了我一个 too many indices for array 的错误。对于那个错误,我只是查看了所有 numpy 模块,但我没有任何解决它的想法。

import numpy as np
import matplotlib.pyplot as mlp
import pandas as pd

#import datasets
i_export = pd.read_csv("2018-2010_export.csv")
x=i_export.iloc[:, [0,1,3,4]].values
y=i_export.iloc[:,2].values

#splitting training test set
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=0)

#calculating missing data
from sklearn.impute import SimpleImputer
impute=SimpleImputer(missing_values=np.nan,strategy='mean')
impute=SimpleImputer.fit(y_test[:,0])
y_test[:,0]=SimpleImputer.fit_transform(y_test[:,0])

【问题讨论】:

    标签: python pandas machine-learning scikit-learn imputation


    【解决方案1】:

    我认为 y_test 是一个一维数组。当您尝试索引y_test[:,0] 时,您尝试索引两个维度。

    您可以使用此 sn-p y_test = y_test.reshape(-1,1) 将您的 y_test 数组变成一列和“n”行的二维数组

    这是我对您的代码所做的更改。我对您使用 Simple Imputer 的方式进行了一些更改。

    from sklearn.impute import SimpleImputer
    imputer=SimpleImputer(missing_values=np.nan,strategy='mean')
    y_test=imputer.fit_transform(y_test.reshape(-1, 1))
    

    【讨论】:

    • 很高兴为您提供帮助,欢迎来到 Stack Overflow。如果此答案解决了您的问题,请将其标记为已接受。
    【解决方案2】:

    你可以试试 DataFrame.fillna() 的方法。在机器学习中玩得开心。

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 2019-10-17
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      相关资源
      最近更新 更多