這裡介紹python的一個庫,numpy庫,這個庫是機器學習,資料分析最經常用到的庫之一,也是利用python做資料必須用到的一個庫,入門機器學習學的第一個python庫就是它了。
先對其導入到python中,相關代碼如下:
import numpy
1.首先利用這個庫建立一個一維數組:
a=np.arange(10)
print(a)
輸出結果如下:
[0 1 2 3 4 5 6 7 8 9]
2.建立一個布爾型的數組:
a=np.full((3,3),True,dtype=bool)
print(a)
結果如下:
[[ True True True]
[ True True True]
[ True True True]]
3.從一維數組從提取滿足條件的元素,比方說提取出奇數的元素:
import numpy as np
a=np.arange(10)
print(a[a%2==1])
結果如下:
[1 3 5 7 9]
4.将數組中的所有奇數替換成-1而不影響原始的數組:
import numpy as np
a=np.arange(10)
print(a[a%2==1])
out=np.where(a%2==1,-1,a)
print(out)
print(a)
結果如下:
[1 3 5 7 9]
[ 0 -1 2 -1 4 -1 6 -1 8 -1]
[0 1 2 3 4 5 6 7 8 9]
5.将一行的數組轉化為兩行的數組:
import numpy as np
a=np.arange(10)
print(a)
b=a.reshape(2,-1)
print(b)
結果如下:
[0 1 2 3 4 5 6 7 8 9]
[[0 1 2 3 4]
[5 6 7 8 9]]
6.組合兩個不同的數組,垂直堆疊數組:
import numpy as np
a=np.arange(10).reshape(2,-1)
print(a)
b=np.ones(10).reshape(2,-1)
print(b)
c=np.concatenate((a,b),axis=0)
print(c)
結果如下:
[[0 1 2 3 4]
[5 6 7 8 9]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[0. 1. 2. 3. 4.]
[5. 6. 7. 8. 9.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
7.采用另外的一種方法水準堆疊兩個不同的數組:
import numpy as np
a=np.arange(10).reshape(2,-1)
print(a)
b=np.ones(10).reshape(2,-1)
print(b)
c=np.hstack((a,b))
print(c)
#垂直堆疊的話将hstack改為vstack
結果如下:
[[0 1 2 3 4]
[5 6 7 8 9]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[0. 1. 2. 3. 4. 1. 1. 1. 1. 1.]
[5. 6. 7. 8. 9. 1. 1. 1. 1. 1.]]
8.如何獲得兩個數組之間的共同元素:
import numpy as np
a=np.array([0,1,2,3,4,5,6,7,8,9]).reshape(2,-1)
b=np.array([7,2,3,8,6,5,4,0,8,8]).reshape(2,-1)
c=np.intersect1d(a,b)
print(c)
結果:
[0 2 3 4 5 6 7 8]
9.如何交換二維數組中的兩個列,比方說第一和第二列:
import numpy as np
a=np.array([0,1,2,3,4,5,6,7,8]).reshape(-1,3)
print(a)
b=a[:,[1,0,2]]
print(b)
結果:
[[0 1 2]
[3 4 5]
[6 7 8]]
[[1 0 2]
[4 3 5]
[7 6 8]]
10.建立一個随機數組:
import numpy as np
a=np.random.random(9).reshape(3,3)
print(a)
結果:
[[0.35743078 0.45562558 0.47565199]
[0.21876288 0.7162558 0.88601442]
[0.87192744 0.2853304 0.48398916]]
11.列印數組并且保留3位小數:
import numpy as np
a=np.random.random(9).reshape(3,3)
print(a)
np.set_printoptions(precision=3)
print(a)
結果:
[[0.61684015 0.01555676 0.59569796]
[0.74603776 0.04927135 0.98439895]
[0.38879249 0.71765352 0.24739962]]
[[0.617 0.016 0.596]
[0.746 0.049 0.984]
[0.389 0.718 0.247]]
12.求數組的平均值,中位數和标準差:
import numpy as np
a=np.random.random(9).reshape(3,3)
print(a)
mean,mid,std=np.mean(a),np.median(a),np.std(a)
print(mean,mid,std)
結果:
[[0.69353914 0.41381197 0.08503532]
[0.59348658 0.72611527 0.93285931]
[0.11307856 0.01634739 0.71822684]]
0.476944488122813 /t 0.593486582461256 0.3146044225752866
這裡順便提一下其他的函數,比方絕對值函數abs,平方函數square,四舍五入函數rint,方差var,求和平均值就不說了。。
13.多元數組的切片:
import numpy as np
a=np.arange(24).reshape((2,3,4))
print(a)
print()
#選取一個次元的
b=a[:,1,-3]
print(b)
print()
c=a[:,1:3,:]
print(c)
print()
#利用步長跳躍切片
d=a[:,:,::2]
print(d)
結果如下:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
[ 5 17]
[[[ 4 5 6 7]
[ 8 9 10 11]]
[[16 17 18 19]
[20 21 22 23]]]
[[[ 0 2]
[ 4 6]
[ 8 10]]
[[12 14]
[16 18]
[20 22]]]
暫時第一篇部落格就寫到這,因為自己是菜雞中的菜雞,是以如果以上的代碼或者結果有錯誤的話,也很正常,歡迎指正指導!