问题
I am working on a neural network and when i try to shuffle the two numpy.ndarray i get this error. 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
回答1:
Have a look at the docs of random.shuffle(x[, random])
The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random()
in your case you pass train_labels, which, according to error message is numpy.ndarray, not function
回答2:
There are two functions named shuffle
that you may want to use, and none of them works the way you expect.
random.shuffle(x, random=None)
shuffles list x
using function random
.
numpy.random.shuffle(x)
shuffles a NumPy array 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
来源:https://stackoverflow.com/questions/53680712/array-is-not-callable-in-python-numpy-ndarray-object-is-not-callable