tile函數
在看機器學習實戰這本書時,遇到numpy.tile(A,B)函數,愣是沒看懂怎麼回事,裝了numpy模塊後,實驗了幾把,原來是這樣子:
重複A,B次,這裏的B可以時int類型也可以是元組類型。
- >>> import numpy
- >>> numpy.tile([0,0],5)#在列方向上重複[0,0]5次,默認行1次
- array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
- >>> numpy.tile([0,0],(1,1))#在列方向上重複[0,0]1次,行1次
- array([[0, 0]])
- >>> numpy.tile([0,0],(2,1))#在列方向上重複[0,0]1次,行2次
- array([[0, 0],
- [0, 0]])
- >>> numpy.tile([0,0],(3,1))
- array([[0, 0],
- [0, 0],
- [0, 0]])
- >>> numpy.tile([0,0],(1,3))#在列方向上重複[0,0]3次,行1次
- array([[0, 0, 0, 0, 0, 0]])
- >>> numpy.tile([0,0],(2,3))<span style="font-family: Arial, Helvetica, sans-serif;">#在列方向上重複[0,0]3次,行2次</span>
- array([[0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0]])
zeros函數
zeros:創建3維數組,數值爲0
- >>> np.zeros((3,4,5))#
- array([[[ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.]],
- [[ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.]],
- [[ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.],
- [ 0., 0., 0., 0., 0.]]])
多維數組與矩陣的轉換
- >>> arr = np.zeros((1,4,5))#記得維度必須匹配(3,3,3)轉換失敗
- >>> mat = np.matrix(arr)
數組創建和數據類型
- #!/usr/bin/env python
- #-*-encoding:utf-8-*-
- import numpy as np
- arr = np.arange(10)#創建擁有10個元素的數組
- larr = arr.tolist()#轉換list
- arr = np.zeros((1,3,3))#創建n維數組
- mat = np.matrix(arr)#將數組轉換爲矩陣,矩陣爲3*3
- alist = [1, 2, 3]
- np.array(alist)#使用List創建數組
- #創建
- arr = np.arange(100)
- arr = np.arange(10,100)#創建包含10~99數組
- arr = np.linspace(1, 2, 100)#創建包含100個取值範圍在1~2之間的數組
- arr = np.logspace(0, 1, 100, base=10)#返回包含100個取值範圍在10+[0~1]之間的數組
- cube = np.zeros((5,5,5)).astype(int) + 1 #使用astype設置數據類型
- cube = np.ones((5, 5, 5)).astype(np.float32)#創建3維數組,元素爲1
- #通過指定數據類型創建n維數組數組
- arr = np.zeros(2, dtype=int)
- arr = np.zeros(2, dtype=np.float32)
- arr1d = np.arange(1000)#一維數組
- arr3d = arr1d.reshape((10,10,10))#轉換爲3維數組
- arr3d = np.reshape(arr1d, (10, 10, 10))
- arr4d = np.zeros((10, 10, 10, 10))
- arr1d = arr4d.ravel()#將4維數組轉換爲1維數組
- recarr = np.zeros((2,), dtype=('i4,f4,a10'))#指定n維數組中每列的數據類型,2*3
- col1 = np.arange(2) + 1
- col2 = np.arange(2, dtype=np.float32)
- col3 = ['Hello', 'World']
- recarr[:]=zip(col1,col2,col3)#按列方式組裝
- #爲每列命名
- recarr.dtype.names = ('Integers' , 'Floats', 'Strings')<span style="font-size:14px;">
- </span>
索引和切片
- arr[0,1]#訪問單個元?
- arr[:,1]#訪問第2列
- arr[1,:]#訪問第2行
- arr = np.arange(7)
- index = np.where(arr > 2)#查找>2的索引
- new_arr = np.delete(arr, index)#刪除index對應的元素
- index = arr > 2 #返回哪些元素>2,[TRUE,FALSE,...]
布爾表達式和數組
- img1 = np.zeros((20, 20)) + 3
- img1[4:-4, 4:-4] = 6
- img1[7:-7, 7:-7] = 9
- index1 = img1 > 2
- index2 = img1 < 6
- compound_index = index1 & index2 #exp1
- compound_index = (img1 > 3) & (img1 < 7) #與expr1含義一樣
- img2 = np.copy(img1[compound_index])
- print img2
- index3 = img1 == 9
- index4 = (index1 & index2) | index3
- import numpy.random as rand
- a = rand.random(100)#生成一個容納100個隨機數的數組
- print a
序列化和反序列化
- #預定義數據欄位名稱和類型
- table = np.loadtxt('example.txt',dtype='names': ('ID', 'Result', 'Type'),\
- 'formats': ('S4', 'f4', 'i2'))
- np.savetxt('somenewfile.txt')#序列化
- #二進制文件加載,保存
- data = np.empty((1000, 1000))
- np.save('test.npy', data)
- np.savez('test.npz', data)#採用壓縮
- newdata = np.load('test.npy')
Max和Min函數
- mat.max(0)#n維數組axis=0維度的最小值,最大值
- mat.min(0)#