'How does one create ndarrays from a function?
I am currently writing a neural network using the keras API. Therefore, I am structuring data that has a comparable input datastructure as that of the MNIST training dataset. It is a tuple consisting of 2 numpy ndarrays with shapes (60000, 28, 28) and (60000,)
I would like to extract 10 random samples out of 2 of my own datasets that represent the X_train and Y_train data. The link between the two are described by 2 definitions. This is working and returns samples as numpy arrays with the desired shapes.
My problem is that I don't know how to efficiently (without iterating) combine these such that an X_train dataset with shape (10, 20, 5) and a Y_train dataset with shape (10 ,2 ,10 ) can be combined to a tuple (X_train, Y_train). I placed the error message in the code. I don't understand why I couldn't set an array element with a sequence; as far as I can tell, it is comparable to the MNIST dataset. Could someone explain to me what is different and what I should do to create the desired tuple (or give a better option)?
I have tried np.fromfunctions and by using a list comprehension, but I thought these to be non applicable. I would love to hear if otherwise.
SAMPLES = 10
class PrepareTestData:
def randomSamples():
sample = random.randint(0,1000)
return sample
def inputData(samples):
print(samples)
inputdata = np.random.rand(20, 5)
print(type(inputdata))
return inputdata
def outputData(samples):
print(samples)
outputdata = np.random.rand(2, 10)
return outputdata
def mainTest():
inputArraySample = np.zeros(SAMPLES,)
outputArraySample = np.zeros(SAMPLES,)
for i in range(0, SAMPLES):
random_sample = PrepareTestData.randomSamples()
inputArraySample[i] = PrepareTestData.inputData(random_sample)
*ERROR: setting an array element with a sequence.*
outputArraySample[i] = PrepareTestData.inputData(random_sample)
final_array = np.core.records.fromarrays(inputArraySample, outputArraySample)
mainTest()
Solution 1:[1]
I did not know there was a way to do a numpy list comprehension. I turns out there is and here is the answer to my own question:
def mainTest():
random_samples = np.array([PrepareTestData.randomSamples() for x in range(SAMPLES)])
inputArraySample = np.array([PrepareTestData.inputData(samples ) for samples in random_samples ])
outputArraySample = np.array([PrepareTestData.outputData(samples ) for samples in random_samples ])
(x_train, Y_train) = (inputArraySample, outputArraySample)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bende |
