python從數分到數編(part2)--隨機數及數組

學習筆記,僅供參考,覺得哪裏不錯就記哪裏

學習書目:《python編程從數據分析到數據編程》–朝樂門;

參考自:numpy.random.randn()與rand()的區別;Python numpy.full函數方法的使用




隨機數


生成一個隨機數


輸入:

import random
#設置隨機種子
random.seed(3)
#生成一個[-10, 10]之間的隨機浮點數,並保留2位小數
round(random.uniform(-10, 10), 2)
#生成[1, 100]之間的隨機整數
random.randint(1, 100)
#生成[0.0, 1.0)之間的隨機浮點數
random.random() 

輸出:

-5.24
70
0.13042279608514273

生成一個隨機數組


輸入:

import numpy as np
#設置隨機種子
rand = np.random.RandomState(10)
#生成3*4的隨機數組,範圍就是[0,10)內的整數
#生成在半開半閉區間[low,high)上離散均勻分佈的整數值
rand.randint(0, 10, (3, 4))
#生成[0, 100]的隨機數組
#以給定的形狀創建一個數組,並在數組中加入在[0,1]之間均勻分佈的隨機樣本。 
rand.rand(10)*100
#生成等距數列
np.linspace(0, 10, 10)
np.linspace(0, 10, 9)

輸出:

array([[9, 4, 0, 1],
       [9, 0, 1, 8],
       [9, 0, 8, 6]])
array([ 68.53598184,  95.33933462,   0.39482663,  51.21922634,
        81.26209617,  61.25260668,  72.17553174,  29.18760682,
        91.77741225,  71.45757834])
array([  0.        ,   1.11111111,   2.22222222,   3.33333333,
         4.44444444,   5.55555556,   6.66666667,   7.77777778,
         8.88888889,  10.        ])
array([  0.  ,   1.25,   2.5 ,   3.75,   5.  ,   6.25,   7.5 ,   8.75,  10.  ])


np.full方法


語法:

numpy.full(shape, fill_value, dtype=None, order='C')

  • 參數解釋
參數 含義
shape 參數值爲整數或整數序列,新數組的形態,單個值代表一維,參數傳元組,元組中元素個數就代表是幾維,例如, (2, 3) or 2.
fill_value 參數值爲標量(無向量),表示填充數組的值
dtype 參數值爲字符串型數據,表示填充數組的數據類型,默認值爲None
order 參數值爲可選項{‘C’, ‘F’},,在內存中以行爲主(C風格)或列爲主(Fortran風格)連續(行或列)順序存儲多維數據。

  • 舉個例子

輸入:

np.full((2, 3), 5, 'int64', 'C')

輸出:

array([[5, 5, 5],
       [5, 5, 5]], dtype=int64)

形狀與重構


輸入:

import numpy as np
array1 = np.arange(1, 21)
id(array1)
#查看形狀
array1.shape
#利用reshape重構,返回另一個新的數組
reArray = array1.reshape(4, 5)
reArray.shape
id(reArray)
#利用resize重構,更改數組本身,即就地修改
array1.resize(5, 4)
array1
#轉置變換,返回另一個新的數組
array1.swapaxes(0, 1)
#將多維數組轉換爲1維數組,返回另一個新的數組
array1.flatten()
#將多維數組轉換爲嵌套列表,返回另一個新的列表
array1.tolist()
#重新設定數組的數據類型,返回另一個新的數組
array1.astype(np.float)

輸出:

652802545664
(20,)
(4, 5)
652802546784
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16],
       [17, 18, 19, 20]])
array([[ 1,  5,  9, 13, 17],
       [ 2,  6, 10, 14, 18],
       [ 3,  7, 11, 15, 19],
       [ 4,  8, 12, 16, 20]])
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20])
[[1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12],
 [13, 14, 15, 16],
 [17, 18, 19, 20]]
array([[  1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.],
       [  9.,  10.,  11.,  12.],
       [ 13.,  14.,  15.,  16.],
       [ 17.,  18.,  19.,  20.]])

ndarray的拆分與合併


拆分


輸入:

import numpy as np
array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
#橫向拆分, split方法
x1, x2, x3 = np.split(array1, [3, 5])
x1, x2, x3
#縱向拆分
upper, lower = np.vsplit(array1.reshape(2, 4), [1])
#可以試試[0], [2], [3]
upper
lower

輸出:

(array([1, 2, 3]), array([4, 5]), array([6, 7, 8]))
array([[1, 2, 3, 4]])
array([[5, 6, 7, 8]])

合併


輸入:

#數據的合併
np.concatenate((upper, lower), axis = 0)
#axis = 0表示對列進行拼接,axis = 1表示對行進行拼接
#列拼接, 列數必須相等
np.vstack([upper, lower])
#行拼接,行數必須相等
np.hstack([upper, lower])

輸出:

array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
array([[1, 2, 3, 4, 5, 6, 7, 8]])

插入與刪除


輸入:

import numpy as np
array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
#刪除某個位置的元素
np.delete(array1, 2)
#在某個位置插入特定元素
np.insert(array1, 1, 10)

輸出:

array([1, 2, 4, 5, 6, 7, 8])
array([ 1, 10,  2,  3,  4,  5,  6,  7,  8])

缺失值處理


檢測缺失值


輸入:

import numpy as np
array1 = np.array([1, 2, 3, 4, 5, 6, 7, 8])
#判斷數組的每個元素是否爲缺失值
np.isnan(array1)
#判斷數組中是否至少有一個缺失值
#any:一個爲True則返回True
np.any(np.isnan(array1))
#判斷數組中是否全都是缺失值
#any:全都爲True則返回True
np.all(np.isnan(array1))

輸出:

array([False, False, False, False, False, False, False, False], dtype=bool)
False
False

在缺失值存在的情況下求和


輸入:

import numpy as np
array1 = np.array([1, 2, 3, np.nan])
#返回給定軸上的數組元素的和,將非數字(nan)處理爲零。
np.nansum(array1)
np.sum(array1)

輸出:

6.0
nan

ndarray的排序


輸入:

import numpy as np
array1 = np.array([5, 4, 6, 3, 7])
#返回排序結果
np.sort(array1)
#返回排序後的index
np.argsort(array1)
array2 = np.random.randint(0, 10, (3, 4))
array2
#分別對多維數組,按照行(1)和列(0)進行排序
np.sort(array2, axis = 1)
np.sort(array2, axis = 0)

輸出:

array([3, 4, 5, 6, 7])
array([3, 1, 0, 2, 4], dtype=int64)
array([[6, 6, 5, 0],
       [6, 0, 1, 1],
       [6, 6, 3, 5]])
array([[0, 5, 6, 6],
       [0, 1, 1, 6],
       [3, 5, 6, 6]])
array([[6, 0, 1, 0],
       [6, 6, 3, 1],
       [6, 6, 5, 5]])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章