機器學習一

tile函數    

    在看機器學習實戰這本書時,遇到numpy.tile(A,B)函數,愣是沒看懂怎麼回事,裝了numpy模塊後,實驗了幾把,原來是這樣子:

重複A,B次,這裏的B可以時int類型也可以是元組類型。

  1. >>> import numpy  
  2. >>> numpy.tile([0,0],5)#在列方向上重複[0,0]5次,默認行1次  
  3. array([0000000000])  
  4. >>> numpy.tile([0,0],(1,1))#在列方向上重複[0,0]1次,行1次  
  5. array([[00]])  
  6. >>> numpy.tile([0,0],(2,1))#在列方向上重複[0,0]1次,行2次  
  7. array([[00],  
  8.        [00]])  
  9. >>> numpy.tile([0,0],(3,1))  
  10. array([[00],  
  11.        [00],  
  12.        [00]])  
  13. >>> numpy.tile([0,0],(1,3))#在列方向上重複[0,0]3次,行1次  
  14. array([[000000]])  
  15. >>> numpy.tile([0,0],(2,3))<span style="font-family: Arial, Helvetica, sans-serif;">#在列方向上重複[0,0]3次,行2次</span>  
  16. array([[000000],  
  17.        [000000]])  

zeros函數

zeros:創建3維數組,數值爲0

  1. >>> np.zeros((3,4,5))#  
  2. array([[[ 0.,  0.,  0.,  0.,  0.],  
  3.         [ 0.,  0.,  0.,  0.,  0.],  
  4.         [ 0.,  0.,  0.,  0.,  0.],  
  5.         [ 0.,  0.,  0.,  0.,  0.]],  
  6.   
  7.        [[ 0.,  0.,  0.,  0.,  0.],  
  8.         [ 0.,  0.,  0.,  0.,  0.],  
  9.         [ 0.,  0.,  0.,  0.,  0.],  
  10.         [ 0.,  0.,  0.,  0.,  0.]],  
  11.   
  12.        [[ 0.,  0.,  0.,  0.,  0.],  
  13.         [ 0.,  0.,  0.,  0.,  0.],  
  14.         [ 0.,  0.,  0.,  0.,  0.],  
  15.         [ 0.,  0.,  0.,  0.,  0.]]])  


多維數組與矩陣的轉換

  1. >>> arr = np.zeros((1,4,5))#記得維度必須匹配(3,3,3)轉換失敗  
  2. >>> mat = np.matrix(arr)  

數組創建和數據類型

  1. #!/usr/bin/env python  
  2. #-*-encoding:utf-8-*-  
  3. import numpy as np  
  4. arr = np.arange(10)#創建擁有10個元素的數組  
  5. larr = arr.tolist()#轉換list  
  6. arr = np.zeros((1,3,3))#創建n維數組  
  7. mat = np.matrix(arr)#將數組轉換爲矩陣,矩陣爲3*3  
  8. alist = [123]  
  9. np.array(alist)#使用List創建數組  
  10. #創建  
  11. arr = np.arange(100)  
  12. arr = np.arange(10,100)#創建包含10~99數組  
  13. arr = np.linspace(12100)#創建包含100個取值範圍在1~2之間的數組  
  14. arr = np.logspace(01100, base=10)#返回包含100個取值範圍在10+[0~1]之間的數組  
  15. cube = np.zeros((5,5,5)).astype(int) + 1 #使用astype設置數據類型  
  16. cube = np.ones((555)).astype(np.float32)#創建3維數組,元素爲1  
  17. #通過指定數據類型創建n維數組數組  
  18. arr = np.zeros(2, dtype=int)  
  19. arr = np.zeros(2, dtype=np.float32)  
  20. arr1d = np.arange(1000)#一維數組  
  21. arr3d = arr1d.reshape((10,10,10))#轉換爲3維數組  
  22. arr3d = np.reshape(arr1d, (101010))  
  23.   
  24. arr4d = np.zeros((10101010))  
  25. arr1d = arr4d.ravel()#將4維數組轉換爲1維數組  
  26.   
  27. recarr = np.zeros((2,), dtype=('i4,f4,a10'))#指定n維數組中每列的數據類型,2*3  
  28. col1 = np.arange(2) + 1  
  29. col2 = np.arange(2, dtype=np.float32)  
  30. col3 = ['Hello''World']  
  31. recarr[:]=zip(col1,col2,col3)#按列方式組裝  
  32. #爲每列命名  
  33. recarr.dtype.names = ('Integers' , 'Floats''Strings')<span style="font-size:14px;">  
  34. </span>  

索引和切片

  1. arr[0,1]#訪問單個元?  
  2. arr[:,1]#訪問第2列    
  3. arr[1,:]#訪問第2行    


  1. arr = np.arange(7)  
  2. index = np.where(arr > 2)#查找>2的索引  
  3. new_arr = np.delete(arr, index)#刪除index對應的元素  

  1. index = arr > 2 #返回哪些元素>2,[TRUE,FALSE,...]  

布爾表達式和數組

  1. img1 = np.zeros((2020)) + 3  
  2.   
  3. img1[4:-44:-4] = 6  
  4. img1[7:-77:-7] = 9  
  5. index1 = img1 > 2  
  6. index2 = img1 < 6  
  7. compound_index = index1 & index2 #exp1  
  8. compound_index = (img1 > 3) & (img1 < 7#與expr1含義一樣  
  9. img2 = np.copy(img1[compound_index])  
  10. print img2  
  11.   
  12. index3 = img1 == 9  
  13. index4 = (index1 & index2) | index3  

  1. import numpy.random as rand  
  2.   
  3. a = rand.random(100)#生成一個容納100個隨機數的數組  
  4. print a  

序列化和反序列化

  1. #預定義數據欄位名稱和類型  
  2. table = np.loadtxt('example.txt',dtype='names': ('ID''Result''Type'),\  
  3.     'formats': ('S4''f4''i2'))  
  4. np.savetxt('somenewfile.txt')#序列化  
  5. #二進制文件加載,保存  
  6. data = np.empty((10001000))  
  7. np.save('test.npy', data)  
  8. np.savez('test.npz', data)#採用壓縮  
  9. newdata = np.load('test.npy')     

Max和Min函數

  1. mat.max(0)#n維數組axis=0維度的最小值,最大值  
  2. mat.min(0)#  
發佈了30 篇原創文章 · 獲贊 7 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章