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
#2
I tried adding python tags to your code, but it still losing the indentation. Please see the BBCode tutorial link in my signature and repost your code with proper formatting. Also post the entire traceback for the error you are getting.

And your csv data doesn't look like it's in csv format. I don't see any commas. Is whatever is loading the data expecting commas? If it can handle the format of your data, my guess is that there is something in your data that is not a floating point number, like N/A or something. You'll need to clean up your data before you can process it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-11-2017, 02:23 PM)ichabod801 Wrote: I tried adding python tags to your code, but it still losing the indentation. Please see the BBCode tutorial link in my signature and repost your code with proper formatting. Also post the entire traceback for the error you are getting. And your csv data doesn't look like it's in csv format. I don't see any commas. Is whatever is loading the data expecting commas? If it can handle the format of your data, my guess is that there is something in your data that is not a floating point number, like N/A or something. You'll need to clean up your data before you can process it.
OK, I will repost my code later. The data doesn't copy from the CSV file so that there is not commas there. I already checked my data. There is no N/A or missing value inside.

(Jul-11-2017, 02:23 PM)ichabod801 Wrote: I tried adding python tags to your code, but it still losing the indentation. Please see the BBCode tutorial link in my signature and repost your code with proper formatting. Also post the entire traceback for the error you are getting. And your csv data doesn't look like it's in csv format. I don't see any commas. Is whatever is loading the data expecting commas? If it can handle the format of your data, my guess is that there is something in your data that is not a floating point number, like N/A or something. You'll need to clean up your data before you can process it.
Here is the trace error:

WARNING:tensorflow:From D:\Anaconda4.4.0\envs\tensorflow\lib\site-packages\tensorflow\python\util\tf_should_use.py:170: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use tf.global_variables_initializer instead.




---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-39b7fba5be19> in <module>()
84
85
---> 86 train_neural_network(x)

<ipython-input-6-39b7fba5be19> in train_neural_network(x)
73
74 _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
---> 75 y: batch_y})
76 epoch_loss += c
77 i+=batch_size

D:\Anaconda4.4.0\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
787 try:
788 result = self._run(None, fetches, feed_dict, options_ptr,
--> 789 run_metadata_ptr)
790 if run_metadata:
791 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

D:\Anaconda4.4.0\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
966 feed_handles[subfeed_name] = subfeed_val
967 else:
--> 968 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
969
970 if (not is_tensor_handle_feed and

D:\Anaconda4.4.0\envs\tensorflow\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
529
530 """
--> 531 return array(a, dtype, copy=False, order=order)
532
533

ValueError: could not convert string to float: 'train2.CSV'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem of converting Matlab code to python DongyanZ 2 1,410 Feb-03-2023, 01:04 PM
Last Post: jefsummers
Sad ValueError: could not convert string to float badju 0 4,326 Jul-01-2021, 12:13 AM
Last Post: badju
  Indirectlty convert string to float in JSON file WBPYTHON 6 5,943 May-06-2020, 12:09 PM
Last Post: WBPYTHON
  ValueError: could not convert string to float RahulSingh 3 4,190 Apr-09-2020, 02:59 PM
Last Post: dinesh
  Help batch converting .json chosen file to MySQL BrandonKastning 2 2,327 Mar-14-2020, 09:19 PM
Last Post: BrandonKastning
  ValueError: could not convert string to float: '4 AVENUE' Kudzo 4 5,931 Jan-26-2020, 10:47 PM
Last Post: Kudzo
  Converting Dataframe in Python from Object to Float marco_ita 11 13,176 Jan-09-2020, 12:33 PM
Last Post: jefsummers
  [split] Converting excel file to txt file unexceptionalhobby 2 4,363 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Converting string the pandas dataframe chrismc 0 2,353 Jan-24-2019, 11:07 AM
Last Post: chrismc
  ValueError: could not convert the string to float Grin 3 10,234 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