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

【python】numpy中(n,1)和(n,)相互轉換

編輯:Python

版本:
numpy=1.19.3
python=3.6

一、 (n,1) 轉 (n,)

代碼:

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

result:

(4, 1)


將(4,1)轉為(4,)

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

result:

(4,)

二、 (n,) 轉 (n,1)

代碼:

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

result:

(4,)


將(4,)轉為 (4,1)

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

或者

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

result:

(4, 1)
(4, 1)

參考:https://blog.csdn.net/zkp_987/article/details/119989767


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