數據分析之numpy(二)
八、花式索引法
花式一:
import numpy as np代碼:names=np.array(["jone","alex","maty","bob","meimei","joy","jasen"]) namesarray([jone, alex, maty, bob, meimei, joy, jasen], dtype=<U6)代碼:arar=np.random.randn(7,5) arararray([[ 1.74244453, -0.24359124, -1.04920117, -0.57646851, 1.7108311 ], [ 0.05394535, 1.6802235 , 0.56517872, 2.4561455 , 2.29771208], [-0.57232895, 1.09965133, 0.46836728, -0.80126458, -0.8332292 ], [-0.60305896, -1.28418309, -0.82449585, -1.12472598, -1.27699896], [-1.08231699, -0.73265317, 1.41289305, 1.01787527, -1.67563334], [-1.48467021, -1.11201939, 0.62802462, -0.29447066, 0.9732206 ], [ 0.04211349, -0.3905672 , 0.09110404, 1.27503489, 0.18248142]])代碼:arar[names=="bob"]#以bob在names的位置索引arararray([[-0.60305896, -1.28418309, -0.82449585, -1.12472598, -1.27699896]])代碼:arar[names!="jone"]array([[ 0.05394535, 1.6802235 , 0.56517872, 2.4561455 , 2.29771208], [-0.57232895, 1.09965133, 0.46836728, -0.80126458, -0.8332292 ], [-0.60305896, -1.28418309, -0.82449585, -1.12472598, -1.27699896], [-1.08231699, -0.73265317, 1.41289305, 1.01787527, -1.67563334], [-1.48467021, -1.11201939, 0.62802462, -0.29447066, 0.9732206 ], [ 0.04211349, -0.3905672 , 0.09110404, 1.27503489, 0.18248142]])代碼:arar[~(names!="jone")]#~反轉條件array([[ 1.74244453, -0.24359124, -1.04920117, -0.57646851, 1.7108311 ]])代碼:namesarray([jone, alex, maty, bob, meimei, joy, jasen], dtype=<U6)代碼:names_1=(names=="bob")|(names=="joy")#names中是bob也是joy的布爾型索引 names_1array([False, False, False, True, False, True, False], dtype=bool)代碼:arar[names_1]array([[-0.60305896, -1.28418309, -0.82449585, -1.12472598, -1.27699896], [-1.48467021, -1.11201939, 0.62802462, -0.29447066, 0.9732206 ]])代碼:arar[names!="joy"]=7#索引改array arararray([[ 7. , 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. , 7. ], [ 7. , 7. , 7. , 7. , 7. ], [-1.48467021, -1.11201939, 0.62802462, -0.29447066, 0.9732206 ], [ 7. , 7. , 7. , 7. , 7. ]])代碼:arar[[1,2,3]]array([[ 7., 7., 7., 7., 7.], [ 7., 7., 7., 7., 7.], [ 7., 7., 7., 7., 7.]])代碼:arar[:3]array([[ 7., 7., 7., 7., 7.], [ 7., 7., 7., 7., 7.], [ 7., 7., 7., 7., 7.]])
花式二:
代碼:arr3=np.array([[[1,2,3],[4,5,6],[7,8,9]],[[3,2,1],[6,5,4],[9,8,7]]]) arr3array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[3, 2, 1], [6, 5, 4], [9, 8, 7]]])代碼:arr3[[0,1]]array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[3, 2, 1], [6, 5, 4], [9, 8, 7]]])代碼:arr3_1=arr3[0,:1] arr3_1array([[1, 2, 3]])代碼:arr3_1.shape(1, 3)
花式三:
代碼:arrr=np.arange(20).reshape(5,4) arrrarray([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])代碼:arrr[2:,1:]#錢為行後為列array([[ 9, 10, 11], [13, 14, 15], [17, 18, 19]])代碼:arrr[1:,1:]array([[ 5, 6, 7], [ 9, 10, 11], [13, 14, 15], [17, 18, 19]])
九、改變行與列
代碼:arrr[[4,2,3,0,1]]#改變行array([[16, 17, 18, 19], [ 8, 9, 10, 11], [12, 13, 14, 15], [ 0, 1, 2, 3], [ 4, 5, 6, 7]])代碼:arrr[:,[0,3,1,2]]#改變列array([[ 0, 3, 1, 2], [ 4, 7, 5, 6], [ 8, 11, 9, 10], [12, 15, 13, 14], [16, 19, 17, 18]])代碼:arrr[[4,2,3,0,1]][:,[0,3,1,2]]#改變行與列array([[16, 19, 17, 18], [ 8, 11, 9, 10], [12, 15, 13, 14], [ 0, 3, 1, 2], [ 4, 7, 5, 6]])
十、簡單的矩陣運算
代碼:dott=np.arange(20).reshape(4,5) dottarray([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]])代碼:dott.T#轉置array([[ 0, 5, 10, 15], [ 1, 6, 11, 16], [ 2, 7, 12, 17], [ 3, 8, 13, 18], [ 4, 9, 14, 19]])代碼:np.dot(dott.T,dott)#矩陣乘法array([[350, 380, 410, 440, 470], [380, 414, 448, 482, 516], [410, 448, 486, 524, 562], [440, 482, 524, 566, 608], [470, 516, 562, 608, 654]])代碼:np.dot(dott,dott.T)array([[ 30, 80, 130, 180], [ 80, 255, 430, 605], [ 130, 430, 730, 1030], [ 180, 605, 1030, 1455]])代碼:asr=np.arange(100).reshape(5,2,10) asrarray([[[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]], [[20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]], [[40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]], [[60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79]], [[80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]])代碼:asr.transpose((2,1,0))array([[[ 0, 20, 40, 60, 80], [10, 30, 50, 70, 90]], [[ 1, 21, 41, 61, 81], [11, 31, 51, 71, 91]], [[ 2, 22, 42, 62, 82], [12, 32, 52, 72, 92]], [[ 3, 23, 43, 63, 83], [13, 33, 53, 73, 93]], [[ 4, 24, 44, 64, 84], [14, 34, 54, 74, 94]], [[ 5, 25, 45, 65, 85], [15, 35, 55, 75, 95]], [[ 6, 26, 46, 66, 86], [16, 36, 56, 76, 96]], [[ 7, 27, 47, 67, 87], [17, 37, 57, 77, 97]], [[ 8, 28, 48, 68, 88], [18, 38, 58, 78, 98]], [[ 9, 29, 49, 69, 89], [19, 39, 59, 79, 99]]])代碼:asr.transpose((0,2,1))array([[[ 0, 10], [ 1, 11], [ 2, 12], [ 3, 13], [ 4, 14], [ 5, 15], [ 6, 16], [ 7, 17], [ 8, 18], [ 9, 19]], [[20, 30], [21, 31], [22, 32], [23, 33], [24, 34], [25, 35], [26, 36], [27, 37], [28, 38], [29, 39]], [[40, 50], [41, 51], [42, 52], [43, 53], [44, 54], [45, 55], [46, 56], [47, 57], [48, 58], [49, 59]], [[60, 70], [61, 71], [62, 72], [63, 73], [64, 74], [65, 75], [66, 76], [67, 77], [68, 78], [69, 79]], [[80, 90], [81, 91], [82, 92], [83, 93], [84, 94], [85, 95], [86, 96], [87, 97], [88, 98], [89, 99]]])
十一、通用函數
代碼:x = np.random.randn(10) xarray([-1.16962193, 1.83074349, -0.16067879, 0.38896856, -0.68373755, -0.68457496, 0.88010085, -0.4234542 , 0.04302339, 0.75449822])代碼:np.exp(x) #e的x次方 點對點array([ 0.3104843 , 6.2385232 , 0.85156555, 1.47545816, 0.50472702, 0.50430453, 2.41114285, 0.65478116, 1.04396232, 2.12654419])代碼:np.sqrt(np.arange(10)) #sqrt開根array([ 0. , 1. , 1.41421356, 1.73205081, 2. , 2.23606798, 2.44948974, 2.64575131, 2.82842712, 3. ])代碼:xx=np.arange(5) xy=np.array([1,4,2,6,3]) xx xyarray([0, 1, 2, 3, 4])array([1, 4, 2, 6, 3])代碼:np.maximum(xx,xy) #點對點選最大array([1, 4, 2, 6, 4])代碼:np.minimum(xx,xy)#點對點選最小array([0, 1, 2, 3, 3])代碼:x = np.random.randn(10)#modf 返回小鼠部分與整數部分 fraction,whole_part = np.modf(x) fractionarray([ 0.15137869, -0.91615035, -0.85996433, -0.20454559, -0.05266373, -0.44921799, 0.07141493, 0.0253758 , 0.19156079, -0.53004007])代碼:whole_partarray([ 1., -0., -0., -0., -2., -0., 0., 0., 1., -1.])代碼:xo=np.random.randn(10) np.sqrt(xo)array([ nan, 0.6942882 , 1.35745873, nan, 0.78378984, 0.40086869, 0.60299044, 0.57813193, 1.10384972, 0.25834013])代碼:xo#沒改變原來的xoarray([-1.77416866, 0.4820361 , 1.8426942 , -0.16437971, 0.61432652, 0.16069571, 0.36359747, 0.33423653, 1.21848421, 0.06673962])代碼:np.sqrt(xo, xo)array([ nan, 0.6942882 , 1.35745873, nan, 0.78378984, 0.40086869, 0.60299044, 0.57813193, 1.10384972, 0.25834013])代碼:xo#改變了原來的xoarray([ nan, 0.6942882 , 1.35745873, nan, 0.78378984, 0.40086869, 0.60299044, 0.57813193, 1.10384972, 0.25834013])
推薦閱讀:
※python模擬登陸知乎(最新版)
※OpenResty部署反向代理
※部分免費Python免費視頻
※全棧 - 8 爬蟲 使用urllib2獲取數據