我的数据包含1991-2015年的月平均气温。它具有列Year和Tas。我使用SVM和DT模型进行了处理,它显示了实际数据和预测数据,但是对于ANN,它却不能这样做。有人可以向我解释原因吗?
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
numpy.random.seed(7)
import pandas as pd
# load pima indians dataset
dataset = pd.read_csv(r"C:\Users\admin\Desktop\Machine Learning\Temperature.csv")
# split into input (X) and output (Y) variables
x = dataset.iloc[:,0:1].values
y = dataset.iloc[:,1].values
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size = 0.20,random_state=0)
# create model
model = Sequential()
model.add(Dense(12, input_dim=1, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(x, y, epochs=150, batch_size=10)
pred = model.predict(x_test)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)
import matplotlib.pyplot as plt
plt.plot(x_test)
plt.plot(pred, color='red')
plt.show()
from sklearn import metrics
import numpy as np
print('Mean Absolute Error for temp:',metrics.mean_absolute_error(y_test,pred))
print('Mean Squared Error for temp:',metrics.mean_squared_error(y_test,pred))
print('Root Mean Squared Error for temp:',np.sqrt(metrics.mean_squared_error(y_test,pred)))
# evaluate the model
scores = model.evaluate(x,y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))`
如果我输入以下代码,应该这样做:
predsvmtemp = regressor.predict(x_test)
#compare the actual data and predicted data values
resulttemp=pd.DataFrame({'Actual data':y_test,'Predicted data':resulttemp})
它应该显示结果,但仅不适用于ANN。