Sklearn 0.20+ 的交叉验证?
Cross-validation for Sklearn 0.20+?
我正在尝试进行交叉验证,但我 运行 遇到了一条错误消息:'Found input variables with inconsistent numbers of samples: [18, 1]'
我在 pandas 数据框 (df) 中使用不同的列作为特征,最后一列作为标签。这源自 UC Irvine 的机器学习存储库。在导入我过去使用的交叉验证包时,我收到一个错误,它可能已经贬值了。我要运行决策树,SVM,K-NN。
我的代码是这样的:
feature = [df['age'], df['job'], df['marital'], df['education'], df['default'], df['housing'], df['loan'], df['contact'],
df['month'], df['day_of_week'], df['campaign'], df['pdays'], df['previous'], df['emp.var.rate'], df['cons.price.idx'],
df['cons.conf.idx'], df['euribor3m'], df['nr.employed']]
label = [df['y']]
from sklearn.cross_validation import train_test_split
from sklearn.model_selection import cross_val_score
# Model Training
x = feature[:]
y = label
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
任何帮助都会很棒!
您的 feature
列表中的项目是 pandas 系列。您不需要像以前那样在列表中列出每个功能;您只需要将它们全部作为单个 "table".
传递
例如,这看起来像银行数据集:
df = pd.read_csv('bank.csv', sep=';')
#df.shape
#(4521, 17)
#df.columns
#Index(['age', 'job', 'marital', 'education', 'default', 'balance', 'housing',
# 'loan', 'contact', 'day', 'month', 'duration', 'campaign', 'pdays',
# 'previous', 'poutcome', 'y'],
# dtype='object')
x = df.iloc[:, :-1]
y = df.iloc[:, -1]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
应该可以。这里唯一要注意的是 x
是一个有 16 列的 DataFrame 但它的基础数据是一个 numpy ndarray - 不是系列列表而是单个 "matrix".
cross_validation
模块已弃用。新模块 model_selection
已取而代之。所以你用 cross_validation
所做的一切。 model_selection
现已推出。那么你上面的代码就变成了:
feature = [df['age'], df['job'], df['marital'], df['education'], df['default'], df['housing'], df['loan'], df['contact'],
df['month'], df['day_of_week'], df['campaign'], df['pdays'], df['previous'], df['emp.var.rate'], df['cons.price.idx'],
df['cons.conf.idx'], df['euribor3m'], df['nr.employed']]
label = [df['y']]
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
现在就声明 X 和 y 而言,为什么要将它们包装在列表中。像这样使用它们:
feature = df[['age', 'job', 'marital', 'education', 'default', 'housing',
'loan', 'contact', 'month', 'day_of_week', 'campaign',
'pdays', 'previous', 'emp.var.rate', 'cons.price.idx',
'cons.conf.idx', 'euribor3m', 'nr.employed']]
label = df['y']
然后您可以简单地使用您的代码,无需更改任何内容。
# Model Training
x = feature[:]
y = label
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
关于交叉验证中折叠的最后一个问题,sklearn 中有多个 类 可以执行此操作(取决于任务)。请看一看:
其中包含折叠迭代器。请记住,所有这些都存在于 model_selection
包中。
我正在尝试进行交叉验证,但我 运行 遇到了一条错误消息:'Found input variables with inconsistent numbers of samples: [18, 1]'
我在 pandas 数据框 (df) 中使用不同的列作为特征,最后一列作为标签。这源自 UC Irvine 的机器学习存储库。在导入我过去使用的交叉验证包时,我收到一个错误,它可能已经贬值了。我要运行决策树,SVM,K-NN。
我的代码是这样的:
feature = [df['age'], df['job'], df['marital'], df['education'], df['default'], df['housing'], df['loan'], df['contact'],
df['month'], df['day_of_week'], df['campaign'], df['pdays'], df['previous'], df['emp.var.rate'], df['cons.price.idx'],
df['cons.conf.idx'], df['euribor3m'], df['nr.employed']]
label = [df['y']]
from sklearn.cross_validation import train_test_split
from sklearn.model_selection import cross_val_score
# Model Training
x = feature[:]
y = label
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
任何帮助都会很棒!
您的 feature
列表中的项目是 pandas 系列。您不需要像以前那样在列表中列出每个功能;您只需要将它们全部作为单个 "table".
例如,这看起来像银行数据集:
df = pd.read_csv('bank.csv', sep=';')
#df.shape
#(4521, 17)
#df.columns
#Index(['age', 'job', 'marital', 'education', 'default', 'balance', 'housing',
# 'loan', 'contact', 'day', 'month', 'duration', 'campaign', 'pdays',
# 'previous', 'poutcome', 'y'],
# dtype='object')
x = df.iloc[:, :-1]
y = df.iloc[:, -1]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
应该可以。这里唯一要注意的是 x
是一个有 16 列的 DataFrame 但它的基础数据是一个 numpy ndarray - 不是系列列表而是单个 "matrix".
cross_validation
模块已弃用。新模块 model_selection
已取而代之。所以你用 cross_validation
所做的一切。 model_selection
现已推出。那么你上面的代码就变成了:
feature = [df['age'], df['job'], df['marital'], df['education'], df['default'], df['housing'], df['loan'], df['contact'],
df['month'], df['day_of_week'], df['campaign'], df['pdays'], df['previous'], df['emp.var.rate'], df['cons.price.idx'],
df['cons.conf.idx'], df['euribor3m'], df['nr.employed']]
label = [df['y']]
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
现在就声明 X 和 y 而言,为什么要将它们包装在列表中。像这样使用它们:
feature = df[['age', 'job', 'marital', 'education', 'default', 'housing',
'loan', 'contact', 'month', 'day_of_week', 'campaign',
'pdays', 'previous', 'emp.var.rate', 'cons.price.idx',
'cons.conf.idx', 'euribor3m', 'nr.employed']]
label = df['y']
然后您可以简单地使用您的代码,无需更改任何内容。
# Model Training
x = feature[:]
y = label
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
关于交叉验证中折叠的最后一个问题,sklearn 中有多个 类 可以执行此操作(取决于任务)。请看一看:
其中包含折叠迭代器。请记住,所有这些都存在于 model_selection
包中。