1. 程式人生 > 實用技巧 >anaconda及jupyter notebook的使用之numpy模組的用法(2)

anaconda及jupyter notebook的使用之numpy模組的用法(2)

今日內容概要

numpy模組結束

  • ndarray建立

  • numpy內建方法

  • 索引與切片(花式索引、布林索引)

  • 常用函式

  • 統計方法

  • 隨機數

numpy的內建方法

import numpy as np
1.
# 1.ndarray的建立
np.array([1,2,3,4,5,6,7],ndmin=3)
array([[[1,2,3,4,5,6,7]])
2.
# 2.python中的range
# for i in range(10):
#      print(i)
np.arange(1,7)  # arange比python中的range更好用
array([1,2,3,4,5,6])
np.arange(1.0,8.0)
array([1.,2.,3.,4.,5.,6.,7.])
int是否包含最後一個數  
# 3.linspace
    np.linspace(1,10,num=20,retstep=True,endpoint=False) # retstep展示間隔數  endpoint是否包含最後一個數  
    (array([1.  , 1.45, 1.9 , 2.35, 2.8 , 3.25, 3.7 , 4.15, 4.6 , 5.05, 5.5 ,
        5.95, 6.4 , 6.85, 7.3 , 7.75, 8.2 , 8.65, 9.1 , 9.55]), 0.45)

# 4.zeros
np.zeros(10,dtype=int) # 預設都是float
res = np.zeros((3,5),dtype=int) # 預設都是二維
res
   array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]])
# 5.ones 用法跟zeros一致
np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])

# 6.empty
np.empty((3,3))  # 預設也是二維
array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000],
       [0.00000000e+000, 0.00000000e+000, 6.14617663e-321],
       [8.34448532e-308, 1.69105613e-306, 2.56765117e-312]])       
 
# 7.eye
np.eye(10,dtype=int)
array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]])       

ndarray跟標量運算

統一的規律都是
    陣列內的每一個元素都跟該元素(數字)做運算
 
陣列與陣列之間做運算
     兩個陣列的大小得一致
     運算的時候按照對應的位置計算

索引與切片

2 索引
# python中索引:從0開始的
# l = [111,222,333,444,555,666]
# l[1]
res = np.arrray([111,222,333,444,555,666,777])
res[2] # numpy中索引也是從0開始
333


res1 = np.array([[1,2,3,4],[5,6,7,8]])
res1
'''
在numpy索引的規律
              0  1  2  3  列索引
   行索引     [
       0     [1, 2, 3, 4],
       1     [5, 6, 7, 8] 
                ]
'''
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
面是列索引
# 求上述二維數字裡面的元素7
# res[1][2] # 寫法1
res1[1,2]  # 寫法2  逗號前面是行索引,逗號後面是列索引
7
3   切片
'''
切片取值
l1 = [11,22,33,44,55,66,77,88]
l1[1:3]
​
numpy切片操作跟python中的切片操作一致
'''
res2 = np.array([11,22,33,44,55,66,77])
res2[1:6]
array([22, 33, 44, 55, 66])
res3 = np.array([[1,2,3,4],[5,6,7,8],[9,11,22,33]])
res3
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 11, 22, 33]])
# 獲取上述ndarray裡面的6,7,11,22
res3[1:3,1:3]
array([[ 6,  7],
       [11, 22]])
4  運算
res * 3
res = np.array([[1,2,3],[4,5,6]])
res * 3
array([[ 3,  6,  9],
       [12, 15, 18]])
res1 + res2
res1 = np.array([[11,22,33],[44,55,66]])
res2 = np.array([[1,2,3],[4,5,6]])
res1 + res2
array([[12, 24, 36],
       [48, 60, 72]])

res1 * res2
array([[ 11,  44,  99],
       [176, 275, 396]])

res3 = np.array([[1,2,3,4],[5,6,7,8]])
res1 + res3
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-56-b5f7cab23041> in <module>
      1 res3 = np.array([[1,2,3,4],[5,6,7,8]])
----> 2 res1 + res3

ValueError: operands could not be broadcast together with shapes (2,3) (2,4) 

reshap

res4 = np.array([111,222,333,444,555,666])
# 轉換陣列的維數  轉的時候一定要注意元素的個數到底夠不夠 不夠直接報錯
res4.reshape(2,3)
array([[111, 222, 333],
       [444, 555, 666]])

res4.reshape(3,3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-60-274f26b3bc7a> in <module>
----> 1 res4.reshape(3,3)

ValueError: cannot reshape array of size 6 into shape (3,3)

布林型索引

import random
li = [random.randint(1,10) for i in range(30)]
'''
python裡面的列表生成式
new_list = []
for i in range(30):
    new_list.append(random.randint(1,10))  # 往列表中新增30個1到10的隨機數
'''
res = np.array(li)
res
array([ 3,  8,  7, 10,  6,  2,  7,  4,  1, 10,  5,  3,  6,  6,  2,  9,  6,
        8,  1,  5,  7,  8,  2,  6,  6,  4,  5,  5,  9,  5])

# 求出數字中大於5的數
# res > 5 # 就是跟數組裡面的每一個元素作比較    結果是布林值索引
res[res>5] # 布林值索引取值
# 布林型索引:將同樣大小的布林陣列傳進索引,會返回一個有True對應位置的元素的陣列
array([ 8,  7, 10,  6,  7, 10,  6,  6,  9,  6,  8,  7,  8,  6,  6,  9])

花式索引

res5 = np.array([1,2,3,4,5,6,7,8,9,10])
# 拿出 2 6 9 10
res5[[1,5,8,9]] # 花式索引
array([ 2,  6,  9, 10])
# 在python中如何獲取元素的索引值
# l = [111,222,333,444,555,666]
# l.index(333)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-70-305672a1ca18> in <module>
      2 # l = [111,222,333,444,555,666]
      3 # l.index(333)
----> 4 res5.index(3)

AttributeError: 'numpy.ndarray' object has no attribute 'index'

通用函式

# 1.絕對值
np.abs([-1,-2,-3])
np.fabs([-11.,-22.,-33.])
array([11., 22., 33.])

# 2.平方根
np.sprt(2)
1.4142135623730951
# 3 平方
np.square(4)
16
# 4 e的次方
np.exp(1)
2.718281828459045
# 5 自然對數
np.log(4)
1.3862943611198906
# 6 ceil取整 取大
np.ceil(5.4)
6.0
# 7 floor取整 取小
np.floor(4.6)
4.0
# 8 四捨五入
np.rint(4.6)
5.0
# 9 將小數分割成整數和小數
np.modf(4.5)
(0.5, 4.0)
np.modf([3.4,5.6])
(array([0.4, 0.6]), array([3., 5.]))
# 10 isnan  判斷當前資料是否缺失
np.isnan(56)
False
# 11 cos sin tan
np.cos(45)
0.5253219888177297

補充

1.自動提示

​    按TAB鍵

2.自動檢視方法的使用方式

   在方法名後面加?執行即可



3.程式碼的註釋

​         ctrl + ?