python cross val score,Python Keras cross_val_score错误

Unfortunately I am running into an error I cannot fix.

If i just copy and paste the code I get the following error when running this snippet:

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)

The error says:

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

Thanks for any help.

Here is the full traceback:

Traceback (most recent call last):

File "", line 1, in

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

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'

解决方案

The specific error reported is:

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

The fault was introduced by a bug in Keras version 1.2.1. It occurs when you use the Keras wrapper classes (e.g. KerasClassifier and KerasRegressor) and scikit-learn function cross_val_score().

The bug has been identified and patched in the Keras GitHub project.

There are two fixes that I have tried:

Fix 1: Roll-back to Keras version 1.2.0.

Type:

sudo pip install keras==1.2.0

Fix 2: Monkey-patch Keras with the fix.

After your imports, but before your work type:

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

Both fixes work for me (Python 2 and 3/sklearn 0.18.1).

Some additional candidate fixes:

Wait for the next version of Keras (1.2.2) to be released.

Checkout Keras from Github then build and install manually.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python交叉验证是一种机器学习技术,用于评估模型的性能和泛化能力。它将数据集分成训练集和测试集,然后多次重复此过程,每次使用不同的数据子集进行训练和测试。这样可以减少过拟合和欠拟合的风险,提高模型的准确性和可靠性。Python中有许多库和工具可以实现交叉验证,如scikit-learn和Keras。 ### 回答2: Python中的交叉验证是一种用于评估机器学习模型性能的技术。交叉验证通过将数据集分为训练集和测试集来测试模型的泛化能力。常见的交叉验证方法包括K折交叉验证和留一交叉验证。 K折交叉验证将数据集分为K个相等的子集,其中K-1个子集作为训练集,剩下一个子集作为测试集。然后,重复K次交叉验证实验,每一次都选择不同的测试集。最后,对每次实验的测试结果进行平均,作为模型的最终性能评估指标。 留一交叉验证是一种特殊的K折交叉验证,当K等于数据集大小时,就是留一交叉验证。对于每一次实验,只有一个样本被作为测试集,其余样本作为训练集。由于每个样本都会被作为测试集一次,留一交叉验证提供了对模型性能更准确的评估。 在Python中,使用机器学习库scikit-learn中的cross_val_score函数可以方便地实现交叉验证。该函数接受一个模型对象、数据集和评估指标作为参数,并返回每一次交叉验证的测试结果。通过对这些结果进行统计分析,可以得到模型的性能评估指标。 交叉验证在机器学习中是一种常用的技术,有助于防止模型过拟合并提高模型的稳定性和泛化能力。通过交叉验证,我们可以更准确地评估模型的性能,并选择最优的模型参数。 ### 回答3: Python的交叉验证(cross validation)是一种模型评估的方法,其目的是在有限的数据集上评估和调整机器学习模型的参数,从而提高模型的泛化能力。 交叉验证通过将原始数据集分为训练集和验证集,并多次重复此过程,来评估和调整模型。常用的交叉验证方法有k折交叉验证(k-fold cross validation)和留一交叉验证(leave-one-out cross validation)。 在k折交叉验证中,数据集被平均分成k份,其中k-1份用作训练集,剩下的1份用作验证集。然后重复k次,每次选取不同的验证集。最后将每次验证集的评估结果取平均得到最终评估结果。 而在留一交叉验证中,每个样本都被当作一个验证集,其他样本作为训练集。这种方法在数据集较小的情况下常用,因为它能够充分利用所有样本,但计算开销较大。 交叉验证的优点是能够更准确地评估模型的性能,因为它使用了更多的数据进行验证,并且减少了由于数据分割导致的偶然性。同时,它还能够帮助选择最佳的模型参数,从而提高模型的泛化能力。 在Python中,有多种库可以进行交叉验证,例如scikit-learn库中的cross_val_score函数可以方便地进行k折交叉验证。此外,还可以使用其他的机器学习库,如TensorFlow和PyTorch,它们也提供了类似的交叉验证功能。 总之,Python的交叉验证是一种用于评估和调整机器学习模型的方法,可以提高模型的泛化能力,并且通过使用现有的机器学习库,可以很方便地进行交叉验证。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值