ValueError:使用序列设置数组元素。

时间:2017-04-07 07:44:20

标签: python numpy tensorflow

我正在使用iris数据集处理分类问题,我遇到了ValueError问题:设置一个带序列的数组元素。我将数据集转换为numpy.ndarray,但仍然有相同的错误 这是我的代码

import tensorflow as tf 
import numpy as np 
import pandas as pd 

rawdata = pd.read_csv('/home/akki/Desktop/TF/IRIS.csv')
print(np.shape(rawdata))
rawdatashuffel = rawdata.reindex(np.random.permutation(rawdata.index)).as_matrix()


data = rawdatashuffel[:,range(0,7)]
print(np.shape(data))
labels = rawdatashuffel[:,8]
print(np.shape(labels))
##############################################################
# one hot genration
Number_of_op_class = 3
lab = np.eye(Number_of_op_class)
print(lab)
#print(type(lab[0]))

###########################################################################
# converting label to one hot
for i in range(0,100):
    if labels[i] == 'setosa':
        labels[i] = lab[0]
    elif labels[i] == 'versicolor':
        labels[i] = lab[1]
    elif labels[i] == 'virginica':
        labels[i] = lab[2]
print(labels)
print(type(labels))
#labels = np.asarray(labels)




 train_data = data[range(0,79),:]
 train_label = labels[range(0,79)]

test_data = data[range(80,99),:]
test_labels = labels[range(80,99)]

#print(test_labels)

X = tf.placeholder(tf.float32)
y_ = tf.placeholder(tf.float32)

W=tf.Variable(tf.zeros([8,3]))
b=tf.Variable(tf.zeros([3]))

y = tf.nn.softmax(tf.matmul(X, W) + b)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

#optimiser -
train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
#calculating accuracy of our model 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


#session parameters
sess = tf.InteractiveSession()
#initialising variables
init = tf.initialize_all_variables()
sess.run(init)
#number of interations
epoch=2000

for step in range(epoch):
   _, c=sess.run([train_step,cross_entropy], feed_dict={X: train_data, y_: train_label})
    if step%500==0 :
       print(c)

我得到的错误是

Traceback (most recent call last):
  File "iris.py", line 70, in <module>
    _, c=sess.run([train_step,cross_entropy], feed_dict={X: train_data, y_: train_label})
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 372, in run
run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 619, in _run
    np_val = np.array(subfeed_val, dtype=subfeed_dtype)
ValueError: setting an array element with a sequence.

0 个答案:

没有答案