无法获得训练和测试集

时间:2019-01-08 17:32:26

标签: cross-validation

我应用k倍交叉验证将数据分为训练集和测试集。 但是,当我想要训练和测试集时,会出现以下错误:

AttributeError:“ numpy.ndarray”对象没有属性“ iloc”

感谢您的帮助。

y = df_dummies['Churn'].values
X = df_dummies.drop(columns = ['Churn'])

 from sklearn.preprocessing import MinMaxScaler
features = X.columns.values
scaler = MinMaxScaler(feature_range = (0,1))
scaler.fit(X)
X = pd.DataFrame(scaler.transform(X))
X.columns = features 

from sklearn.model_selection import KFold

kf=KFold(n_splits=5,shuffle=True)

for train,test in kf.split(X):
print("%s %s" % (train,test))


for train_index, test_index in kf.split(X):
     print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X.iloc[train_index], X.iloc[test_index]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]   
from sklearn.linear_model import LogisticRegression
CLF = LogisticRegression().fit(X_train, y_train)
print('Accuracy of Logistic regression classifier on training set:          {:.2f}'
 .format(CLF.score(X_train, y_train)))
print('Accuracy of Logistic regression classifier on test set: {:.2f}'
 .format(CLF.score(X_test, y_test)))  
NameError: name 'y_train' is not defined

1 个答案:

答案 0 :(得分:0)

问题是df_dummies['Churn'].values返回一个数组而不是数据帧。但是,您正在尝试从不存在的数组中获取属性。 iloc函数位于pandas.DataFrame中。

改为使用y = df_dummies['Churn']

参考:https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.iloc.html#pandas.DataFrame.iloc

PS:我不知道如何将这些类型的问题迁移到姊妹站点。也许,有人知道可以将其迁移到交叉验证。

相关问题