One-dimensional and two-dimensional | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

One-dimensional and two-dimensional

How to make two-dimensional into a one-dimensional vector? And can display 1 in ndim THX

22nd Dec 2018, 2:26 AM
yan yu
yan yu - avatar
6 Answers
+ 3
I'm guessing you're talking about numpy arrays. In that case, you can use the flatten method: import numpy as np a = np.array([[1, 2], [3, 4]]) b = a.flatten() print(b) # [1 2 3 4] print(b.ndim) # 1 Docs: https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.ndarray.flatten.html
22nd Dec 2018, 2:45 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
If you want the shape to be (1,35), you can use reshape() arrayB=arrayA.reshape((1,35)) But then arrayB would technically remain 2-dimensional. np.ndim(arrayB) would print 2.
22nd Dec 2018, 3:14 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
The tuple (1, 35) has length two. Thus ndim() on an array of shape (1, 35) will always return 2. More generally, the length of the tuple given by shape() will always be equal to ndim().
22nd Dec 2018, 3:33 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
Thanksgiving😀😀
22nd Dec 2018, 4:08 AM
yan yu
yan yu - avatar
+ 1
https://code.sololearn.com/cj7V45vYnA34/?ref=app I want to ask is to use arrayB=np.ravel (arrayA), then print(np.shape(arrayB)) will run out (35,), can it run out (1,35)?
22nd Dec 2018, 3:01 AM
yan yu
yan yu - avatar
0
So how to make np.shape (1,35) and np.ndim() as two-dimensional
22nd Dec 2018, 3:21 AM
yan yu
yan yu - avatar