• <tfoot id="ukgsw"><input id="ukgsw"></input></tfoot>
    
    • 久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
      分享

      【Python數據挖掘】第三篇

       highoo 2019-03-20

      一、Numpy

      數組是一系列同類型數據的集合,可以被非零整數進行索引,可以通過列表進行數組的初始化,數組也可以通過索引進行切片。

      Numpy提供了幾乎全部的科學計算方式。

      1
      2
      # numpy 導入方式:
      import numpy as np

      ①、創建數組:

      1.簡單一二維數組

      1
      2
      3
      4
      5
      np.array( [1,2,3,4] )                 #  一維數組
      np.array( ['1',5,True] )              #  數組內容為字符型
      np.array( [True,True] )               #  布爾型數組
      np.array( [[1,2,3,4] , [5,6,7,8]] )   #  二維數組

      2.范圍函數生成 一維數組:

      1
      2
      3
      4
      np.arange([start,] stop[, step,], dtype=None)
      np.arange(1,10)
      # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

      3.均分函數生成 一維數組:(等差數列)

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
      start : 初始值
      stop : 末尾值
      num : 生成的樣本數 , 必須為非負數
      endpoint : 默認True  , 數組最后一個元素為 stop項
      # 數組step計算:
      當 endpoint = True 時, step =  (end - start) / (num - 1)
      當 endpoint = False 時, step =  (end - start) / num
      np.linspace(1,10,num=5,endpoint=False)
      # array([ 1. ,  2.8,  4.6,  6.4,  8.2])

      4.創建元素為1 的數組

      1
      2
      np.ones(4)          #  一維數組  array([ 1.,  1.,  1.,  1.])
      np.ones([4,5])      #  二維數組  4行5列

      5.創建元素為0 的數組

      1
      2
      np.zeros(4)          #  一維數組   array([ 0.,  0.,  0.,  0.])
      np.zeros([4,5])      #  二維數組   4行5列

      6.創建一定形狀的數組

      1
      2
      3
      numpy.empty(shape, dtype=float, order='C')
      np.empty([2,3])            # 創建2行3列數組

      7.創建方陣型,行列相等,對角元素為1,其余元素為0

      1
      2
      3
      4
      5
      np.eye(4)                       #  4行4列 ,  元素為0 , 對角線元素為1
      array([[ 1.0.0.0.],
             [ 0.1.0.0.],
             [ 0.0.1.0.],
             [ 0.0.0.1.]])

      8.創建與某數組大小相同的數組,元素為0

      1
      2
      arr1 = np.eye(4)              #  4行4列
      arr2 = np.empty_like(arr1)    #  4行4列

      9.Series轉換Array

      1
      np.array(series)

      ②、Numpy下的random類創建 隨機數組

      1.創建符合 [0:1) 均勻分布 的數組

      1
      2
      3
      np.random.rand(d0, d1, ..., dn)
      np.random.rand(4,5)          # 4行5列數組 

      2.創建符合 標準正態分布 的數組

      1
      2
      3
      np.random.randn(d0, d1, ..., dn)
      np.random.randn(4,5)         # 4行5列數組

      3.創建 隨機整數 的數組 , (不包含)

      1
      2
      3
      4
      5
      np.random.randint(low, high=None, size=None, dtype='l')
      np.random.randint(5, size=(2, 4))           # 生成0到4之間的 2 x 4數組
      array([[4, 0, 2, 1],
             [3, 2, 2, 0]])

      4.創建 隨機整數 的數組 , (包含)

      1
      2
      3
      4
      5
      np.random.random_integers(low, high=None, size=None)
      np.random.random_integers(5, size=(2, 4))   # 生成1到5之間的 2 x 4數組
      array([[3, 3, 4, 3],
             [3, 4, 1, 5]])

      5.創建 [0.0,1.0) 隨機浮點數

      1
      2
      3
      4
      5
      6
      7
      np.random.random(size=None)
      np.random.random_sample(size=None)
      np.random.ranf(size=None)
      np.random.sample(size=None)
      np.random.random( (5,) )
      np.random.random_sample( (4,5) )           # 4行5列 浮點數數組

      6.從給定的1-D數組 生成隨機樣本

      1
      2
      3
      4
      5
      6
      7
      8
      np.random.choice(a, size=None, replace=True, p=None)
      p:1-D array-like,可選 ( 設置概率 )與a中的每個條目相關聯的概率。如果沒有給出樣本,則假設在a中的所有條目均勻分布。
      np.random.choice(5, 3)                  #  從np.arange(5)生成大小為3的均勻隨機樣本:
      np.random.choice(5, 3, replace=False)   #  從np.arange(5)生成大小為3的均勻隨機樣本,沒有重復:
      aa_milne_arr = ['pooh', 'rabbit', 'piglet', 'Christopher']
      np.random.choice(aa_milne_arr, 5, p=[0.5, 0.1, 0.1, 0.3])

      7.返回隨機字節

      1
      2
      3
      4
      np.random.bytes(length)
      np.random.bytes(10)
      # b'u\x1e\xd6\x8d\xf5]\xab6\xed\x0c'

      ③、重要屬性

      1
      2
      3
      4
      np.shape     # 查看數組的維度  如:  (4,)  一個數字代表1維 , (5,6) 代表二維,5行6列數組  , .....
      np.size      # 查看數組元素總個數
      np.ndim      # 查看數組維度數
      len(array)   # 查看數組行數

      ④、重要方法

      1. 給定條件判斷元素

      1
      2
      3
      4
      5
      6
      numpy.where(condition[, x, y])                #  根據條件,從x或y返回元素。
      np.where(arr1 > 0 , True , False )
      array([[FalseTrueTrue, False],
             [ True, False, False, False],
             [ TrueTrueTrue, False]], dtype=bool)

      2.查找數組唯一元素

      1
      2
      3
      4
      5
      6
      7
      8
      np.unique(ar, return_index=False, return_inverse=False, return_counts=False)[source]
      return_counts = True   # 返回出現次數<br>
      np.unique([1, 1, 2, 2, 3, 3])
      # array([1, 2, 3])
      a = np.array([[1, 1], [2, 3]])
      np.unique(a)
      # array([1, 2, 3])

      3.兩個數組連接 

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      np.concatenate((a1, a2, ...), axis=0)     # 沿現有軸連接數組序列。
      a = np.array([[1, 2], [3, 4]])
      b = np.array([[5, 6]])
      np.concatenate((a, b), axis=0)
      array([[1, 2],
             [3, 4],
             [5, 6]])
      np.concatenate((a, b.T), axis=1)

      ⑤、索引與切片

      ⑥、數組計算

      1.加法

      1
      2
      3
      4
      5
      6
      7
      a = np.array([1,2,3])
      b = np.array([-1,2,-4])
      np.add(x1, x2[, out]) = <ufunc 'add'>
      np.add(a,b)              #  等效于 a + b
      # array([ 0,  4, -1])

      2.減法

      1
      2
      3
      np.subtract(x1, x2[, out]) = <ufunc 'subtract'>
      np.subtract(a,b)       # a - b

      3.乘法

      1
      2
      3
      np.multiply(x1, x2[, out]) = <ufunc 'multiply'>
      np.multiply(a,b)       # a * b

      4.除法

      1
      2
      3
      np.divide(x1, x2[, out]) = <ufunc 'divide'>
      np.divide(a,b)         # a / b

      5.點積 (相乘后把元素相加)  

      兩矩陣的點積 需要 左邊矩陣列 與 右邊矩陣行 數目相等

      1
      2
      3
      4
      np.dot(a, b, out=None)
      np.dot(a,b)
      np.dot(a,b.T)

      6.廣播

      兩矩陣相加 , 類型shape不一樣時 , 自動廣播計算 ,作用在每一行每個元素

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      arr1 = np.random.randint(1,10,size=(3,4))
      array([[3, 3, 4, 1],
             [8, 4, 8, 2],
             [6, 4, 4, 9]])
      arr2 = np.array([2,2,2,2])
      array([2, 2, 2, 2])
      arr1 + arr2
      array([[ 5563],
             [106, 104],
             [ 866, 11]])
      # 方式二 :
      arr1 + 6           # 每個元素都加6

      7.求和

      1
      2
      3
      4
      5
      6
      7
      8
      9
      np.sum(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue>)
      # 給定軸上的數組元素的總和。
      np.sum([0.5, 1.5])
      #  2.0
      np.sum([[0, 1], [0, 5]], axis=0)
      #  array([0, 6])
      np.sum([[0, 1], [0, 5]], axis=1)
      #  array([1, 5])

      8.求平均

      1
      2
      3
      4
      5
      6
      7
      8
      np.mean(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue>)
      # 沿指定軸計算算術平均值。
      a = np.array([[1, 2], [3, 4]])
      np.mean(a)
      #  2.5
      np.mean(a, axis=0)
      #  array([ 2.,  3.])

      9.求平方根

      1
      2
      3
      4
      5
      np.sqrt(x[, out]) = <ufunc 'sqrt'>
      # 按元素方式返回數組的正平方根。
      np.sqrt([1,4,9])
      # array([ 1.,  2.,  3.])

      10.求指數

      1
      2
      np.exp(x[, out]) = <ufunc 'exp'>
      # 計算輸入數組中所有元素的指數。

      11.求絕對值

      1
      2
      3
      4
      5
      6
      np.absolute(x[, out]) = <ufunc 'absolute'>
      # 逐個計算絕對值。
      x = np.array([-1.2, 1.2])
      np.absolute(x)
      # array([ 1.2,  1.2])

      12.求自然對數

      1
      2
      np.log(x[, out]) = <ufunc 'log'>
      # 自然對數,逐元素。

      ⑦、線性代數計算

      1.數組轉置

      1
      2
      arr1 = np.random.randint(0,10,size=(4,4))
      np.transpose(arr1)            # arr1.T

      2.矩陣的逆

      1
      2
      3
      4
      5
      a = np.array([[1,2],[4,7]])
      np.linalg.inv(a)
      array([[-7.2.],
             [ 4., -1.]])

      3.沿數組的對角線返回總和

      1
      2
      3
      a = np.array([[1,2],[4,7],[5,2]])
      np.trace(a)
      # 8

      4.正方形數組的特征值和右特征向量

      1
      2
      3
      4
      5
      w, v = np.linalg.eig(np.array([ [1, -1], [1, 1] ]))
      w; v
      array([ 1. + 1.j1. - 1.j])
      array([[ 0.70710678+0.j        0.70710678+0.j        ],
             [ 0.00000000-0.70710678j0.00000000+0.70710678j]])
      二、可視化

      ①、matplotlib 導入方式:                       ??官方文檔

      1
      import matplotlib.pyplot as plt

      ②、條形圖

      1
      2
      3
      4
      5
      6
      7
      8
      9
      plt.bar(left, height, width=0.8, bottom=None, hold=None, data=None, **kwargs)
      y = np.array([10,20,30,50])
      x = np.arange(len(y))
      plt.bar(x,y,color='r')                  # 垂直方向
      plt.show()
      plt.barh(x,y,color='r')                # 水平方向
      plt.show()

      ③、多圖形排列

      1
      2
      3
      4
      5
      6
      7
      8
      y = np.random.randint(10,100,size=(4,4))
      x = np.arange(4)
      plt.bar(x,y[0],color='r',width=0.25)
      plt.bar(x+0.25,y[1],color='b',width=0.25)
      plt.bar(x+0.5,y[2],color='g',width=0.25)
      plt.show()

      ④、圖形堆疊

      1
      2
      3
      plt.bar(x,y[0],color='r')
      plt.bar(x,y[1],color='y',bottom=y[0])
      plt.bar(x,y[2],color='g',bottom=y[0] + y[1])

      ⑤、散點圖

      1
      2
      3
      data = np.random.rand(1024,2)
      plt.scatter(data[:,0],data[:,1])
      plt.show()

      ⑥、直方圖

      1
      2
      3
      x = np.random.rand(1000)
      plt.hist(x,bins=50)             # 顯示50條
      plt.show()

      ⑦、箱形圖

      1
      2
      3
      x = np.random.randn(100,5)
      plt.boxplot(x)
      plt.show()

       

      注意:

      numpy數組計算中*和dot是有很大區別的

      1.numpy乘法運算中"*"是數組元素逐個計算具體代碼如下


       

      2.numpy乘法運算中dot是按照矩陣乘法的規則來運算的具體實現代碼如下:

       

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發表

        請遵守用戶 評論公約

        類似文章 更多

        主站蜘蛛池模板: 色偷偷人人澡久久超碰97| 午夜精品福利亚洲国产| 中文无码熟妇人妻AV在线| 国内丰满熟女出轨VIDEOS| 久久狠狠高潮亚洲精品| 国产乱码卡二卡三卡4| 老熟妇性色老熟妇性| 国产成A人片在线观看视频下载 | 呦交小U女精品视频| 亚洲夂夂婷婷色拍WW47| 精品乱码一区二区三区四区| 欧美大胆老熟妇乱子伦视频| 2019久久久高清日本道| 又大又长粗又爽又黄少妇毛片| 精品国产黑色丝袜高跟鞋| 亚洲欧美日韩综合久久久| 亚洲午夜成人精品电影在线观看| 亚洲AV午夜成人无码电影| 国产精品毛片在线完整版SAB| 成人看的污污超级黄网站免费 | 亚洲高清日韩专区精品| 性奴sm虐辱暴力视频网站| 亚洲 日本 欧洲 欧美 视频| 亚洲一区二区三区自拍公司| 久久毛片少妇高潮| 日本福利一区二区精品| 亚洲V天堂V手机在线| 国产欧美VA天堂在线观看视频 | 公喝错春药让我高潮 | 国产按头口爆吞精在线视频| 国产在线观看播放av| 亚洲AV中文无码乱人伦在线咪咕| 69堂人成无码免费视频果冻传媒| 色一情一乱一伦麻豆| 色窝窝无码一区二区三区成人网站| 欧美大屁股流白浆XXXX| 天下第二社区在线视频| 天下第二社区在线视频| 日韩精品人妻av一区二区三区| 天天夜碰日日摸日日澡性色AV| 国产在线欧美日韩精品一区|