首先,您必须使用来自sklearn.model_selection 库的train_test_split 类将您的数据集 拆分为training 集和test 集。
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.08, random_state = 0)
此外,您必须使用 StandardScaler 类来 scale 您的值。
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
然后,您应该添加更多层以获得更好的结果。
注意
通常最好应用以下公式来找出所需的隐藏层 层的总数。
Nh = Ns/(α∗ (Ni + No))
在哪里
- Ni = 输入神经元的数量。
- 否 = 输出神经元的数量。
- Ns = 训练数据集中的样本数。
- α = 任意比例因子,通常为 2-10。
所以我们的分类器变成:
# Initialising the ANN
model = Sequential()
# Adding the input layer and the first hidden layer
model.add(Dense(32, activation = 'relu', input_dim = 6))
# Adding the second hidden layer
model.add(Dense(units = 32, activation = 'relu'))
# Adding the third hidden layer
model.add(Dense(units = 32, activation = 'relu'))
# Adding the output layer
model.add(Dense(units = 1))
您使用的metric-metrics=['accuracy'] 对应于分类问题。如果您想做回归,请删除metrics=['accuracy']。也就是说,只需使用
model.compile(optimizer = 'adam',loss = 'mean_squared_error')
Here 是regression 和classification 的keras 指标列表
此外,您必须为 fit 方法定义 batch_size 和 epochs 值。
model.fit(X_train, y_train, batch_size = 10, epochs = 100)
训练完network 后,您可以使用model.predict 方法predict 得到X_test 的结果。
y_pred = model.predict(X_test)
现在,您可以比较我们从神经网络预测中获得的y_pred 和y_test 这是真实数据。为此,您可以使用matplotlib 库创建plot。
plt.plot(y_test, color = 'red', label = 'Real data')
plt.plot(y_pred, color = 'blue', label = 'Predicted data')
plt.title('Prediction')
plt.legend()
plt.show()
看来我们的神经网络学得很好
这是plot 的外观。
这是完整的代码
import numpy as np
from keras.layers import Dense, Activation
from keras.models import Sequential
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# Importing the dataset
dataset = np.genfromtxt("data.txt", delimiter='')
X = dataset[:, :-1]
y = dataset[:, -1]
# Splitting the dataset into the Training set and Test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.08, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# Initialising the ANN
model = Sequential()
# Adding the input layer and the first hidden layer
model.add(Dense(32, activation = 'relu', input_dim = 6))
# Adding the second hidden layer
model.add(Dense(units = 32, activation = 'relu'))
# Adding the third hidden layer
model.add(Dense(units = 32, activation = 'relu'))
# Adding the output layer
model.add(Dense(units = 1))
#model.add(Dense(1))
# Compiling the ANN
model.compile(optimizer = 'adam', loss = 'mean_squared_error')
# Fitting the ANN to the Training set
model.fit(X_train, y_train, batch_size = 10, epochs = 100)
y_pred = model.predict(X_test)
plt.plot(y_test, color = 'red', label = 'Real data')
plt.plot(y_pred, color = 'blue', label = 'Predicted data')
plt.title('Prediction')
plt.legend()
plt.show()