Python Forum
The problem of converting string to float by using CSV file
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The problem of converting string to float by using CSV file
#1
my question here
I try to use CSV file as the input of the neural network. But I got the warming as  'could not convert string to float: 'train2.CSV'
' My CSV files contain 15 columns. I have no idea how to convert it to float type. My data is over 10K.
Here is my CSV data looks like

12.71 17.76 91.42 30.78 0 0 6.9 0 978 3576 205 326 4500 800 5300
85.12 25.81 9.82 32.48 0 0 6.89 2.12 1036 4039 213 771 1596 4061 5657
91.24 8.56 23.33 30.61 0 0 6.87 1.93 1190 3823 209 631 1599 4065 5664
23.39 18.12 89.47 0 2.64 22.95 6.86 4.26 952 3600 204 933 3745 806 4551
22.65 20.28 81.53 0 1.71 19.32 6.85 4.23 970 3340 200 572 3745 805 4550
32.92 4.92 86.69 34.47 0 0 6.85 1.92 998 3768 209 639 3600 800 4400
15.93 0 83.33 31.93 5.1 22.06 6.84 0 1020 3780 187 443 3548 803 4351
92.41 27.12 17.83 16.83 0 0 6.83 3.62 952 2478 228 906 1500 4000 5500
0 0 145.89 5.1 2.31 0 6.8 4.91 1384 3624 243 842 4800 0 4800
84.45 25.51 10.08 33.16 0 0 6.8 1.5 1152 3603 217 618 1599 4061 5660


import tensorflow as tf
#from tensorflow.examples.tutorials.mnist import input_data

import numpy as np

train_x,train_y,test_x,test_y = ('train2.CSV','trainY.CSV','test2.CSV.','Text Y.CSV.')
n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500

n_classes = 2
batch_size = 100
hm_epochs = 10

x = tf.placeholder('float')
y = tf.placeholder('float')

hidden_1_layer = {'f_fum':n_nodes_hl1,
                 'weight':tf.Variable(tf.random_normal([len(train_x[0]), n_nodes_hl1])),
                 'bias':tf.Variable(tf.random_normal([n_nodes_hl1]))}

hidden_2_layer = {'f_fum':n_nodes_hl2,
                 'weight':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                 'bias':tf.Variable(tf.random_normal([n_nodes_hl2]))}

hidden_3_layer = {'f_fum':n_nodes_hl3,
                 'weight':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                 'bias':tf.Variable(tf.random_normal([n_nodes_hl3]))}

output_layer = {'f_fum':None,
               'weight':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
               'bias':tf.Variable(tf.random_normal([n_classes])),}


# Nothing changes
def neural_network_model(data):

   l1 = tf.add(tf.matmul(data,hidden_1_layer['weight']), hidden_1_layer['bias'])
   l1 = tf.nn.relu(l1)

   l2 = tf.add(tf.matmul(l1,hidden_2_layer['weight']), hidden_2_layer['bias'])
   l2 = tf.nn.relu(l2)

   l3 = tf.add(tf.matmul(l2,hidden_3_layer['weight']), hidden_3_layer['bias'])
   l3 = tf.nn.relu(l3)

   output = tf.matmul(l3,output_layer['weight']) + output_layer['bias']

   return output

def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
   #tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(prediction,y) )
    an easier, more compact way. 
   
   
   
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
   
for epoch in range(hm_epochs):
epoch_loss = 0
i=0
while i < len(train_x):
start = i
end = i+batch_size
batch_x = np.array(train_x[start:end])
batch_y = np.array(train_y[start:end])

_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                             y: batch_y})
epoch_loss += c
i+=batch_size

print('Epoch', epoch+1, 'completed out of',hm_epochs,'loss:',epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))

   
train_neural_network(x)
Reply


Messages In This Thread
The problem of converting string to float by using CSV file - by CChen - Jul-11-2017, 02:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem of converting Matlab code to python DongyanZ 2 2,164 Feb-03-2023, 01:04 PM
Last Post: jefsummers
Sad ValueError: could not convert string to float badju 0 5,413 Jul-01-2021, 12:13 AM
Last Post: badju
  Indirectlty convert string to float in JSON file WBPYTHON 6 8,210 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  ValueError: could not convert string to float RahulSingh 3 5,398 Apr-09-2020, 02:59 PM
Last Post: dinesh
  Help batch converting .json chosen file to MySQL BrandonKastning 2 3,009 Mar-14-2020, 09:19 PM
Last Post: BrandonKastning
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 7,457 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  Converting Dataframe in Python from Object to Float marco_ita 11 16,975 Jan-09-2020, 12:33 PM
Last Post: jefsummers
  [split] Converting excel file to txt file unexceptionalhobby 2 5,306 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Converting string the pandas dataframe chrismc 0 2,808 Jan-24-2019, 11:07 AM
Last Post: chrismc
  ValueError: could not convert the string to float Grin 3 11,339 Jun-14-2018, 08:17 PM
Last Post: killerrex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020