我有以下数据集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]列,并用NN的输出填充'Predictions'列
最终数据集应如下所示:
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
非常感谢你的帮助。
答案 0 :(得分:2)
.... 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")
:)