Numpy維度知識總結
1、維度究竟是行數還是列數?
機器學習時總聽到m維行向量、n維列向量。究竟維度是啥?這里的維度和numpy中一樣嘛?
答案:不一樣。
- m維行向量:m維表示一行中有m列,由于是行向量,所以是1行m列
- n維列向量:n維表示一行中有n行,由于是列向量,所以是n行1列
- m維向量:看書的習慣了,一般書籍開頭會說此本書以列向量為基準,那m維向量就指m維列向量了
- 矩陣:一般不說是幾維,矩陣的表示一般就是dm。所以m維行向量也可以看作是m1的矩陣,n維列向量也可以看作是1*n的矩陣
但在numpy中維度概念不一樣啊。numpy中維度就是平常所說的一維(只有x軸)、二維(x、y軸)、三維(x、y、z軸)…numpy中如何表示零維標量、一維、二維、三維等等?
標量print后只有一個數字 一維print后有一對花括號 [ ] 二維print后有兩對 [ [] ] 三維print后有三對[ [ [] ] ] 依次類推…
注意,list里面也是同樣規(guī)則。但list 和 ndarray背后有很大區(qū)別
import numpy as np a = 12 print("a : ", a) print("shape of a : ", np.shape(a)) #標量,0維 print("type of a : ",type(a)) print("_____________________________________") b = np.array(12) print("b : ", b) print("shape of b : ", np.shape(b)) #標量,0維 print("type of b : ",type(b)) print("_____________________________________") c = [12] print("c : ", c) print("shape of c : ", np.shape(c)) #一維向量 print("type of c : ",type(c)) print("_____________________________________") d = np.array([12]) print("d : ", d) print("shape of d : ", np.shape(d)) #一維向量 print("type of d : ",type(d)) print("_____________________________________") e = np.array([12,13]) print("e : ", e) print("shape of e : ", np.shape(e)) #一維向量 print("type of e : ",type(e)) print("_____________________________________") f = np.arange(0, 20) print("f : ", f) print("shape of f : ", np.shape(f)) #一維向量 print("type of f : ",type(f)) print("_____________________________________") g = np.array([[11],[12]]) print("g : ", g) print("shape of g : ", np.shape(g)) #二維向量 print("type of g: ",type(g))
輸出:
a : 12 shape of a : () type of a : <class ‘int’>
b : 12 shape of b : () type of b : <class ‘numpy.ndarray’>
c : [12] shape of c : (1,) type of c : <class ‘list’>
d : [12] shape of d : (1,) type of d : <class ‘numpy.ndarray’>
e : [12 13] shape of e : (2,) type of e : <class ‘numpy.ndarray’>
f : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] shape of f : (20,) type of f : <class ‘numpy.ndarray’>
g : [[11] [12]] shape of g : (2, 1) type of g: <class ‘numpy.ndarray’>
2、shape又是什么?
形狀唄。用 .shape 屬性就能查看,或者 np.shape() 函數 也行。
- () 表示零維。
- (20,) 表示1維。區(qū)別(20),(20)python里等價于20。
- (2,3) 表示2維。也就是所謂的矩陣了。線代里喜歡用 2*3 表示
- (1,2,3) 表示3維。維度從右到左依次增高。
注意:
列向量或行向量即可看作一維,又可看作二維。例如[1,1,1]: 1、(3,) 即一維 2、(1,3)即二維,1*3的矩陣
3、一維和二維初始化
常使用一維和二維,所以說一下初始化以及背后的轉化。
3.1、初始化
import numpy as np import pandas # 1、np.array()初始化,傳入一個list即可。 a1 = np.array(12) #0維,() a2 = np.array([12]) #1維,(1,) a3 = np.array([12,13]) #1維,(2,) a4 = np.array([[12,13], [14,15]]) #2維,(2,2) # 此函數在pandas.read_csv()導入數據集后得用 # df = pandas.read_csv('temperature_dataset.csv') # data = np.array(df) # 2、np.arange()初始化,只能初始化1維。傳入start、stop、步長 b1 = np.arange(3, 7, 0.5) #1維 # 3、np.linspace()初始化,也是只能初始化1維 c1 = np.linspace(3, 7, num=5) #1維 # 4、np.zeros()初始化,傳入shape。創(chuàng)建一個全是0的數組 d1 = np.zeros(5) #1維 print(d1) d2 = np.zeros((5,)) #1維,和上面等價 print(d2) d4 = np.zeros(2,3) #報錯?。?! print(d4) d3 = np.zeros((2,3)) #2維 print(d3)
3.2、常用的行向量、列向量初始化
上面已經說了,行列向量既可以看作是一維,也可以看作是二維。所以都行兩個不同維度初始化都行,不用糾結。
只是按照不同維度創(chuàng)建后需要很清楚背后的運算。 例如np.dot() 點積運算,如果不清楚自己當初設的是一維還是二維,那很有可能報錯。
我習慣用二維表示。
因為在numpy里一維既可以做行向量也可以做列向量,那對于任意一個給定的一維向量,我們就無法確定他到底是行向量還是列向量,為了防止這種尷尬的境地,習慣上用二維矩陣而不是一維矩陣來表示行向量和列向量,因為二維必定能夠確定他是行向量還是列向量。
3.3、高維如何降到低維?
最常用的就是二維如何轉化成一維。
- np.reshape()函數
- np.squeeze()函數
- np.flatten()函數
- np.ravel()函數
4、為啥搞清維數那么重要?
原因如下:
1、劃分數據集就很重要
2、很多公式的推導對應背后的代碼,常常就是由于維度不匹配報錯,
3、是用matlibplot畫圖時本該傳一維當作x、y,但誤傳了二維導致無法正常畫圖
4、就是廣播操作時也需要清楚維度,否則可能因為不滿足廣播的維度而報錯。
到此這篇關于Numpy維度知識總結的文章就介紹到這了,更多相關Numpy維度內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python如何使用logging為Flask增加logid
這篇文章主要介紹了Python如何使用logging為Flask增加logid,幫助大家更好的理解和學習使用python,感興趣的朋友可以了解下2021-03-03