简体   繁体   English

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

[英]array is not callable in python “'numpy.ndarray' object is not callable”

I am working on a neural network and when i try to shuffle the two numpy.ndarray i get this error. 我正在使用神经网络,当我尝试将两个numpy.ndarray改组时,出现此错误。 I tried rechecking the shuffle function format and cannot find any faults with that. 我尝试重新检查随机播放功能格式,但找不到任何错误。 Please help 请帮忙

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

Thanks 谢谢

Have a look at the docs of random.shuffle(x[, random]) 看看random.shuffle(x [,random])的文档

The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); 可选参数random是一个0参数的函数,返回[0.0,1.0)中的一个随机浮点数; by default, this is the function random() 默认情况下,这是函数random()

in your case you pass train_labels, which, according to error message is numpy.ndarray, not function 在您的情况下,您传递了train_labels,根据错误消息它是numpy.ndarray,不起作用

There are two functions named shuffle that you may want to use, and none of them works the way you expect. 您可能要使用两个名为shuffle函数,但它们都不符合您的期望。

random.shuffle(x, random=None) shuffles list x using function random . random.shuffle(x, random=None)使用random函数对列表x进行random.shuffle(x, random=None)

numpy.random.shuffle(x) shuffles a NumPy array x . numpy.random.shuffle(x) NumPy数组x

Both functions can shuffle only one array at a time, but you want to shuffle two arrays, and you want to shuffle them consistently. 这两个函数一次只能混洗一个数组,但是您想混洗两个数组,并且希望始终对它们进行混洗。 Consider building a pandas Series, shuffling ("sampling") the Series, and then splitting it into values and labels again: 考虑构建一个熊猫系列,对系列进行改组(“采样”),然后再次将其拆分为值和标签:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM