KerasRegressor:ValueError:不支持连续[重复]
Posted
技术标签:
【中文标题】KerasRegressor:ValueError:不支持连续[重复]【英文标题】:KerasRegressor: ValueError: continuous is not supported [duplicate] 【发布时间】:2019-04-19 16:43:57 【问题描述】:我正在尝试将regression learning method 应用于我的具有 28 个维度的数据。
代码:
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】:这是因为 accuracy_score 用于分类模型。
由于这是一个回归模型,您应该尝试使用:
estimator.score(X_test,Y_test)
【讨论】:
以上是关于KerasRegressor:ValueError:不支持连续[重复]的主要内容,如果未能解决你的问题,请参考以下文章
由于不可克隆性,将 KerasRegressor 与 cross_validate 一起使用失败
如何解决 raise ValueError("columns must have matching element counts") ValueError: columns mus