Numpy維度知識(shí)總結(jié)
1、維度究竟是行數(shù)還是列數(shù)?
機(jī)器學(xué)習(xí)時(shí)總聽到m維行向量、n維列向量。究竟維度是啥?這里的維度和numpy中一樣嘛?
答案:不一樣。
- m維行向量:m維表示一行中有m列,由于是行向量,所以是1行m列
- n維列向量:n維表示一行中有n行,由于是列向量,所以是n行1列
- m維向量:看書的習(xí)慣了,一般書籍開頭會(huì)說此本書以列向量為基準(zhǔn),那m維向量就指m維列向量了
- 矩陣:一般不說是幾維,矩陣的表示一般就是dm。所以m維行向量也可以看作是m1的矩陣,n維列向量也可以看作是1*n的矩陣
但在numpy中維度概念不一樣啊。numpy中維度就是平常所說的一維(只有x軸)、二維(x、y軸)、三維(x、y、z軸)…numpy中如何表示零維標(biāo)量、一維、二維、三維等等?
標(biāo)量print后只有一個(gè)數(shù)字 一維print后有一對(duì)花括號(hào) [ ] 二維print后有兩對(duì) [ [] ] 三維print后有三對(duì)[ [ [] ] ] 依次類推…
注意,list里面也是同樣規(guī)則。但list 和 ndarray背后有很大區(qū)別
import numpy as np a = 12 print("a : ", a) print("shape of a : ", np.shape(a)) #標(biāo)量,0維 print("type of a : ",type(a)) print("_____________________________________") b = np.array(12) print("b : ", b) print("shape of b : ", np.shape(b)) #標(biāo)量,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() 函數(shù) 也行。
- () 表示零維。
- (20,) 表示1維。區(qū)別(20),(20)python里等價(jià)于20。
- (2,3) 表示2維。也就是所謂的矩陣了。線代里喜歡用 2*3 表示
- (1,2,3) 表示3維。維度從右到左依次增高。
注意:
列向量或行向量即可看作一維,又可看作二維。例如[1,1,1]: 1、(3,) 即一維 2、(1,3)即二維,1*3的矩陣
3、一維和二維初始化
常使用一維和二維,所以說一下初始化以及背后的轉(zhuǎn)化。
3.1、初始化
import numpy as np import pandas # 1、np.array()初始化,傳入一個(gè)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) # 此函數(shù)在pandas.read_csv()導(dǎo)入數(shù)據(jù)集后得用 # 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)建一個(gè)全是0的數(shù)組 d1 = np.zeros(5) #1維 print(d1) d2 = np.zeros((5,)) #1維,和上面等價(jià) print(d2) d4 = np.zeros(2,3) #報(bào)錯(cuò)!??! print(d4) d3 = np.zeros((2,3)) #2維 print(d3)
3.2、常用的行向量、列向量初始化
上面已經(jīng)說了,行列向量既可以看作是一維,也可以看作是二維。所以都行兩個(gè)不同維度初始化都行,不用糾結(jié)。
只是按照不同維度創(chuàng)建后需要很清楚背后的運(yùn)算。 例如np.dot() 點(diǎn)積運(yùn)算,如果不清楚自己當(dāng)初設(shè)的是一維還是二維,那很有可能報(bào)錯(cuò)。
我習(xí)慣用二維表示。
因?yàn)樵趎umpy里一維既可以做行向量也可以做列向量,那對(duì)于任意一個(gè)給定的一維向量,我們就無法確定他到底是行向量還是列向量,為了防止這種尷尬的境地,習(xí)慣上用二維矩陣而不是一維矩陣來表示行向量和列向量,因?yàn)槎S必定能夠確定他是行向量還是列向量。
3.3、高維如何降到低維?
最常用的就是二維如何轉(zhuǎn)化成一維。
- np.reshape()函數(shù)
- np.squeeze()函數(shù)
- np.flatten()函數(shù)
- np.ravel()函數(shù)
4、為啥搞清維數(shù)那么重要?
原因如下:
1、劃分?jǐn)?shù)據(jù)集就很重要
2、很多公式的推導(dǎo)對(duì)應(yīng)背后的代碼,常常就是由于維度不匹配報(bào)錯(cuò),
3、是用matlibplot畫圖時(shí)本該傳一維當(dāng)作x、y,但誤傳了二維導(dǎo)致無法正常畫圖
4、就是廣播操作時(shí)也需要清楚維度,否則可能因?yàn)椴粷M足廣播的維度而報(bào)錯(cuò)。
到此這篇關(guān)于Numpy維度知識(shí)總結(jié)的文章就介紹到這了,更多相關(guān)Numpy維度內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python如何使用logging為Flask增加logid
這篇文章主要介紹了Python如何使用logging為Flask增加logid,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Python列表pop()函數(shù)使用實(shí)例詳解
這篇文章主要介紹了Python列表pop()函數(shù)使用實(shí)例詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式
這篇文章主要介紹了pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12