【问题标题】:Can't get train and test sets无法获得训练集和测试集
【发布时间】:2019-01-08 17:32:25
【问题描述】:

我应用了 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

【问题讨论】:

  • 嗨 Sergio0606,欢迎来到 stackoverflow.com,这个问题似乎不属于这里。你见过交叉验证的姊妹网站吗? stats.stackexchange.com
  • 我是堆栈溢出的新手。我没看见过它。太棒了

标签: cross-validation


【解决方案1】:

问题是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:我不知道如何将这些类型的问题迁移到姊妹网站。也许,知道的人可以将此迁移到交叉验证。

【讨论】:

    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 2017-11-01
    • 2015-01-17
    • 2013-06-18
    • 2019-08-15
    • 2012-12-04
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多