數據分析 --- 01. Numpy

NumPy(Numerical Python) 是 Python 語言的一個擴展程序庫,支持大量的維度數組與矩陣運算,此外也針對數組運算提供大量的數學函數庫。

一.建立 

  1. 使用np.array() 建立

導入模塊:

import numpy as np

 

  ①一維數據建立數組

np.array([1,2,3,4,5])
結果爲:
array([1, 2, 3, 4, 5])

  

  ②二維數組建立dom

np.array([[1,2,3.2],[4,5,6]])
array([[ 1. ,  2. ,  3.2],
       [ 4. ,  5. ,  6. ]])

 

注意:

numpy默認ndarray的全部元素的類型是相同的
若是傳進來的列表中包含不一樣的類型,則統一爲同一類型,優先級:str
>float>int

 

  2.使用np 的 routlines 函數建立

  ①np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 等差數列函數

np.linspace(0,100,num=51)
結果爲:

array([ 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22., 24., 26., 28., 30., 32., 34., 36., 38., 40., 42., 44., 46., 48., 50., 52., 54., 56., 58., 60., 62., 64., 66., 68., 70., 72., 74., 76., 78., 80., 82., 84., 86., 88., 90., 92., 94., 96., 98., 100.])

 

  ②np.arange([start, ]stop, [step, ]dtype=None)ui

np.arange(0,100,2)
結果爲:

array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98])

 

  ③np.random.randint(low, high=None, size=None, dtype='l')lua

np.random.randint(0,100,size=(5,6))
結果爲:

array([[46, 91, 10, 79, 98, 47], [66, 41, 99, 7, 69, 22], [99, 51, 25, 49, 82, 59], [ 6, 20, 28, 33, 63, 66], [29, 58, 91, 46, 60, 57]])

 

  ④np.random.random(size=None)spa

np.random.random((5,5))
結果爲:

array([[ 0.44483672, 0.14777086, 0.83894078, 0.50285068, 0.27617434], [ 0.96386475, 0.11509807, 0.83173289, 0.93913778, 0.74246338], [ 0.71150576, 0.09607129, 0.04946329, 0.72253115, 0.65797176], [ 0.47363412, 0.2775543 , 0.40101206, 0.05375956, 0.30270107], [ 0.05198828, 0.62509195, 0.42600588, 0.18691781, 0.54398897]])

 

#指定變化因子,生成的數據就不會變化了
np.random.seed(90) np.random.randint(0,100,size=(5,6))
array([[91, 29, 31, 67, 39, 68],
       [58, 37, 18, 74, 96, 51],
       [30, 80, 18, 77, 82,  9],
       [ 0,  0, 10, 95, 12,  4],
       [26,  9, 48, 70, 75, 80]])

 

二.屬性

4個必記參數: 

  ndim:維度

  shape:形狀(各維度的長度)

  size:總長度
  dtype:元素類型

 

  #使用matplotlib.pyplot獲取一個numpy數組,數據來源於一張圖片3d



import matplotlib.pyplot as plt %matplotlib inline

cat_img_arr = plt.imread('./cat.jpg')

plt.imshow(cat_img_arr)

 

 

#查看形狀:

cat_img_arr.shape
#結果爲:

(456, 730, 3)

 

 

#查看元素類型:
cat_img_arr.dtype
結果爲:

dtype('uint8')

 

三.基本操做

#生成數組

arr = np.random.randint(60,100,size=(7,5))
array([[75, 90, 93, 74, 91],
       [62, 62, 83, 88, 62],
       [78, 61, 92, 67, 82],
       [66, 73, 62, 61, 66],
       [93, 73, 72, 67, 93],
       [79, 71, 98, 69, 62],
       [64, 94, 98, 83, 74]])

 

  1.索引

①獲取第一行code

arr[0]
array([75, 90, 93, 74, 91])

 

 ②獲取前兩行blog

arr[[0,1]]
array([[75, 90, 93, 74, 91],
       [62, 62, 83, 88, 62]])

 

  2.切片

①獲取前兩行排序

arr[0:2]
array([[75, 90, 93, 74, 91],
       [62, 62, 83, 88, 62]])

 

 ②獲取前兩列

arr[:,0:2]  #逗號左邊表示行 右邊表示列
array([[75, 90],
       [62, 62],
       [78, 61],
       [66, 73],
       [93, 73],
       [79, 71],
       [64, 94]])

