Search code examples
pythontensorflownon-linear-regression

TensorFlow regression with error :"could not convert string to float: "


Problem:

ValueError: could not convert string to float:

I have been sticking here for several days, and someone told me that Stack Overflow my solve my problem. This is the first time for me to ask question, so pleas forgive any possible mistakes.

The code is aimed to find relation between 15 inputs and 1 out put and ran under Jupyter. Data is extracted from 'data.xls' by using 'xlrd' and stored into list. I planned to represent the loss by calculating the mean squared error.

Thanks you !

import xlrd
import numpy
import tensorflow as tf

book=xlrd.open_workbook('data.xls')
sheet0=book.sheet_by_index(0)
sheet_name=book.sheet_names()[0]

rows_number=sheet0.nrows

X=[]
for i in range(rows_number-1):
    temp=sheet0.row_values(i+1)
    del temp[0:4]
    X.append(temp)

Y=[]
for i in range(rows_number-1):
    temp=sheet0.row_values(i+1)
    Y.append([temp[3]])


w1= tf.Variable(tf.random_normal([15, 10],name='matrix1', stddev=1))
b1 = tf.Variable(tf.constant(0.1, shape=[10]))
w2= tf.Variable(tf.random_normal([10, 10],name='matrix2', stddev=1))
b2 = tf.Variable(tf.constant(0.1, shape=[10]))
w3= tf.Variable(tf.random_normal([10, 1],name='matrix3', stddev=1))

x = tf.placeholder(tf.float32, shape=(None, 15), name="x-input")
y_= tf.placeholder(tf.float32, shape=(None, 1), name='y-input')

a1= tf.add(tf.matmul(x, w1),b1)
a2=tf.add(tf.matmul(tf.nn.sigmoid(a1),w2),b2)
y=tf.matmul(tf.nn.sigmoid(a2),w3)
y=tf.nn.sigmoid(y)

loss = tf.losses.mean_squared_error(y_, y)
train=tf.train.AdamOptimizer(0.1).minimize(loss)

with tf.Session() as sess:

    init_op = tf.global_variables_initializer()
    sess.run(init_op)


    STEPS = 30000
    for i in range(STEPS):
        sess.run(train, feed_dict={x: X, y_: Y})

ValueError                                Traceback (most recent call last)
<ipython-input-22-de3ef36f5080> in <module>()
      7     STEPS = 30000
      8     for i in range(STEPS):
----> 9         sess.run(train, feed_dict={x: X, y_: Y})
     10 
     11 

~\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    898     try:
    899       result = self._run(None, fetches, feed_dict, options_ptr,
--> 900                          run_metadata_ptr)
    901       if run_metadata:
    902         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\Anaconda3\envs\ML\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1102             feed_handles[subfeed_t] = subfeed_val
   1103           else:
-> 1104             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
   1105 
   1106           if (not is_tensor_handle_feed and

~\Anaconda3\envs\ML\lib\site-packages\numpy\core\numeric.py in asarray(a, dtype, order)
    490 
    491     """
--> 492     return array(a, dtype, copy=False, order=order)
    493 
    494 

ValueError: could not convert string to float: 

I have checked the data type of the elements of two list X and Y. And the shape is (835,15) for X, (835,1) for Y.

Here is the content of X and Y X-inputY-input


Solution

  • When empty string(' ') is fed, it gives the error.


    The excel has several empty cells, so some values are empty and can not be converted into float. When empty string(' ') is fed, it gives the error.