2019-11-01

import numpy as np
x = np.arange(10)
x  # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
X = np.arange(15).reshape(5, 3)
X  # array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])

访问

x[2]  # 2
X[1, 1]  # 4

切片

x[5:]
# array([5, 6, 7, 8, 9])
# 行切片, 列切片
X[2: 4,1: ]
# array([[ 7,  8],
       [10, 11]])
X[2:, :2]
# array([[ 6,  7],
       [ 9, 10],
       [12, 13]])
# 如果说不关心reshape的另外一个参数,我们可以写成-1,numpy指定推导出这个参数
x.reshape(5, -1)

numpy的与运算

numpy’s universal function

X
# array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14]])
X + 1
# array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12],
       [13, 14, 15]])
X * 2
# array([[ 0,  2,  4],
       [ 6,  8, 10],
       [12, 14, 16],
       [18, 20, 22],
       [24, 26, 28]])
np.sin(X)
# array([[ 0.        ,  0.84147098,  0.90929743],
       [ 0.14112001, -0.7568025 , -0.95892427],
       [-0.2794155 ,  0.6569866 ,  0.98935825],
       [ 0.41211849, -0.54402111, -0.99999021],
       [-0.53657292,  0.42016704,  0.99060736]])

NUMPY 中的 argsort()

x = np.arange(16)
x
# array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])
# 随机打乱
from random import shuffle
np.random.shuffle(x)
x
# array([14,  5,  9,  1, 11, 15, 10,  0,  4, 12,  8, 13,  2,  3,  7,  6])
np.argsort(x)
# array([ 7,  3, 12, 13,  8,  1, 15, 14, 10,  2,  6,  4,  9, 11,  0,  5],
      dtype=int64)

Numpy中的布尔索引

names = np.array(['Bob', 'Joe', 'Will','Bob', 'Will', 'Joe', 'Joe'])
names
# array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='<U4')
# 使用np.random模块的randn 生成一些正太分布的随机数据
data = np.random.randn(7, 4)
data
# array([[ 0.9715914 ,  1.84276945, -1.03498205,  0.06479183],
       [-0.72963088,  0.54051887,  0.65735415,  1.70943972],
       [-1.32681499, -0.04807712,  1.94086214,  0.07310734],
       [ 0.05078277, -1.33179208, -0.67719624, -1.24228479],
       [ 1.70443473, -0.5033954 ,  0.25572451, -0.72601664],
       [ 0.47793404, -2.53421398, -0.92782367, -0.78611696],
       [-0.18870118,  0.01618071, -0.1922537 , -0.69942672]])
# 假设每个名字对应data数组的一行
names =='Bob'
# array([ True, False, False,  True, False, False, False])
# 布尔型索引可以应用于数据的筛选
data[names =='Bob']
# array([[ 1.61997836, -0.61026748, -1.06091459,  2.4365358 ],
       [ 1.13486616,  0.20752017,  0.65238338, -0.10738133]])
# 布尔型索引应用修改值
# 选取所有JOE的行,并且全部值 赋值为 666
data[names == 'Joe'] = 0
data
# array([[ 1.61997836, -0.61026748, -1.06091459,  2.4365358 ],
       [ 0.        ,  0.        ,  0.        ,  0.        ],
       [-0.56709889,  0.45822422,  1.64072796,  0.98907644],
       [ 1.13486616,  0.20752017,  0.65238338, -0.10738133],
       [-0.60478467, -0.22499332,  0.39228233, -1.00812209],
       [ 0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ]])
# 选取所有Will的行,并且将选取的数据的后两列赋值为 0
data[names == 'Will',2:] = 0
data
# array([[ 0.9715914 ,  1.84276945, -1.03498205,  0.06479183],
       [-0.72963088,  0.54051887,  0.65735415,  1.70943972],
       [-1.32681499, -0.04807712,  0.        ,  0.        ],
       [ 0.05078277, -1.33179208, -0.67719624, -1.24228479],
       [ 1.70443473, -0.5033954 ,  0.        ,  0.        ],
       [ 0.47793404, -2.53421398, -0.92782367, -0.78611696],
       [-0.18870118,  0.01618071, -0.1922537 , -0.69942672]])

https://www.jianshu.com/p/4307c3c3bb58

Python量化投资网携手4326手游为资深游戏玩家推荐:《《宝石研物语》:9月3日版本更新预告

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
NumPy
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论