③獲取前兩行和前兩列

arr[0:2,0:2]
array([[75, 90],
       [62, 62]])

 

④行倒序

arr[::-1]
array([[64, 94, 98, 83, 74],
       [79, 71, 98, 69, 62],
       [93, 73, 72, 67, 93],
       [66, 73, 62, 61, 66],
       [78, 61, 92, 67, 82],
       [62, 62, 83, 88, 62],
       [75, 90, 93, 74, 91]])

 

⑤列倒序

arr[:,::-1]
array([[91, 74, 93, 90, 75],
       [62, 88, 83, 62, 62],
       [82, 67, 92, 61, 78],
       [66, 61, 62, 73, 66],
       [93, 67, 72, 73, 93],
       [62, 69, 98, 71, 79],
       [74, 83, 98, 94, 64]])

 

⑥所有倒序

arr[::-1,::-1]
array([[74, 83, 98, 94, 64],
       [62, 69, 98, 71, 79],
       [93, 67, 72, 73, 93],
       [66, 61, 62, 73, 66],
       [82, 67, 92, 61, 78],
       [62, 88, 83, 62, 62],
       [91, 74, 93, 90, 75]])

 

⑦將圖片的數組倒敘

 

 

 

 

 

   3.變形

①將多維數組變造成一維數組

arr1 = arr.reshape((35,))
array([75, 90, 93, 74, 91, 62, 62, 83, 88, 62, 78, 61, 92, 67, 82, 66, 73,
       62, 61, 66, 93, 73, 72, 67, 93, 79, 71, 98, 69, 62, 64, 94, 98, 83,
       74])

 

②將一維數組變造成多維數組



arr1.reshape((5,7))

# -1 表示自動補充,這兩種結果同樣 arr1.reshape((
-1,7))
array([[75, 90, 93, 74, 91, 62, 62],
       [83, 88, 62, 78, 61, 92, 67],
       [82, 66, 73, 62, 61, 66, 93],
       [73, 72, 67, 93, 79, 71, 98],
       [69, 62, 64, 94, 98, 83, 74]])

 

  4.級聯

①將兩個數組進行級聯

np.concatenate((arr,arr),axis=1)
array([[75, 90, 93, 74, 91, 75, 90, 93, 74, 91],
       [62, 62, 83, 88, 62, 62, 62, 83, 88, 62],
       [78, 61, 92, 67, 82, 78, 61, 92, 67, 82],
       [66, 73, 62, 61, 66, 66, 73, 62, 61, 66],
       [93, 73, 72, 67, 93, 93, 73, 72, 67, 93],
       [79, 71, 98, 69, 62, 79, 71, 98, 69, 62],
       [64, 94, 98, 83, 74, 64, 94, 98, 83, 74]])

 

②合併圖片

arr_3 = np.concatenate((cat_img_arr,cat_img_arr,cat_img_arr),axis=1)
arr_9 = np.concatenate((arr_3,arr_3,arr_3),axis=0)
plt.imshow(arr_9)

 

 

級聯須要注意的點:

  級聯的參數是列表:必定要加中括號或小括號
  維度必須相同
  形狀相符:在維度保持一致的前提下,若是進行橫向(axis
=1)級聯,必須保證進行級聯的數組行數保持一致。若是進行縱向(axis=0)級聯,必須保證進行級聯的數組列數保持一致。
  可經過axis參數改變級聯的方向

 

   5.切分

 

 四.聚合操做

np.sum    求和
np.prod    np.nanprod    Compute product of elements
np.mean    平均
np.std    np.nanstd    Compute standard deviation
np.var    np.nanvar    Compute variance
np.min    最小
np.max    最大
np.argmin    np.nanargmin    Find index of minimum value
np.argmax    np.nanargmax    Find index of maximum value
np.median    np.nanmedian    Compute median of elements
np.percentile    np.nanpercentile    Compute rank-based statistics of elements
np.any    N/A    Evaluate whether any elements are true
np.all    N/A    Evaluate whether all elements are true
np.power 冪運算

 

 示例:

arr.mean(axis=1)
array([ 84.6,  71.4,  76. ,  65.6,  79.6,  75.8,  82.6])

 

 五.排序

np.sort()與ndarray.sort()均可以,但有區別:

  np.sort()不改變輸入值
  ndarray.sort()本地處理,不佔用空間,但改變輸入值

 

 

 

相關文章
相關標籤/搜索