pandas AttributeError: 'numpy.ndarray' 对象没有属性 'iloc'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53183433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:08:15  来源:igfitidea点击:

AttributeError: 'numpy.ndarray' object has no attribute 'iloc'

pandasnumpy

提问by Abhishek Thakur

I am trying to combine two machine learning algorithm using stacking to achieve greater results but am failing in some of the aspects. Here's my code:

我正在尝试使用堆叠将两种机器学习算法结合起来以获得更好的结果,但在某些方面失败了。这是我的代码:

class Ensemble(threading.Thread): "Stacking with three Classification Models to improve the accuracy of Predictions" def init(self, X, Y, XT, YT, accLabel=None): threading.Thread.init(self) self.X = X self.Y = Y self.XT=XT self.YT=YT self.accLabel= accLabel

class Ensemble(threading.Thread):“与三个分类模型堆叠以提高预测的准确性” def init(self, X, Y, XT, YT, accLabel=None): threading.Thread。init(self) self.X = X self.Y = Y self.XT=XT self.YT=YT self.accLabel= accLabel

def Stacking(self,model,n_fold,train,test,y):

    folds=StratifiedKFold(n_splits=n_fold,random_state=1)
    test_pred=np.empty((test.shape[0],1),float)
    train_pred=np.empty((0,1),float)
    for train_indices,val_indices in folds.split(train,y):
        x_train,x_val=train.iloc[train_indices],train.iloc[val_indices]
        y_train,y_val=y.iloc[train_indices],y.iloc[val_indices]

        model.fit(X=x_train,y=y_train)
        train_pred=np.append(train_pred,model.predict(x_val))
        test_pred=np.append(test_pred,model.predict(test))
    return test_pred.reshape(-1,1),train_pred   

def run(self):
    X = np.zeros(self.X.shape)
    Y = np.zeros(self.Y.shape)
    XT = np.zeros(self.XT.shape)
    YT = np.zeros(self.YT.shape)
    np.copyto(X, self.X)
    np.copyto(Y, self.Y)
    np.copyto(XT, self.XT)
    np.copyto(YT, self.YT)

    model1 = tree.DecisionTreeClassifier(random_state=1)
    n_fold=4
    test_pred1 ,train_pred1=self.Stacking(model1, n_fold, X, XT, Y)
    train_pred1=pd.DataFrame(train_pred1)
    test_pred1=pd.DataFrame(test_pred1)

    model2 = KNeighborsClassifier()
    test_pred2 ,train_pred2=self.Stacking(model2, n_fold, X, XT, Y)
    train_pred2=pd.DataFrame(train_pred2)
    test_pred2=pd.DataFrame(test_pred2)

    df = pd.concat([train_pred1, train_pred2], axis=1)
    df_test = pd.concat([test_pred1, test_pred2], axis=1)
    model = LogisticRegression(random_state=1)
    model.fit(df,Y)
    sd = model.score(df_test, YT)
    acc = (sum(sd == YT) / len(YT) * 100)
    print("Accuracy of Ensemble Learning Model is : %.2f" % acc+' %')
    print('=' * 100)
    if self.accLabel: self.accLabel.set("Accuracy of Ensembelance Learning: %.2f" % (acc)+' %')

The error is in 'iloc' inside Stacking method.

错误出在 Stacking 方法中的“iloc”中。

I have been constantly getting the error of np.ndarray has no attribute 'iloc'. I tried to search but couldn't find any specific link though I think this has something to do with iloc belonging to np.ndarray. If someone could please help me with this!!

我一直在不断收到 np.ndarray 没有属性“iloc”的错误。我试图搜索但找不到任何特定链接,尽管我认为这与属于 np.ndarray 的 iloc 有关。如果有人可以帮我解决这个问题!!

回答by Franco Piccolo

As the comments suggest, .ilocis a Pandas dataframe method.

正如评论所暗示的那样,.iloc是一种 Pandas 数据框方法。

To filter a numpy array you just need: array[indices]

要过滤 numpy 数组,您只需要: array[indices]

In your case:

在你的情况下:

x_train,x_val=train[train_indices],train[val_indices]
y_train,y_val=y[train_indices],y[val_indices]