How to save Tensorflow predictions to data frame?
$begingroup$
I am new to Tensorflow. I have trained Tensorflow model, but I need to take model predictions and add them to my original test set as a column. How can I do that?
def model(self, layers_dims, X_train, Y_train, X_test, Y_test, learning_rate=0.00001,
num_epochs=1000, print_cost=True):
"""
Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
Arguments:
X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
X_test -- training set, of shape (input size = 12288, number of training examples = 120)
Y_test -- test set, of shape (output size = 6, number of test examples = 120)
learning_rate -- learning rate of the optimization
num_epochs -- number of epochs of the optimization loop
minibatch_size -- size of a minibatch
print_cost -- True to print the cost every 100 epochs
Returns:
parameters -- parameters learnt by the model. They can then be used to predict.
"""
ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables
tf.set_random_seed(1) # to keep consistent results
seed = 3 # to keep consistent results
(n_x, m) = X_train.shape # (n_x: input size, m : number of examples in the train set)
n_y = Y_train.shape[0] # n_y : output size
#print('Ytrain shape', Y_train.shape)
costs = # To keep track of the cost
# Create Placeholders of shape (n_x, n_y)
X, Y = self.create_placeholders(n_x, n_y)
# Initialize parameters
parameters = NN_predict_trading_decisions.initialize_parameters(layers_dims)
# Forward propagation: Build the forward propagation in the tensorflow graph
ZL = NN_predict_trading_decisions.forward_propagation(X, parameters)
# Cost function: Add cost function to tensorflow graph
cost = NN_predict_trading_decisions.compute_cost(ZL, Y)
# Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
#optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initialize all the variables
init = tf.global_variables_initializer()
# Start the session to compute the tensorflow graph
with tf.Session() as sess:
# Run the initialization
sess.run(init)
# Do the training loop
for epoch in range(num_epochs):
epoch_cost = 0. # Defines a cost related to an epoch
current_cost = sess.run([optimizer, cost], feed_dict= {X: X_train, Y: Y_train})
epoch_cost += current_cost[1]
# Print the cost every epoch
if print_cost == True and epoch % 100 == 0:
print("Cost after epoch %i: %f" % (epoch, epoch_cost))
if print_cost == True and epoch % 5 == 0:
costs.append(epoch_cost)
# plot the cost
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
# lets save the parameters in a variable
parameters = sess.run(parameters)
print("Parameters have been trained!")
# Calculate the correct predictions
correct_prediction = tf.equal(tf.argmax(ZL), tf.argmax(Y))
# Calculate accuracy on the test set
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
return parameters
python tensorflow pandas
$endgroup$
bumped to the homepage by Community♦ 9 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
|
show 1 more comment
$begingroup$
I am new to Tensorflow. I have trained Tensorflow model, but I need to take model predictions and add them to my original test set as a column. How can I do that?
def model(self, layers_dims, X_train, Y_train, X_test, Y_test, learning_rate=0.00001,
num_epochs=1000, print_cost=True):
"""
Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
Arguments:
X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
X_test -- training set, of shape (input size = 12288, number of training examples = 120)
Y_test -- test set, of shape (output size = 6, number of test examples = 120)
learning_rate -- learning rate of the optimization
num_epochs -- number of epochs of the optimization loop
minibatch_size -- size of a minibatch
print_cost -- True to print the cost every 100 epochs
Returns:
parameters -- parameters learnt by the model. They can then be used to predict.
"""
ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables
tf.set_random_seed(1) # to keep consistent results
seed = 3 # to keep consistent results
(n_x, m) = X_train.shape # (n_x: input size, m : number of examples in the train set)
n_y = Y_train.shape[0] # n_y : output size
#print('Ytrain shape', Y_train.shape)
costs = # To keep track of the cost
# Create Placeholders of shape (n_x, n_y)
X, Y = self.create_placeholders(n_x, n_y)
# Initialize parameters
parameters = NN_predict_trading_decisions.initialize_parameters(layers_dims)
# Forward propagation: Build the forward propagation in the tensorflow graph
ZL = NN_predict_trading_decisions.forward_propagation(X, parameters)
# Cost function: Add cost function to tensorflow graph
cost = NN_predict_trading_decisions.compute_cost(ZL, Y)
# Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
#optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initialize all the variables
init = tf.global_variables_initializer()
# Start the session to compute the tensorflow graph
with tf.Session() as sess:
# Run the initialization
sess.run(init)
# Do the training loop
for epoch in range(num_epochs):
epoch_cost = 0. # Defines a cost related to an epoch
current_cost = sess.run([optimizer, cost], feed_dict= {X: X_train, Y: Y_train})
epoch_cost += current_cost[1]
# Print the cost every epoch
if print_cost == True and epoch % 100 == 0:
print("Cost after epoch %i: %f" % (epoch, epoch_cost))
if print_cost == True and epoch % 5 == 0:
costs.append(epoch_cost)
# plot the cost
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
# lets save the parameters in a variable
parameters = sess.run(parameters)
print("Parameters have been trained!")
# Calculate the correct predictions
correct_prediction = tf.equal(tf.argmax(ZL), tf.argmax(Y))
# Calculate accuracy on the test set
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
return parameters
python tensorflow pandas
$endgroup$
bumped to the homepage by Community♦ 9 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
$begingroup$
Save output in a list or numpy array.
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:29
$begingroup$
How can I do that? Sorry, I am very new)
$endgroup$
– Myron Leskiv
Sep 5 '18 at 10:34
$begingroup$
Can you post some part of your code where you are getting predictions after training?
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:38
$begingroup$
So, I did a little research and found this post, it would be useful for you. stackoverflow.com/questions/40002084/…
$endgroup$
– Ankit Seth
Sep 5 '18 at 14:35
$begingroup$
Are your predictions the variableZL? Or are you talking about thecorrect_predictionvariable? What do you want to put in a DataFrame and what type is it? You can runtype(my_variable)on it.
$endgroup$
– n1k31t4
Sep 5 '18 at 14:51
|
show 1 more comment
$begingroup$
I am new to Tensorflow. I have trained Tensorflow model, but I need to take model predictions and add them to my original test set as a column. How can I do that?
def model(self, layers_dims, X_train, Y_train, X_test, Y_test, learning_rate=0.00001,
num_epochs=1000, print_cost=True):
"""
Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
Arguments:
X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
X_test -- training set, of shape (input size = 12288, number of training examples = 120)
Y_test -- test set, of shape (output size = 6, number of test examples = 120)
learning_rate -- learning rate of the optimization
num_epochs -- number of epochs of the optimization loop
minibatch_size -- size of a minibatch
print_cost -- True to print the cost every 100 epochs
Returns:
parameters -- parameters learnt by the model. They can then be used to predict.
"""
ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables
tf.set_random_seed(1) # to keep consistent results
seed = 3 # to keep consistent results
(n_x, m) = X_train.shape # (n_x: input size, m : number of examples in the train set)
n_y = Y_train.shape[0] # n_y : output size
#print('Ytrain shape', Y_train.shape)
costs = # To keep track of the cost
# Create Placeholders of shape (n_x, n_y)
X, Y = self.create_placeholders(n_x, n_y)
# Initialize parameters
parameters = NN_predict_trading_decisions.initialize_parameters(layers_dims)
# Forward propagation: Build the forward propagation in the tensorflow graph
ZL = NN_predict_trading_decisions.forward_propagation(X, parameters)
# Cost function: Add cost function to tensorflow graph
cost = NN_predict_trading_decisions.compute_cost(ZL, Y)
# Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
#optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initialize all the variables
init = tf.global_variables_initializer()
# Start the session to compute the tensorflow graph
with tf.Session() as sess:
# Run the initialization
sess.run(init)
# Do the training loop
for epoch in range(num_epochs):
epoch_cost = 0. # Defines a cost related to an epoch
current_cost = sess.run([optimizer, cost], feed_dict= {X: X_train, Y: Y_train})
epoch_cost += current_cost[1]
# Print the cost every epoch
if print_cost == True and epoch % 100 == 0:
print("Cost after epoch %i: %f" % (epoch, epoch_cost))
if print_cost == True and epoch % 5 == 0:
costs.append(epoch_cost)
# plot the cost
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
# lets save the parameters in a variable
parameters = sess.run(parameters)
print("Parameters have been trained!")
# Calculate the correct predictions
correct_prediction = tf.equal(tf.argmax(ZL), tf.argmax(Y))
# Calculate accuracy on the test set
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
return parameters
python tensorflow pandas
$endgroup$
I am new to Tensorflow. I have trained Tensorflow model, but I need to take model predictions and add them to my original test set as a column. How can I do that?
def model(self, layers_dims, X_train, Y_train, X_test, Y_test, learning_rate=0.00001,
num_epochs=1000, print_cost=True):
"""
Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
Arguments:
X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
X_test -- training set, of shape (input size = 12288, number of training examples = 120)
Y_test -- test set, of shape (output size = 6, number of test examples = 120)
learning_rate -- learning rate of the optimization
num_epochs -- number of epochs of the optimization loop
minibatch_size -- size of a minibatch
print_cost -- True to print the cost every 100 epochs
Returns:
parameters -- parameters learnt by the model. They can then be used to predict.
"""
ops.reset_default_graph() # to be able to rerun the model without overwriting tf variables
tf.set_random_seed(1) # to keep consistent results
seed = 3 # to keep consistent results
(n_x, m) = X_train.shape # (n_x: input size, m : number of examples in the train set)
n_y = Y_train.shape[0] # n_y : output size
#print('Ytrain shape', Y_train.shape)
costs = # To keep track of the cost
# Create Placeholders of shape (n_x, n_y)
X, Y = self.create_placeholders(n_x, n_y)
# Initialize parameters
parameters = NN_predict_trading_decisions.initialize_parameters(layers_dims)
# Forward propagation: Build the forward propagation in the tensorflow graph
ZL = NN_predict_trading_decisions.forward_propagation(X, parameters)
# Cost function: Add cost function to tensorflow graph
cost = NN_predict_trading_decisions.compute_cost(ZL, Y)
# Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer.
#optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Initialize all the variables
init = tf.global_variables_initializer()
# Start the session to compute the tensorflow graph
with tf.Session() as sess:
# Run the initialization
sess.run(init)
# Do the training loop
for epoch in range(num_epochs):
epoch_cost = 0. # Defines a cost related to an epoch
current_cost = sess.run([optimizer, cost], feed_dict= {X: X_train, Y: Y_train})
epoch_cost += current_cost[1]
# Print the cost every epoch
if print_cost == True and epoch % 100 == 0:
print("Cost after epoch %i: %f" % (epoch, epoch_cost))
if print_cost == True and epoch % 5 == 0:
costs.append(epoch_cost)
# plot the cost
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
# lets save the parameters in a variable
parameters = sess.run(parameters)
print("Parameters have been trained!")
# Calculate the correct predictions
correct_prediction = tf.equal(tf.argmax(ZL), tf.argmax(Y))
# Calculate accuracy on the test set
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Train Accuracy:", accuracy.eval({X: X_train, Y: Y_train}))
print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))
return parameters
python tensorflow pandas
python tensorflow pandas
edited Sep 5 '18 at 11:00
Myron Leskiv
asked Sep 5 '18 at 10:25
Myron LeskivMyron Leskiv
566
566
bumped to the homepage by Community♦ 9 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 9 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
$begingroup$
Save output in a list or numpy array.
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:29
$begingroup$
How can I do that? Sorry, I am very new)
$endgroup$
– Myron Leskiv
Sep 5 '18 at 10:34
$begingroup$
Can you post some part of your code where you are getting predictions after training?
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:38
$begingroup$
So, I did a little research and found this post, it would be useful for you. stackoverflow.com/questions/40002084/…
$endgroup$
– Ankit Seth
Sep 5 '18 at 14:35
$begingroup$
Are your predictions the variableZL? Or are you talking about thecorrect_predictionvariable? What do you want to put in a DataFrame and what type is it? You can runtype(my_variable)on it.
$endgroup$
– n1k31t4
Sep 5 '18 at 14:51
|
show 1 more comment
$begingroup$
Save output in a list or numpy array.
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:29
$begingroup$
How can I do that? Sorry, I am very new)
$endgroup$
– Myron Leskiv
Sep 5 '18 at 10:34
$begingroup$
Can you post some part of your code where you are getting predictions after training?
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:38
$begingroup$
So, I did a little research and found this post, it would be useful for you. stackoverflow.com/questions/40002084/…
$endgroup$
– Ankit Seth
Sep 5 '18 at 14:35
$begingroup$
Are your predictions the variableZL? Or are you talking about thecorrect_predictionvariable? What do you want to put in a DataFrame and what type is it? You can runtype(my_variable)on it.
$endgroup$
– n1k31t4
Sep 5 '18 at 14:51
$begingroup$
Save output in a list or numpy array.
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:29
$begingroup$
Save output in a list or numpy array.
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:29
$begingroup$
How can I do that? Sorry, I am very new)
$endgroup$
– Myron Leskiv
Sep 5 '18 at 10:34
$begingroup$
How can I do that? Sorry, I am very new)
$endgroup$
– Myron Leskiv
Sep 5 '18 at 10:34
$begingroup$
Can you post some part of your code where you are getting predictions after training?
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:38
$begingroup$
Can you post some part of your code where you are getting predictions after training?
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:38
$begingroup$
So, I did a little research and found this post, it would be useful for you. stackoverflow.com/questions/40002084/…
$endgroup$
– Ankit Seth
Sep 5 '18 at 14:35
$begingroup$
So, I did a little research and found this post, it would be useful for you. stackoverflow.com/questions/40002084/…
$endgroup$
– Ankit Seth
Sep 5 '18 at 14:35
$begingroup$
Are your predictions the variable
ZL? Or are you talking about the correct_prediction variable? What do you want to put in a DataFrame and what type is it? You can run type(my_variable) on it.$endgroup$
– n1k31t4
Sep 5 '18 at 14:51
$begingroup$
Are your predictions the variable
ZL? Or are you talking about the correct_prediction variable? What do you want to put in a DataFrame and what type is it? You can run type(my_variable) on it.$endgroup$
– n1k31t4
Sep 5 '18 at 14:51
|
show 1 more comment
2 Answers
2
active
oldest
votes
$begingroup$
Thanks to Ankit Seth I found the answer.
#this returns output layer of forward prop with trained parameters of the model.
pred = forward_propagation(X, parameters)
predictions_sigm = pred.eval(feed_dict = {X: X_test})
predictions_sigm = sigmoid(predictions)
#pred.eval returns linear regression layer for each class separately, so we need to pick index of maximum of them. I don't use softmax layer, since the output should be the same.
#init list of classes
y_list =
for i in range(predictions_sigm.shape[1]):
class_list =
class_list.append(predictions_sigm[0][i])
class_list.append(predictions_sigm[1][i])
class_list.append(predictions_sigm[2][i])
class_list.append(predictions_sigm[3][i])
#get index of maximum value of sigmoid. it will correspond to class
y = np.argmax(class_list)
y_list.append(y)
#then append y_list to dataframe
df['predicted_y'] = y_list
$endgroup$
add a comment |
$begingroup$
Create a list of the inputs, run each input through your model and save the prediction into a list then you can run the following code.
preds = YOUR_LIST_OF_PREDICTION_FROM_NN
result = pd.DataFrame(data={'Id': YOUR_TEST_DATAFRAME['Id'], 'PREDICTION_COLUM_NAME': preds})
result.to_csv(path_or_buf='submittion.csv', index = False, header = True)
Then extract the prediction from a tensor in Tensorflow.
This will extract data from Tensor:
pred = forward_propagation(X, parameters)
predictions = pred.eval(feed_dict = {X: X_test})
$endgroup$
1
$begingroup$
you copied my post from Kaggle)
$endgroup$
– Myron Leskiv
Sep 6 '18 at 10:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f37831%2fhow-to-save-tensorflow-predictions-to-data-frame%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Thanks to Ankit Seth I found the answer.
#this returns output layer of forward prop with trained parameters of the model.
pred = forward_propagation(X, parameters)
predictions_sigm = pred.eval(feed_dict = {X: X_test})
predictions_sigm = sigmoid(predictions)
#pred.eval returns linear regression layer for each class separately, so we need to pick index of maximum of them. I don't use softmax layer, since the output should be the same.
#init list of classes
y_list =
for i in range(predictions_sigm.shape[1]):
class_list =
class_list.append(predictions_sigm[0][i])
class_list.append(predictions_sigm[1][i])
class_list.append(predictions_sigm[2][i])
class_list.append(predictions_sigm[3][i])
#get index of maximum value of sigmoid. it will correspond to class
y = np.argmax(class_list)
y_list.append(y)
#then append y_list to dataframe
df['predicted_y'] = y_list
$endgroup$
add a comment |
$begingroup$
Thanks to Ankit Seth I found the answer.
#this returns output layer of forward prop with trained parameters of the model.
pred = forward_propagation(X, parameters)
predictions_sigm = pred.eval(feed_dict = {X: X_test})
predictions_sigm = sigmoid(predictions)
#pred.eval returns linear regression layer for each class separately, so we need to pick index of maximum of them. I don't use softmax layer, since the output should be the same.
#init list of classes
y_list =
for i in range(predictions_sigm.shape[1]):
class_list =
class_list.append(predictions_sigm[0][i])
class_list.append(predictions_sigm[1][i])
class_list.append(predictions_sigm[2][i])
class_list.append(predictions_sigm[3][i])
#get index of maximum value of sigmoid. it will correspond to class
y = np.argmax(class_list)
y_list.append(y)
#then append y_list to dataframe
df['predicted_y'] = y_list
$endgroup$
add a comment |
$begingroup$
Thanks to Ankit Seth I found the answer.
#this returns output layer of forward prop with trained parameters of the model.
pred = forward_propagation(X, parameters)
predictions_sigm = pred.eval(feed_dict = {X: X_test})
predictions_sigm = sigmoid(predictions)
#pred.eval returns linear regression layer for each class separately, so we need to pick index of maximum of them. I don't use softmax layer, since the output should be the same.
#init list of classes
y_list =
for i in range(predictions_sigm.shape[1]):
class_list =
class_list.append(predictions_sigm[0][i])
class_list.append(predictions_sigm[1][i])
class_list.append(predictions_sigm[2][i])
class_list.append(predictions_sigm[3][i])
#get index of maximum value of sigmoid. it will correspond to class
y = np.argmax(class_list)
y_list.append(y)
#then append y_list to dataframe
df['predicted_y'] = y_list
$endgroup$
Thanks to Ankit Seth I found the answer.
#this returns output layer of forward prop with trained parameters of the model.
pred = forward_propagation(X, parameters)
predictions_sigm = pred.eval(feed_dict = {X: X_test})
predictions_sigm = sigmoid(predictions)
#pred.eval returns linear regression layer for each class separately, so we need to pick index of maximum of them. I don't use softmax layer, since the output should be the same.
#init list of classes
y_list =
for i in range(predictions_sigm.shape[1]):
class_list =
class_list.append(predictions_sigm[0][i])
class_list.append(predictions_sigm[1][i])
class_list.append(predictions_sigm[2][i])
class_list.append(predictions_sigm[3][i])
#get index of maximum value of sigmoid. it will correspond to class
y = np.argmax(class_list)
y_list.append(y)
#then append y_list to dataframe
df['predicted_y'] = y_list
answered Sep 6 '18 at 7:07
Myron LeskivMyron Leskiv
566
566
add a comment |
add a comment |
$begingroup$
Create a list of the inputs, run each input through your model and save the prediction into a list then you can run the following code.
preds = YOUR_LIST_OF_PREDICTION_FROM_NN
result = pd.DataFrame(data={'Id': YOUR_TEST_DATAFRAME['Id'], 'PREDICTION_COLUM_NAME': preds})
result.to_csv(path_or_buf='submittion.csv', index = False, header = True)
Then extract the prediction from a tensor in Tensorflow.
This will extract data from Tensor:
pred = forward_propagation(X, parameters)
predictions = pred.eval(feed_dict = {X: X_test})
$endgroup$
1
$begingroup$
you copied my post from Kaggle)
$endgroup$
– Myron Leskiv
Sep 6 '18 at 10:20
add a comment |
$begingroup$
Create a list of the inputs, run each input through your model and save the prediction into a list then you can run the following code.
preds = YOUR_LIST_OF_PREDICTION_FROM_NN
result = pd.DataFrame(data={'Id': YOUR_TEST_DATAFRAME['Id'], 'PREDICTION_COLUM_NAME': preds})
result.to_csv(path_or_buf='submittion.csv', index = False, header = True)
Then extract the prediction from a tensor in Tensorflow.
This will extract data from Tensor:
pred = forward_propagation(X, parameters)
predictions = pred.eval(feed_dict = {X: X_test})
$endgroup$
1
$begingroup$
you copied my post from Kaggle)
$endgroup$
– Myron Leskiv
Sep 6 '18 at 10:20
add a comment |
$begingroup$
Create a list of the inputs, run each input through your model and save the prediction into a list then you can run the following code.
preds = YOUR_LIST_OF_PREDICTION_FROM_NN
result = pd.DataFrame(data={'Id': YOUR_TEST_DATAFRAME['Id'], 'PREDICTION_COLUM_NAME': preds})
result.to_csv(path_or_buf='submittion.csv', index = False, header = True)
Then extract the prediction from a tensor in Tensorflow.
This will extract data from Tensor:
pred = forward_propagation(X, parameters)
predictions = pred.eval(feed_dict = {X: X_test})
$endgroup$
Create a list of the inputs, run each input through your model and save the prediction into a list then you can run the following code.
preds = YOUR_LIST_OF_PREDICTION_FROM_NN
result = pd.DataFrame(data={'Id': YOUR_TEST_DATAFRAME['Id'], 'PREDICTION_COLUM_NAME': preds})
result.to_csv(path_or_buf='submittion.csv', index = False, header = True)
Then extract the prediction from a tensor in Tensorflow.
This will extract data from Tensor:
pred = forward_propagation(X, parameters)
predictions = pred.eval(feed_dict = {X: X_test})
edited Sep 6 '18 at 8:58
ebrahimi
74421022
74421022
answered Sep 6 '18 at 7:48
Prasan KarunarathnaPrasan Karunarathna
113
113
1
$begingroup$
you copied my post from Kaggle)
$endgroup$
– Myron Leskiv
Sep 6 '18 at 10:20
add a comment |
1
$begingroup$
you copied my post from Kaggle)
$endgroup$
– Myron Leskiv
Sep 6 '18 at 10:20
1
1
$begingroup$
you copied my post from Kaggle)
$endgroup$
– Myron Leskiv
Sep 6 '18 at 10:20
$begingroup$
you copied my post from Kaggle)
$endgroup$
– Myron Leskiv
Sep 6 '18 at 10:20
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f37831%2fhow-to-save-tensorflow-predictions-to-data-frame%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Save output in a list or numpy array.
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:29
$begingroup$
How can I do that? Sorry, I am very new)
$endgroup$
– Myron Leskiv
Sep 5 '18 at 10:34
$begingroup$
Can you post some part of your code where you are getting predictions after training?
$endgroup$
– Ankit Seth
Sep 5 '18 at 10:38
$begingroup$
So, I did a little research and found this post, it would be useful for you. stackoverflow.com/questions/40002084/…
$endgroup$
– Ankit Seth
Sep 5 '18 at 14:35
$begingroup$
Are your predictions the variable
ZL? Or are you talking about thecorrect_predictionvariable? What do you want to put in a DataFrame and what type is it? You can runtype(my_variable)on it.$endgroup$
– n1k31t4
Sep 5 '18 at 14:51