Merge numpy arrays returned from loop

2024/10/7 20:30:47

I have a loop that generates numpy arrays:

for x in range(0, 1000):myArray = myFunction(x)

The returned array is always one dimensional. I want to combine all the arrays into one array (also one dimensional.

I tried the following, but it failed

allArrays = []
for x in range(0, 1000):myArray = myFunction(x)allArrays += myArray

The error is ValueError: operands could not be broadcast together with shapes (0) (9095). How can I get that to work?

For instance these two arrays:

[ 234 342 234 5454 34 6]
[ 23 2 1 4 55 34]

Shall be merge into this array:

[ 234 342 234 5454 34 6 23 2 1 4 55 34 ]
Answer

You probably mean

allArrays = np.array([])
for x in range(0, 1000):myArray = myFunction(x)allArrays = np.concatenate([allArrays, myArray])

A more concise approach (see wims answer) is to use a list comprehension,

allArrays = np.concatenate([myFunction(x) for x in range]) 
https://en.xdnf.cn/q/70205.html

Related Q&A

Play mp3 using Python, PyQt, and Phonon

I been trying all day to figure out the Qts Phonon library with Python. My long term goal is to see if I could get it to play a mms:// stream, but since I cant find an implementation of this done anywh…

Python dictionary keys(which are class objects) comparison with multiple comparer

I am using custom objects as keys in python dictionary. These objects has some default hash and eq methods defined which are being used in default comparison But in some function i need to use a diffe…

How can I make np.save work for an ndarray subclass?

I want to be able to save my array subclass to a npy file, and recover the result later.Something like:>>> class MyArray(np.ndarray): pass >>> data = MyArray(np.arange(10)) >>&g…

With ResNet50 the validation accuracy and loss is not changing

I am trying to do image recognition with ResNet50 in Python (keras). I tried to do the same task with VGG16, and I got some results like these (which seem okay to me): resultsVGG16 . The training and v…

string has incorrect type (expected str, got spacy.tokens.doc.Doc)

I have a dataframe:train_review = train[review] train_reviewIt looks like:0 With all this stuff going down at the moment w... 1 \The Classic War of the Worlds\" by Timothy Hi... 2 T…

custom URLs using django rest framework

I am trying to use the django rest framework to expose my models as APIs.serializersclass UserSerializer(serializers.HyperlinkedModelSerializer):class Meta:model = Userviewsetclass UserViewSet(viewsets…

Does python logging.FileHandler use block buffering by default?

The logging handler classes have a flush() method. And looking at the code, logging.FileHandler does not pass a specific buffering mode when calling open(). Therefore when you write to a log file, it …

Non brute force solution to Project Euler problem 25

Project Euler problem 25:The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be F1 = 1, F2 = 1, F3 = 2, F4 = 3, F5 =…

python:class attribute/variable inheritance with polymorphism?

In my endeavours as a python-apprentice i got recently stuck at some odd (from my point of view) behaviour if i tried to work with class attributes. Im not complaining, but would appreciate some helpfu…

Unable to load firefox in selenium webdriver in python

I have installed Python 3.6.2, Selenium 3.5.0 with GeckoDriver 0.18.0 and the firefox version is 54.0.1version on windows 7. I am trying to run a selenium script which is loading a firefox where i get …