Python Keras cross_val_score 错误

标签 python scikit-learn regression keras

我正在尝试在 keras 上做这个关于回归的小教程: http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/

不幸的是,我遇到了无法修复的错误。 如果我只是复制并粘贴代码,则在运行此代码段时会出现以下错误:

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
# load dataset
dataframe = pandas.read_csv("housing.csv", delim_whitespace=True,header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,0:13]
Y = dataset[:,13]
# define base mode
def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(13, input_dim=13, init='normal', activation='relu'))
    model.add(Dense(1, init='normal'))
    # Compile model
    model.compile(loss='mean_squared_error', optimizer='adam')
    return model
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# evaluate model with standardized dataset
estimator = KerasRegressor(build_fn=baseline_model, nb_epoch=100,batch_size=5, verbose=0)

kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)

错误说:

TypeError: get_params() got an unexpected keyword argument 'deep'

感谢您的帮助。

这是完整的回溯:

Traceback (most recent call last):


File "<stdin>", line 1, in <module>
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 140, in cross_val_score
    for train, test in cv_iter)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 758, in __call__
    while self.dispatch_one_batch(iterator):
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 603, in dispatch_one_batch
    tasks = BatchedCalls(itertools.islice(iterator, batch_size))
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__
    self.items = list(iterator_slice)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 140, in <genexpr>
    for train, test in cv_iter)
  File "C:\Users\myname\Anaconda3\lib\site-packages\sklearn\base.py", line 67, in clone
    new_object_params = estimator.get_params(deep=False)
TypeError: get_params() got an unexpected keyword argument 'deep'

最佳答案

具体报错是:

TypeError: get_params() got an unexpected keyword argument 'deep'

该故障是由 Keras 1.2.1 版本的 bug 引入的。当您使用 Keras 包装器类(例如 KerasClassifier 和 KerasRegressor)和 scikit-learn 函数 cross_val_score() 时会发生这种情况。

错误已identifiedpatched在 Keras GitHub 项目中。

我尝试了两个修复方法:

修复 1:回滚到 Keras 版本 1.2.0。

类型:

sudo pip install keras==1.2.0

修复 2:使用修复的 Monkey-patch Keras。

导入之后,工作类型之前:

from keras.wrappers.scikit_learn import BaseWrapper
import copy

def custom_get_params(self, **params):
    res = copy.deepcopy(self.sk_params)
    res.update({'build_fn': self.build_fn})
    return res

BaseWrapper.get_params = custom_get_params

这两个修复对我都有效(Python 2 和 3/sklearn 0.18.1)。

一些额外的候选修复:

  • 等待下一个版本的 Keras (1.2.2) 发布。
  • 从 Github 检查 Keras,然后手动构建和安装。

关于Python Keras cross_val_score 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41796618/

相关文章:

python - Scikit学习中的线性回归和梯度下降?

r - 如何使用 lapply 函数将回归输出列表转换为 broom::tidy 的数据框?

machine-learning - 总误差平均值是回归模型的足够性能指标吗?

python - 无法通过在 django admin 中替换 django 的默认日期时间小部件来使自定义小部件正常工作

python - 如何设置 Tkinter 小部件的大小(以像素为单位)?

python - 在 matplotlib python 中以 3D 形式动画绘制两种不同的颜色和形状

machine-learning - 带有 Scikit-Learn 的传导 SVM

python - 使用 pandas 查找另一个 df 中一行的所有单元格,如果全部存在则返回标志

python - 使用 2 个分类器进行集成学习

r - 如何在 R 的 lm() 中正确选择因变量和控件?