import numpy
nfl = numpy.genfromtxt("nfl.csv", delimiter=",")
得到numpy.ndarray類型
但這樣的讀法有個(gè)問題罪治,numpy會(huì)試圖將strings轉(zhuǎn)為floats,轉(zhuǎn)不過來會(huì)變成nan医舆。為了讓讀出的數(shù)據(jù)是string格式拌汇,加上參數(shù)dtype="U75"蚯根。
import numpy as np
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.csv",dtype="U75",skip_header=1,delimiter=",")
print(world_alcohol)
axis=0/1如何選擇如下:
- 1 means that we want to perform the operation on each row
- 0 means on each column.
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=1)
得到:[30, 75, 120]
總結(jié):
1驹止、numpy是什么棵红?
- numpy庫核心是提供了一種多維數(shù)組對象叫ndarray凶赁。
- 矩陣matrix是numpy的特例,一個(gè)二維數(shù)組逆甜。
- arr=np.array(data)虱肄,得到ndarray型arr
2、numpy最大的劣勢是什么交煞?
- 數(shù)組中每個(gè)元素必須是相同的數(shù)據(jù)格式咏窿,都是string/float...
- 行和列必須用數(shù)字引用,沒有行頭/列頭的概念
3素征、numpy使用功能有哪些集嵌?
- 輕松轉(zhuǎn)化元素的格式:如arr=arr.astype(float64)
- 不用編寫循環(huán)萝挤,批量執(zhí)行數(shù)組對應(yīng)元素的操作,如arr*arr,arr-arr
- 各種索引和切片根欧,當(dāng)然任何修改會(huì)直接反映到源數(shù)據(jù)
A. 數(shù)字切片
第一個(gè)是行怜珍,第二個(gè)是列,如arr[1,:2]
B. 布爾型索引
如is_year=(arr[:,0]=='1990');arr[is_year,:]得到1990年對應(yīng)的行
matrix = np.array([[5, 10, 15],
[20, 25, 30],
[35, 40, 45]])
second_column_25 = (matrix[:,1] == 25)
print(second_column_25,matrix[second_column_25,:])
C. 花式索引:利用整數(shù)數(shù)組進(jìn)行索引
arr=np.arange(32).reshape((8,4))
arr
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]])
arr[[1,5,7,2],[0,3,1,2]]
array([ 4, 23, 29, 10])
arr[[1,5,7,2]]
array([[ 4, 5, 6, 7],
[20, 21, 22, 23],
[28, 29, 30, 31],
[ 8, 9, 10, 11]])
arr[[1,5,7,2]][:,[0,3,1,2]]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
arr[np.ix_([1,5,7,2],[0,3,1,2])]
array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
- numpy提供了各類函數(shù)凤粗,如abs,sqrt,dot,transpose,where,unique,cumsum,cumprod
arr=np.arange(3)
print(arr)
np.where(arr>0,2,-2)
[0 1 2]
array([-2, 2, 2])