michael0196
michael0196

Reputation: 1637

How to apply a trained neural network to write outputs into a csv file?

I have the following dataset,data_num, on which I have trained the neural network, here is sample of 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

Here's the entire code:

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))

Now that the network is trained, I would like to apply the code to a new dataset, predict_num and print the NN output into a new column 'Predictions'. Here's a sample of the new dataset:

  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 also has an extra column 'Stock', so I would like to specify the features only as columns [A, B, C, D] and fill the column 'Predictions' with the output of the NN.

The final dataset should look like this:

  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

Thank you so much for your help.

Upvotes: 2

Views: 1936

Answers (1)

Steven Black
Steven Black

Reputation: 2232

use model.predict

.... 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")

:)

Upvotes: 3

Related Questions