01、python数据分析与机器学习实战——Python科学计算库-Numpy
深度學習——學習目錄
NumPy介紹
NumPy系統是Python的一種開源的數值計算擴展。
這種工具可用來存儲和處理大型矩陣,
比Python自身的嵌套列表(nested list structure)結構要高效的多
(該結構也可以用來表示矩陣(matrix))。
numpy基礎
矩陣運算
矩陣和數的計算:
加法四則運算:
一個矩陣與一個數字進行四則運算就是把矩陣中的每一個元素都與這個數字進行四則運算:
這是一個numpy的廣播機制造成的,在運算過程中,四則運算的值被廣播到所有的元素上面。
假設有下面這么一個txt文件,要用numpy打開:
Year,WHO region,Country,Beverage Types,Display Value 1986,Western Pacific,Viet Nam,Wine,0 1986,Americas,Uruguay,Other,0.5 1985,Africa,Cte d'Ivoire,Wine,1.62 1986,Americas,Colombia,Beer,4.27 1987,Americas,Saint Kitts and Nevis,Beer,1.98 1987,Americas,Guatemala,Other,0 1987,Africa,Mauritius,Wine,0.13 1985,Africa,Angola,Spirits,0.39 1986,Americas,Antigua and Barbuda,Spirits,1.55 1984,Africa,Nigeria,Other,6.1 1987,Africa,Botswana,Wine,0.2 1989,Americas,Guatemala,Beer,0.62 1985,Western Pacific,Lao People's Democratic Republic,Beer,0 1984,Eastern Mediterranean,Afghanistan,Other,0 1985,Western Pacific,Viet Nam,Spirits,0.05 1987,Africa,Guinea-Bissau,Wine,0.07 1984,Americas,Costa Rica,Wine,0.06 1989,Africa,Seychelles,Beer,2.23 1984,Europe,Norway,Spirits,1.62 1984,Africa,Kenya,Beer,1.08 1986,South-East Asia,Myanmar,Wine,0 1989,Americas,Costa Rica,Spirits,4.51 1984,Europe,Romania,Spirits,2.67 1984,Europe,Turkey,Beer,0.44 1985,Africa,Comoros,Other, 1984,Eastern Mediterranean,Tunisia,Other,0 1985,Europe,United Kingdom of Great Britain and Northern Ireland,Wine,1.36 1984,Eastern Mediterranean,Bahrain,Beer,2.22 1987,Western Pacific,Viet Nam,Beer,0.11 1986,Europe,Italy,Other, 1986,Africa,Sierra Leone,Other,4.48 1986,Western Pacific,Micronesia (Federated States of),Wine,0 1989,Africa,Mauritius,Beer,1.6……
import numpyworld_alcohol=numpy.genfromtxt("world_alcohol.txt",delimiter=",",dtype=str) print(type(world_alcohol)) print(world_alcohol)首先導入numpy庫,然后用numpy.genfromtxt以“,”為分隔符將world_alcohol.txt文件導入;
然后用print(type(world_alcohol))打印world_alcohol的類型;
最后再將整個world_alcohol打印出來。
結果如下:
如果不知道某個方法的作用,可以打印help語句來獲取:
print(help(numpy.genfromtxt))它會打印出幫助注釋:
利用numpy的array可以創建矩陣:
import numpyvector=numpy.array([5,10,15,20]) matrix=numpy.array([[5,10,15,20],[20,25,30],[35,40,45]]) print(vector) print(matrix)
通過.shape方法可以打印矩陣的屬性:
第一行表示有四個元素,第二行表示這是一個2行3列的矩陣。通過.dtype方法可以打印元素的類型:
如果想要從矩陣中提取相應的元素,可以直接用索引,以打開的上述txt文件為例:
numpy同樣也提供了切片的操作:
import numpyvector=numpy.array([5,10,15,20]) print(vector[0:3]) matrix=numpy.array([ [5,10,15], [20,25,30], [35,40,45] ]) print(matrix[:,1]) print(matrix[:,0:2]) print(matrix[1:3,0:2])這里要注意的是:切片操作的時候可以用“:”代替一整行或者一整列以及從某行到某行和從某列到某列,還有,切片是不包含后面的參數內容的。
可以利用矩陣元素的值是否與給定的值相等打印bool值:
import numpyvector=numpy.array([5,10,15,20]) print(vector==10)matrix=numpy.array([ [5,10,15], [20,25,30], [35,40,45] ]) print(matrix==25)
然后可以將獲得的bool值作為索引帶回到矩陣中,同時判斷是否相等同樣支持邏輯運算:
可以利用.astype方法改變矩陣元素的類型:
import numpyvector=numpy.array(["1","2","3"]) print(vector.dtype) print(vector) vector=vector.astype(float) print(vector.dtype) print(vector)numpy也提供了求最值和求和的方法:
import numpyvector=numpy.array([5,10,15,20]) print(vector.min())matrix=numpy.array([ [5,10,15], [20,25,30], [35,40,45] ]) print(matrix.sum(axis=1)) print(matrix.sum(axis=0))sum中的參數:axis=1表示對行求和,axis=0表示對列求和
numpy也提供了一些很便利的方法:
np.arange(15)生成從0到14共15個數字:
將向量轉換為矩陣的操作:
分別打印矩陣的屬性、維度、元素類型名、元素數量:
print(a.shape) print(a.ndim) print(a.dtype.name) print(a.size) print(np.zeros((3,4))) print(np.ones((2,3,4),dtype=np.int32))創建一個元素全為0,2行3列的矩陣和元素全為1,3維3行4列的矩陣:
指定起點、終點和步長,打印向量:
生成一個2行3列的隨機數矩陣:
numpy中也提供了一些常量:
import numpy as np from numpy import piprint(np.linspace(0,2*pi,100))生成從0到2π的100個數:
開方運算和e的次方運算:
進行矩陣之間的四則運算和乘方運算、邏輯運算:
還有矩陣運算:
將矩陣轉換為向量:
然后再轉換成另一種屬性的矩陣,還可以轉置和自定義行列:
矩陣的橫豎拼接:
矩陣的橫向平均拆分和指定拆分:
矩陣的豎向拆分:
矩陣的幾種復制:
矩陣豎向找最大值索引并將其打印出來:
矩陣的成倍擴大:
矩陣的橫向排序、豎向排序和遞增索引排序:
總結
以上是生活随笔為你收集整理的01、python数据分析与机器学习实战——Python科学计算库-Numpy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《C champion》C语言的优点和缺
- 下一篇: 01、python数据分析与机器学习实战