KerasRegressor:ValueError:不支持连续

时间:2018-11-16 13:24:46

标签: python machine-learning keras neural-network regression

我正在尝试对我的28个维度的数据应用regression learning method

代码:

import numpy
import pandas as pd
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
from sklearn.metrics import accuracy_score
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# load dataset
dataframe = pd.read_csv("gold_train_small.csv", header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X = dataset[:,1:29]
Y = dataset[:,0]

# load test set

# load dataset
dataframe = pd.read_csv("gold_test.csv", header=None)
dataset = dataframe.values
# split into input (X) and output (Y) variables
X_test = dataset[:,1:29]
Y_test = dataset[:,0]

# define base model
def baseline_model():
    # create model
    model = Sequential()
    model.add(Dense(28, input_dim=28, kernel_initializer='normal', activation='relu'))
    model.add(Dense(1, kernel_initializer='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, epochs=100, batch_size=5, verbose=0)

kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(estimator, X, Y, cv=kfold)
print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std()))

#Baseline: 31.64 (26.82) MSE

# evaluate model with standardized dataset
numpy.random.seed(seed)
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50, batch_size=5, verbose=0)))
pipeline = Pipeline(estimators)
kfold = KFold(n_splits=10, random_state=seed)
results = cross_val_score(pipeline, X, Y, cv=kfold)
print("Standardized: %.2f (%.2f) MSE" % (results.mean(), results.std()))


estimator.fit(X, Y)
prediction = estimator.predict(X_test)
accuracy_score(Y_test, prediction)

但是,我在最后一行收到以下错误:

  

ValueError:不支持连续

我应该使用其他措施吗?

1 个答案:

答案 0 :(得分:1)

这是因为precision_score用于分类模型。

由于这是一个回归模型,因此您应尝试使用:

estimator.score(X_test,Y_test)