我有以下数据集data_num
,我已在该数据集上训练了神经网络,这是data_num
的示例:
A B C D Label1
0 95 91 3 10 9
1 91 95 4 3 9
2 68 65 31 31 6
3 50 43 51 58 4
4 8 4 93 97 0
5 16 20 81 90 1
6 75 79 28 26 7
7 74 76 27 22 7
8 45 46 56 57 4
9 5 7 97 93 0
这是完整的代码:
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
import pandas as pd
import numpy as np
import matplotlib
from matplotlib import style
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from IPython.core.display import display
from sklearn.preprocessing import MinMaxScaler
matplotlib.style.use('ggplot')
data_num = pd.read_csv('mult_test.csv')
scaler = MinMaxScaler(feature_range=(0, 1))
features = data_num.drop(['Label1'], axis=1, errors='ignore')
features = pd.DataFrame(scaler.fit_transform(features))
scale_num_data = pd.concat([data_num['Label1'], features], axis=1)
dtrain, dtest = train_test_split(scale_num_data, test_size=0.25, random_state=570)
X = dtrain.drop(['Label1'], axis=1, errors='ignore')
y = dtrain['Label1']
Xtest = dtest.drop(['Label1'], axis=1, errors='ignore')
ytest = dtest['Label1']
model = Sequential([
Dense(10, input_shape=(4, ), activation='relu'),
Dense(32, activation='relu'),
Dense(10, activation='softmax')
])
model.summary()
model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=10, batch_size=10, shuffle=True)
scores = model.evaluate(Xtest, ytest, batch_size=5)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
现在网络已经训练完毕,我想将代码应用到新数据集 predict_num
并将 NN 输出打印到新列“预测”中。以下是新数据集的示例:
Stock A B C D Prediction
0 AMCX 46 43 57 52
1 ABAC 83 86 11 18
2 AKAM 55 52 45 43
3 ACW 96 99 9 8
4 AOLS 46 43 54 52
5 ABAX 9 9 100 95
6 AMTX 9 1 91 97
7 ABMC 73 79 29 25
8 ALE 58 56 50 44
9 AMAT 8 1 98 92
predict_num
还有一个额外的列“Stock”,因此我只想将特征指定为列 [A、B、C、D],并用输出填充“预测”列神经网络的。
最终的数据集应如下所示:
Stock A B C D Prediction
0 AMCX 46 43 57 52 4
1 ABAC 83 86 11 18 8
2 AKAM 55 52 45 43 5
3 ACW 96 99 9 8 9
4 AOLS 46 43 54 52 4
5 ABAX 9 9 100 95 0
6 AMTX 9 1 91 97 0
7 ABMC 73 79 29 25 7
8 ALE 58 56 50 44 5
9 AMAT 8 1 98 92 0
非常感谢您的帮助。
最佳答案
.... your code ....
model.summary()
model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(X, y, epochs=10, batch_size=10, shuffle=True)
pred = model.predict(xtest)
xtest["prediciton"] = pred
xtest.to_csv("my_new_file.csv")
:)
关于python - 如何应用经过训练的神经网络将输出写入 csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49564403/