程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] conversion between (n, 1) and (n,) in numpy

編輯:Python

edition :
numpy=1.19.3
python=3.6

One 、 (n,1) turn (n,)

Code :

import numpy as np
a = np.array([[12],[26],[40],[66]])
print(a.shape)

result:

(4, 1)


take (4,1) To (4,)

a_post_1 = np.squeeze(a)
print(a_post_1.shape)

result:

(4,)

Two 、 (n,) turn (n,1)

Code :

import numpy as np
b = np.array([12,26,40,66])
print(b.shape)

result:

(4,)


take (4,) To (4,1)

b_post_1 = b.reshape(-1,1)
print(b_post_1.shape)

perhaps

b_post_2 = np.reshape(b,(-1,1))
print(b_post_2.shape)

result:

(4, 1)
(4, 1)

Reference resources :https://blog.csdn.net/zkp_987/article/details/119989767


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved