数组在python中不可调用“'numpy.ndarray'对象不可调用”

时间:2018-12-08 08:08:39

标签: python numpy-ndarray

我正在使用神经网络,当我尝试将两个numpy.ndarray改组时,出现此错误。我尝试重新检查随机播放功能格式,但找不到任何错误。请帮助

train_images,train_labels = shuffle(train_images,train_labels)
TypeError                                 
Traceback (most recent call last)
<ipython-input-8-b3f4173331ac> in <module>
 18     print("Training the Network")
 19     for i in range(epoch):
 20     --> train_images,train_labels = shuffle(train_images,train_labels)
 21         for offset in range (0,no_eg,batch_size):
 22             end = offset+batch_size

/usr/lib/python3.5/random.py in shuffle(self, x, random)
275             for i in reversed(range(1, len(x))):
276                 # pick an element in x[:i+1] with which to exchange x[i]
277            -->  j = _int(random() * (i+1))
278                 x[i], x[j] = x[j], x[i]
279 

TypeError: 'numpy.ndarray' object is not callabl

谢谢

2 个答案:

答案 0 :(得分:1)

看看random.shuffle(x[, random])

的文档
  

可选参数random是一个0参数的函数,返回一个   [0.0,1.0)中的随机浮点数;默认情况下,这是函数random()

在您的情况下,您传递了train_labels,根据错误消息,该标签为numpy.ndarray,而不起作用

答案 1 :(得分:0)

您可能要使用两个名为shuffle的函数,但它们都不符合您的期望。

random.shuffle(x, random=None)使用功能x对列表random进行混洗。

numpy.random.shuffle(x)将NumPy数组x改组。

这两个函数一次只能混洗一个数组,但是您想混洗两个数组,并且想要连续地混洗它们。考虑构建一个熊猫系列,对系列进行改组(“采样”),然后再次将其拆分为值和标签:

import pandas as pd
series = pd.Series(train_images, index=train_labels)
shuffled = series.sample(series.size)
train_images_shuffled = shuffled.values
train_labels_shuffled = shuffled.index