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.

你可能感兴趣的:(python,cross,val,score)