从最简单的波士顿房价数据分析入手,在网上找了一段代码,根据我自己的理解做了以下分析,有不对之处还请各位大神指出。
导入KERAS和波士顿房价数据包
import keras
from keras.datasets import boston_housing
导入层级模型和全连接层
from keras.models import Sequential
from keras.layers import Dense
从房价数据包中导入数据
(x_train,y_train),(x_test,y_test) = boston_housing.load_data()
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.fit_transform(x_test)
print(x_train[0])
print(x_test[0])
n_hidden_1 = 64
n_hidden_2 = 64
n_input = 13
n_classes = 1
training_epochs = 200
batch_size = 10
input_dim=n_input,第一层必须写明输入数据维度,这里是13;
n_hidden_1指输出维度,这里是64(可以自己设置任意值),代表第一层的神经元个数;
n_hidden_2指第二层的输出,也代表第二层的神经元个数;¶
第三层为一个维度的输出n_classes
from keras import models
model = models.Sequential()
model.add(Dense(n_hidden_1,activation='relu',input_dim=n_input))
model.add(Dense(n_hidden_2,activation='relu'))
model.add(Dense(n_classes))
import keras.backend as K
def r2(y_true,y_pred):
a = K.square(y_pred - y_true)
b = K.sum(a)
c = K.mean(y_true)
d = K.square(y_true - c)
e = K.sum(d)
f = 1 - b/e
return f
model.compile(loss='mse',optimizer='rmsprop',metrics=['mae',r2])
history = model.fit(x_train,y_train,batch_size=batch_size,epochs=training_epochs)
pred_y_test = model.predict(x_test)
print(pred_y_test)
没搞明白这里r2的参数是怎么传进去的?
from sklearn.metrics import r2_score
pred_acc = r2_score(y_test,pred_y_test)
print('pred_acc',pred_acc)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(16,8), dpi=160)
plt.plot(range(len(y_test)), y_test, ls='-.',lw=2,c='r',label='真实值')
plt.plot(range(len(pred_y_test)), pred_y_test, ls='-',lw=2,c='b',label='预测值')
plt.grid(alpha=0.4, linestyle=':')
plt.legend()
plt.xlabel('number')
plt.ylabel('房价')
plt.show()