1、numpy.genfromtxt讀取txt文件
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str)
#上面一句話是:實例化一個numpy.genfromtxt的對象,第一參數傳要讀取的文件名,第二個是分割符,最后一個讀取后的數據類型。這是用numpy.genfromtxt讀取txt文件內容的方法。
print(type(world_alcohol))
print(world_alcohol)
#print(help(numpy.genfromtxt))
輸出為:
<class 'numpy.ndarray'>
[['Year' 'WHO region' 'Country' 'Beverage Types' 'Display Value']
['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
['1986' 'Americas' 'Uruguay' 'Other' '0.5']
...,
['1987' 'Africa' 'Malawi' 'Other' '0.75']
['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
2、numpy.array建立矩陣
vector = numpy.array([5, 10, 15, 20]) #使用numpy.array方法把一個list轉化為一個向量或者叫一行矩陣。
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]]) #使用numpy.array方法把多個list轉化為一個矩陣。
print(vector)
print(matrix)
所以寫一維矩陣寫一個中括號,寫二維矩陣寫二個中括號,寫三維矩陣寫三個中括號。
輸出:
[ 5 10 15 20]
[[ 5 10 15]
[20 25 30]
[35 40 45]]
3、shape方法打印矩陣形狀
vector = numpy.array([5, 10, 15, 20])
print(vector.shape) #vector.shape就是打印該矩陣的形狀,即幾維,幾行幾列。
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print(matrix.shape) #matrix.shape就是打印該矩陣的形狀,即幾維,幾行幾列。
一般XXX.shape用作調試程序用。
輸出:
(4,)
(3, 3)
4、dtpye方法打印矩陣元素類型
當構建numpy矩陣時,要求里面的所有元素的數據類型為相同的數據類型。
numbers = numpy.array([1, 2, 3, 4])
print(numbers)
print(numbers.dtype)
輸出:
[1 2 3 4]
int32
當上述代碼修改成下面時:
numbers = numpy.array([1, 2, 3, 4.0])
print(numbers)
print(numbers.dtype)
輸出:
[ 1. 2. 3. 4.]
float64
5、numpy索引
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1)
print(world_alcohol)
uruguay_other_1986 = world_alcohol[1,4] #我們需要取的數據是第2行第5個,同list第一個數據索引值為0
third_country = world_alcohol[2,2] #這行我們需要取的數據是第3行第3個。
print(uruguay_other_1986)
print(third_country)
輸出:
0.5
Cte d'Ivoire
6、numpy切片
1)向量的切片
vector = numpy.array([5, 10, 15, 20]) #建立一個向量
print(vector[0:3]) #向量與list的切片方式一致,顧頭不顧尾。
輸出:
[ 5 10 15]
2)矩陣的單列或單行切片
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
]) #建立矩陣
print(matrix[:,1]) # :表示這一行或一列的所有元素。以“,”為間隔,隔開行和列的位置。如果在第一個位置為行,第二個位置為列。
輸出:
[10 25 40]
3)矩陣的多列或多行切片
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,0:2]) #以","為間隔,隔開行和列的位置。":"表示所有元素。
輸出:
[[ 5 10]
[20 25]
[35 40]]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[1:3,0:2])
7、對numpy.array整體的操作
對numpy.array整體的操作=對numpy.array每一個元素做相同的操作。
vector = numpy.array([5, 10, 15, 20])
print(vector == 10)
output:
[False True False False]
這一條定律對矩陣一樣適用:
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix == 25)
output:
[[False False False]
[False True False]
[False False False]]
8、bool值也可以當索引
把某個矩陣的對應布爾值矩陣作為索引傳遞給原矩陣,則會返回出bool值為真的元素
vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print(vector[equal_to_ten])
output:
[False True False False]
[10]
上述規律對矩陣一樣適用:
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
second_column_25 = (matrix[:,1] == 25)
print(second_column_25)
print(matrix[second_column_25, :])
output:
[False True False] [[20 25 30]]
9、bool值表的與、或
1)bool值表的與
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print(equal_to_ten_and_five)
output:
[False False False False]
2)bool值表的或
和上面代碼類似,此處略
10、矩陣data類型轉換
vector = numpy.array(["1", "2", "3"]) #元素為字符串類型
print(vector.dtype) #打印元素類型
print (vector)
vector = vector.astype(float) #用astype()方法進行強制類型轉換。
print (vector.dtype)
print (vector)
11、求矩陣極值等方法
vector = numpy.array([5, 10, 15, 20])
vector.min() #求矩陣中最小的元素
#print(help(numpy.array)) #想了解更多的方法或函數,打印相關幫助即可。
12、矩陣求和
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=1) #按行求和
output:array([ 30, 75, 120])
matrix.sum(axis=0) #按列求和
output:array([60, 75, 90])
指數計算和根號計算
B = numpy.arange(3)
print (numpy.exp(B)) #exp是以e為底數,B的每個元素分別作為指數進行計算,計算結果以矩陣的方式顯示
print (numpy.sqrt(B)) #sqrt是對B的每個元素分別開根號,計算結果以矩陣的方式顯示
13、關於矩陣的操作
a = np.floor(10*np.random.random((3,4))) #np.random.random((3,4))是-1到1之間的隨機,*10讓數據擴大10倍便於分辨,floor方法是向下取整又叫舍尾法近似。
print(a.ravel()) #a.ravel() 方法是把一個二維矩陣拉伸成一個1維向量,順序是先第1行從左到右,然后第2行從左到右,以此類推
a.shape = (6, 2) #把矩陣a的形狀設置為6行2列
print(a.T) #把a矩陣轉置,即把原來的第一列變為新矩陣的第一行,第二列變為新矩陣的第二行,即原來的行變為列,原來的列變為行,依次類推。
a.reshape(3,-1) #reshape(3,-1)方法是修改矩陣的形狀,3是3行,-1這個數表示讓計算機根據其他維度的數據,自動算出-1這個地方應該填寫多少,並完成修改。
矩陣的拼接:
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(np.hstack((a,b))) # hstack方法是矩陣橫向拼接方法
print(np.vstack((a,b))) # vstack方法是矩陣縱向拼接方法
矩陣的切分:
a = np.floor(10*np.random.random((2,12)))
print (np.hsplit(a,3)) # hsplit函數是橫向切割,傳入兩個參數,第一個參數是要被切割的矩陣,第二個是切成幾份。
print (np.hsplit(a,(3,4))) # hsplit還有一種用法,指定在某幾個位置切。該例子中,第二個位置參數是一個元組形式,表示在位置3,4分別切一刀
a = np.floor(10*np.random.random((12,2)))
np.vsplit(a,3) # vsplit函數是縱向切割,傳入兩個參數,第一個參數是要被切割的矩陣,第二個是切成幾份。
14、關於復制的操作
1)賦值符號:指向一樣,數據共享
a = np.arange(12) #建立一個12個元素的向量,命名為a
b = a
print(b is a) #打印判斷b是不是a的結果
b.shape = 3,4 #把一維向量b的形狀轉化成3行4列的而二維矩陣
print a.shape #打印a的形狀
print id(a) #打印a的id,id是某個變量在內存中生成時,被賦予的具有唯一性的內存標識
print id(b) #打印b的id
output:
True
(3, 4)
82691200
82691200
由上述例子得出,a和b只是同一個矩陣的不同名稱,他們倆指向的是同一個矩陣,所以不論對a還是b操作,都會引起被指向矩陣的變化。
2)淺復制:指向不同,數據共享
c = a.view() # 矩陣的view方法是淺復制,即c和a指向不同,但又同時共享着數據
print(c is a) #打印c是不是a的結果
c.shape = 2,6
print a.shape
c[0,4] = 1234 #把矩陣c第1行第5列元素賦值為1234
print (a)
output:
False
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
由此得出,矩陣的view方法是淺復制,即c和a指向不同,但又同時共享着數據。
3)深復制:指向不同,數據不同
d = a.copy() # 矩陣的copy方法是深復制,即d和a指向不同,數據不同
print(d is a)
d[0,0] = 9999
print d
print a
output:
False
[[9999 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
15、矩陣的排序和索引
1)最值:
data = np.sin(np.arange(20)).reshape(5,4) #隨機創建一個矩陣
ind = data.argmax(axis=0) #矩陣的argmax方法是求每列或每行的最大值,axis=0這個參數指按列統計,axis=1是按行統計。
print ind
#data_max = data[ind, xrange(data.shape[1])] #把每列最大的元素取出來。
print data_max
2)擴展
a = np.arange(0, 40, 10) #隨機建立一個矩陣
b = np.tile(a, (3, 5)) # tile方法是矩陣拓展方法,第一參數是把a作為整體當做一個元素進行擴展,第二個參數是擴展成3行5列的矩陣。
print b
3)排序
a = np.array([[4, 3, 5], [1, 2, 1]])
#b = np.sort(a, axis=1) #按照行對矩陣a排序,默認是從小到大排序,把新矩陣賦值給b
#a.sort(axis=1) # 這種調用方法和np.sort(a,axis=1)效果一樣
a = np.array([4, 3, 1, 2])
j = np.argsort(a) # argsort方法是先對矩陣a排序,然后對應求出每個元素在原來矩陣a中的索引
print j
print a[j] #把這個索引傳入原矩陣就可以得到排序后的新矩陣。
output:
[2 3 1 0]
[1 2 3 4]
16、對numpy庫的總結
整體來看numpy庫已經包含了對矩陣的基本處理方式,有替代matlab的傾向。想比matlab,numpy庫的優勢還是很明顯的。不過單純學完numpy庫,我還是沒辦法把它和程序化交易結合起來。不過學完這個庫,以后就不用安裝巨大的matlab 2012R 這個軟件了。