pandas - AttributeError:“numpy.ndarray”对象没有属性“iloc”
问题描述
我正在尝试结合使用堆叠的两种机器学习算法来获得更好的结果,但在某些方面失败了。这是我的代码:
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)+' %')
错误出现在 Stacking 方法中的“iloc”中。
我一直收到 np.ndarray has no attribute 'iloc' 的错误。我试图搜索但找不到任何特定链接,尽管我认为这与属于 np.ndarray 的 iloc 有关。如果有人可以帮我解决这个问题!!
解决方案
As the comments suggest, .iloc
is a Pandas dataframe method.
To filter a numpy array you just need: 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]
推荐阅读
- python - 如何使用 aiohttp 将 404 重定向到另一个模板?
- node.js - NodeJS/Mongo 在集合查找中检索关系数据
- android - 如何不断获取http数据?
- php - 在 xampp Windows 上安装 imagick 库扩展?
- npm - 服务器运行时如何安装 npm 包?
- reactjs - ReactJS 中的 WebSocket 使用空数组设置状态
- python - 如何让 pygame.display.update() 方法一直显示窗口?
- sql-server - SQL Server 中的数据库同步
- azure - 如何在powershell / azure cli中动态循环键值对
- powershell - 按共同点分组