简体   繁体   中英

Create a Numpy array from parts of other arrays in Python

EDIT

So I just understand now that Python and Numpy just don't do polymorphism very well. Given the same data, it has to be put into the right form before most functions can use it. So expecting Python to be able to 'one-line' something is beyond its capabilities.

Just for comparison, MATLAB doesn't do polymorphism very well either but it's much less noticeable as by default it keeps all numeric data as a 2D array so the majority of functions will work with the majority of data - making it soooo much easier

EDIT

I'm pretty new to Python and struggling to create new arrays out of existing arrays:

In MATLAB the following works to create a column vector from other column vectors:

a = [b(5:6); c(N:M); d(1:P); e(Q)]

with a lot of computational flexibility (Q could be a vector for example).

In Python, I can't find a nice command to add multiple 1D NumPy arrays together and it seems to have lots of issues with single values as it changes them from NumPy arrays to some other format, WHY?!

Can anyone give me a single line of code to carry out the above? It'd be great to see - so far all I've got is lines and lines of checking for the indexing variables (N, M, P, Q) and soooo many np.array(..)'s everywhere to try and keep things the same data type.

I've tried np.append but that doesn't work for multiple vectors (I could nest them but that seems very ugly, esp if I need to add many arrays) and np.concatenate complains that something is 0-dimensional, I don't understand that at all.

concatenate has no problems with a bunch of 1d array:

In [52]: np.concatenate([np.array([1,2,3]), np.ones((2,)), np.array([1])])
Out[52]: array([1., 2., 3., 1., 1., 1.])

If one argument is scalar:

In [53]: np.concatenate([np.array([1,2,3]), np.ones((2,)), 1])
Traceback (most recent call last):
  File "<ipython-input-53-51b00c09f677>", line 1, in <module>
    np.concatenate([np.array([1,2,3]), np.ones((2,)), 1])
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 2 has 0 dimension(s)

It make a array from the last

In [57]: np.array(1)
Out[57]: array(1)

but that's a 0d array. In MATLAB that would be 2d - everything is 2d, there's not true scalars. Remember, numpy works in python, which has scalars and lists. MATLAB is matrices all the way down ...

Also numpy arrays can be 0d or 1d. There's no artificial 2d lower bound. It's a general array language, not just matrices. In MATLAB even 3d is a tweak on the original 2d.

hstack adds a tweak to make sure all arguments are at least 1d:

In [54]: np.hstack([np.array([1,2,3]), np.ones((2,)), 1])
Out[54]: array([1., 2., 3., 1., 1., 1.])

Even in MATLAB/Octave mismatched dimensions give problems:

>> a = [3:5; [1,2,3]]
a =    
   3   4   5
   1   2   3

>> a = [3:5; [1,2,3]; 4]
error: vertical dimensions mismatch (2x3 vs 1x1)

>> a = [3:5; [1,2,3,4]]
error: vertical dimensions mismatch (1x3 vs 1x4)
>> a = [3:5, [1,2,3,4]]
a =    
   3   4   5   1   2   3   4

>> a = [3:5, [1,2,3,4],5]
a =
   3   4   5   1   2   3   4   5

你会想看看 np.concatenate 它采用数组和轴的序列来连接它们

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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