keras训练好的神经网络预测模型,用测试集测试时,model.predic()结果全为nan
把训练集的数据带入训练却没问题,训练集和测试集的结构一样
代码如下:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
input_file='new_data.xlsx'
outputfile='temprature.xls'
modelfile='net.model'
raw_data=pd.read_excel('raw_data.xlsx')
included_angel=raw_data['航向Os(°)']-raw_data['风向Ow(°)']
raw_data.insert(2,'included_angel',included_angel)
new_data=raw_data.drop(['航向Os(°)','风向Ow(°)'],axis=1)
new_data.to_excel('new_data.xls')
feature=['环境温度T1(°C)', '航速Vs(m/s)', 'included_angel', '风速Vw(m/s)', 'X轴波动(°)',
'Y轴波动(°)', '总航行时间(min)', '当前航速航行时间(min)']
from sklearn.utils import shuffle
new_data = shuffle(new_data)
data_train=new_data.loc[range(0,round(new_data.shape[0]*0.8))].copy()#取前80%数据建模
data_test=new_data.loc[range(round(new_data.shape[0]*0.8),new_data.shape[0])].copy()
train_data_mean=data_train.mean()
train_data_std=data_train.std()
data_train=(data_train-train_data_mean)/train_data_std
test_data_mean=data_test.mean()
test_data_std=data_test.std()
data_test=(data_test-test_data_mean)/test_data_std
x_train=data_train[feature].values
y_train=data_train['电机温度T1(°C)'].values
x_test=data_test[feature].values
y_test=data_test['电机温度T1(°C)'].values
model=Sequential()
model.add(Dense(input_dim=8,units=16))
model.add(Activation('relu'))
model.add(Dense(input_dim=16,units=1))
model.compile(loss='mean_squared_error',optimizer='adam')
model.fit(x_train,y_train,nb_epoch=100,batch_size=16)
y_pred=model.predict(x_train)
print(y_pred)