使用theano的X_Train格式
-
22-10-2019 - |
题
我想在已经使用Sklearn后,尝试Keras(Theano后端)进行回归。
为此,我使用了这个不错的教程 http://machinelearningmastery.com/regression-tutorial-keras-deep-learning-inbirary-python/并试图用我自己的培训数据替换培训数据。
import numpy
import pandas
import pickle
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.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
[X, Y] = pickle.load(open("training_data_1_week_imp_lt_15.pkl", "rb"))
X_train, X_test, y_train, y_test = train_test_split( X, Y, test_size=0.5, random_state=42)
scaler = StandardScaler()
scaler.fit(X_train) # Don't cheat - fit only on training data
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
print (X_train.shape)
# define base mode
def baseline_model():
# create model
model = Sequential()
model.add(Dense(8, input_dim=8, 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)
estimator.fit(numpy.array(X_train),y_train)
但是,我收到以下错误:
Exception: Error when checking model target: the list of Numpy arrays that you are
passing to your model is not the size the model expected. Expected to see 1
arrays but instead got the following list of 6252 arrays: ...
x的格式是通常的sklearn格式:print(x_train.shape)=(6252,8)
如何正确格式化X格式。
我尝试的是转置,但这无效。
我也已经搜索了网络,但找不到解决方案/解释。
谢谢!
编辑: 这是一个小样本文件 https://ufile.io/8a428
[X, Y] = pickle.load(open("test.pkl", "rb"))
解决方案
我解决了这个问题(仍然把头撞在墙上):
estimator.fit(numpy.array(X_train),numpy.array(y_train))
这有效。我不确定为什么。错误消息非常误导恕我直言。