2. Numpy使用

import numpy as np

numpy的基本操作

a = np.array([[1,2,3],[4,5,6]])
b = np.array([1,2,3,4])
c = np.array([[[1,2,3],[4,5,6]],[[1,2,3],[4,5,6]]])
print(a.shape)
print(b.shape)
print(c.shape)
(2, 3)
(4,)
(2, 2, 3)
a.dtype
dtype('int64')
a.itemsize
8
a.size
6
c.ndim
3

生成数组

d = np.array([[1,2,3],[4,5,6]],dtype=np.float32)
d.dtype
dtype('float32')
e=np.zeros([3,4],dtype="float32")
e
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]], dtype=float32)
e.dtype
dtype('float32')
e=np.zeros([3,4],dtype=np.float32)
e
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]], dtype=float32)
f=np.ones([3,4],dtype=np.int32)
f
array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 1, 1, 1]], dtype=int32)
# 从现有数组生成
score = np.array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])

score
array([[80, 89, 86, 67, 79],
       [78, 97, 89, 67, 81],
       [90, 94, 78, 67, 74],
       [91, 91, 90, 67, 69],
       [76, 87, 75, 67, 86],
       [70, 79, 84, 67, 84],
       [94, 92, 93, 67, 64],
       [86, 85, 83, 67, 80]])
score1=np.array(score)
score2=np.asarray(score)
score3=np.copy(score)
score[0][0]=100
score
array([[100,  89,  86,  67,  79],
       [ 78,  97,  89,  67,  81],
       [ 90,  94,  78,  67,  74],
       [ 91,  91,  90,  67,  69],
       [ 76,  87,  75,  67,  86],
       [ 70,  79,  84,  67,  84],
       [ 94,  92,  93,  67,  64],
       [ 86,  85,  83,  67,  80]])
score2
array([[100,  89,  86,  67,  79],
       [ 78,  97,  89,  67,  81],
       [ 90,  94,  78,  67,  74],
       [ 91,  91,  90,  67,  69],
       [ 76,  87,  75,  67,  86],
       [ 70,  79,  84,  67,  84],
       [ 94,  92,  93,  67,  64],
       [ 86,  85,  83,  67,  80]])
# 生成固定范围的数组
np.linspace(0, 100, 10)
array([  0.        ,  11.11111111,  22.22222222,  33.33333333,
        44.44444444,  55.55555556,  66.66666667,  77.77777778,
        88.88888889, 100.        ])
np.arange(10, 50, 15)
array([10, 25, 40])
a=np.random.uniform(0,100,1000000)

import matplotlib.pyplot as plt
plt.figure(figsize=(20,8),dpi=80)
plt.hist(a)
plt.show()
<Figure size 1600x640 with 1 Axes>
b=np.random.normal(0,1,(100,100))
b
array([[-6.54070831e-01, -8.58237690e-01,  1.12840828e-03, ...,
        -5.69480307e-02,  3.55998992e-01, -4.75176936e-01],
       [ 6.76076814e-01,  1.47650320e+00,  4.61246907e-01, ...,
         2.76736091e+00,  2.13077009e-01, -3.92740201e-01],
       [ 6.14808294e-01, -1.56088314e-01,  8.86520985e-03, ...,
         1.47150511e+00,  4.02858641e-01,  2.24224565e+00],
       ...,
       [-1.26753533e+00, -1.46173745e-01, -1.42753958e+00, ...,
        -8.50436456e-02,  5.38332925e-01, -1.05123949e+00],
       [-7.50317584e-02, -4.60182497e-01,  2.16468102e-01, ...,
        -1.38952087e-01, -4.67473834e-01,  1.08061128e+00],
       [-2.42025812e-01, -9.83338435e-02,  1.08822672e-01, ...,
        -1.26860857e+00,  3.39079875e-01,  2.23547985e-01]])
plt.figure(figsize=(20,8),dpi=80)
plt.hist(b,100)
plt.show()

数组的基本操作

# 创建符合正态分布的8只股票10天的涨跌幅数据
stock_change = np.random.normal(0, 1, (8, 10))
stock_change
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
         0.2366924 ,  0.91934989,  0.95733701,  0.7036584 , -0.25490962],
       [-0.99235101,  0.70940651, -0.66934701, -0.33760595,  1.75578837,
        -0.17527665,  1.127985  ,  1.16480656,  0.60324222,  2.220063  ],
       [-1.3326412 , -1.01831572, -1.09038797,  0.35145548, -1.52407572,
         1.24380911,  0.53576183, -0.87476354,  0.94061172, -1.54695238],
       [-0.6141568 ,  0.34004292,  0.53421929,  0.93273841, -2.39181704,
         0.73553393, -0.37974303,  0.12472411,  0.31243104, -0.09388522],
       [ 0.63699391,  0.54513968, -1.45114066,  0.12302606,  0.8179262 ,
        -0.98861413, -1.37164055,  0.04710424,  0.35452202, -1.77008598],
       [ 0.05821616, -0.10157081, -0.0177392 , -0.80556969, -0.644111  ,
         1.62028286,  0.12565102,  0.83290996,  1.06604483,  0.88367563],
       [ 1.82724499, -0.625139  , -1.00688955, -1.31627874, -0.81109591,
         0.96967537,  0.75956849, -0.80162364,  0.44827989,  2.05341272],
       [-1.7788185 ,  0.79750866, -0.29012385,  0.60888444, -1.05643106,
         0.17126915, -2.24288296,  0.30024122, -0.44027555,  2.33346992]])
stock_change[0, 0:3]
array([-0.5889853 , -0.93535336, -0.0675299 ])
stock_change.reshape((10,8))
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
         0.2366924 ,  0.91934989,  0.95733701],
       [ 0.7036584 , -0.25490962, -0.99235101,  0.70940651, -0.66934701,
        -0.33760595,  1.75578837, -0.17527665],
       [ 1.127985  ,  1.16480656,  0.60324222,  2.220063  , -1.3326412 ,
        -1.01831572, -1.09038797,  0.35145548],
       [-1.52407572,  1.24380911,  0.53576183, -0.87476354,  0.94061172,
        -1.54695238, -0.6141568 ,  0.34004292],
       [ 0.53421929,  0.93273841, -2.39181704,  0.73553393, -0.37974303,
         0.12472411,  0.31243104, -0.09388522],
       [ 0.63699391,  0.54513968, -1.45114066,  0.12302606,  0.8179262 ,
        -0.98861413, -1.37164055,  0.04710424],
       [ 0.35452202, -1.77008598,  0.05821616, -0.10157081, -0.0177392 ,
        -0.80556969, -0.644111  ,  1.62028286],
       [ 0.12565102,  0.83290996,  1.06604483,  0.88367563,  1.82724499,
        -0.625139  , -1.00688955, -1.31627874],
       [-0.81109591,  0.96967537,  0.75956849, -0.80162364,  0.44827989,
         2.05341272, -1.7788185 ,  0.79750866],
       [-0.29012385,  0.60888444, -1.05643106,  0.17126915, -2.24288296,
         0.30024122, -0.44027555,  2.33346992]])
stock_change.T
array([[-0.5889853 , -0.99235101, -1.3326412 , -0.6141568 ,  0.63699391,
         0.05821616,  1.82724499, -1.7788185 ],
       [-0.93535336,  0.70940651, -1.01831572,  0.34004292,  0.54513968,
        -0.10157081, -0.625139  ,  0.79750866],
       [-0.0675299 , -0.66934701, -1.09038797,  0.53421929, -1.45114066,
        -0.0177392 , -1.00688955, -0.29012385],
       [-0.72403031, -0.33760595,  0.35145548,  0.93273841,  0.12302606,
        -0.80556969, -1.31627874,  0.60888444],
       [-0.86873673,  1.75578837, -1.52407572, -2.39181704,  0.8179262 ,
        -0.644111  , -0.81109591, -1.05643106],
       [ 0.2366924 , -0.17527665,  1.24380911,  0.73553393, -0.98861413,
         1.62028286,  0.96967537,  0.17126915],
       [ 0.91934989,  1.127985  ,  0.53576183, -0.37974303, -1.37164055,
         0.12565102,  0.75956849, -2.24288296],
       [ 0.95733701,  1.16480656, -0.87476354,  0.12472411,  0.04710424,
         0.83290996, -0.80162364,  0.30024122],
       [ 0.7036584 ,  0.60324222,  0.94061172,  0.31243104,  0.35452202,
         1.06604483,  0.44827989, -0.44027555],
       [-0.25490962,  2.220063  , -1.54695238, -0.09388522, -1.77008598,
         0.88367563,  2.05341272,  2.33346992]])
stock_change.dtype
dtype('float64')
stock_change.astype(np.float32)
array([[-0.5889853 , -0.93535334, -0.0675299 , -0.7240303 , -0.86873674,
         0.2366924 ,  0.9193499 ,  0.957337  ,  0.7036584 , -0.25490963],
       [-0.992351  ,  0.7094065 , -0.669347  , -0.33760595,  1.7557883 ,
        -0.17527665,  1.127985  ,  1.1648066 ,  0.6032422 ,  2.220063  ],
       [-1.3326412 , -1.0183157 , -1.090388  ,  0.35145548, -1.5240757 ,
         1.2438091 ,  0.53576183, -0.87476355,  0.9406117 , -1.5469524 ],
       [-0.6141568 ,  0.34004292,  0.53421926,  0.9327384 , -2.391817  ,
         0.73553395, -0.37974304,  0.12472411,  0.31243104, -0.09388521],
       [ 0.6369939 ,  0.5451397 , -1.4511406 ,  0.12302607,  0.8179262 ,
        -0.98861414, -1.3716406 ,  0.04710424,  0.35452202, -1.7700859 ],
       [ 0.05821616, -0.10157081, -0.0177392 , -0.8055697 , -0.644111  ,
         1.6202829 ,  0.12565102,  0.83290994,  1.0660448 ,  0.88367563],
       [ 1.827245  , -0.625139  , -1.0068896 , -1.3162787 , -0.8110959 ,
         0.96967536,  0.7595685 , -0.80162364,  0.4482799 ,  2.0534127 ],
       [-1.7788185 ,  0.79750866, -0.29012385,  0.60888445, -1.056431  ,
         0.17126915, -2.242883  ,  0.3002412 , -0.44027555,  2.3334699 ]],
      dtype=float32)

数组的运算

# 逻辑运算
stock_change
stock_change1=np.copy(stock_change)
stock_change>0.5 # 逻辑判断, 如果涨跌幅大于0.5就标记为True 否则为False:
array([[False, False, False, False, False, False,  True,  True,  True,
        False],
       [False,  True, False, False,  True, False,  True,  True,  True,
         True],
       [False, False, False, False, False,  True,  True, False,  True,
        False],
       [False, False,  True,  True, False,  True, False, False, False,
        False],
       [ True,  True, False, False,  True, False, False, False, False,
        False],
       [False, False, False, False, False,  True, False,  True,  True,
         True],
       [ True, False, False, False, False,  True,  True, False, False,
         True],
       [False,  True, False,  True, False, False, False, False, False,
         True]])
stock_change1[stock_change1>0.5]=1  # 如果涨跌幅大于0.5就把值改为1
stock_change1
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
         0.2366924 ,  1.        ,  1.        ,  1.        , -0.25490962],
       [-0.99235101,  1.        , -0.66934701, -0.33760595,  1.        ,
        -0.17527665,  1.        ,  1.        ,  1.        ,  1.        ],
       [-1.3326412 , -1.01831572, -1.09038797,  0.35145548, -1.52407572,
         1.        ,  1.        , -0.87476354,  1.        , -1.54695238],
       [-0.6141568 ,  0.34004292,  1.        ,  1.        , -2.39181704,
         1.        , -0.37974303,  0.12472411,  0.31243104, -0.09388522],
       [ 1.        ,  1.        , -1.45114066,  0.12302606,  1.        ,
        -0.98861413, -1.37164055,  0.04710424,  0.35452202, -1.77008598],
       [ 0.05821616, -0.10157081, -0.0177392 , -0.80556969, -0.644111  ,
         1.        ,  0.12565102,  1.        ,  1.        ,  1.        ],
       [ 1.        , -0.625139  , -1.00688955, -1.31627874, -0.81109591,
         1.        ,  1.        , -0.80162364,  0.44827989,  1.        ],
       [-1.7788185 ,  1.        , -0.29012385,  1.        , -1.05643106,
         0.17126915, -2.24288296,  0.30024122, -0.44027555,  1.        ]])
stock_change
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031, -0.86873673,
         0.2366924 ,  0.91934989,  0.95733701,  0.7036584 , -0.25490962],
       [-0.99235101,  0.70940651, -0.66934701, -0.33760595,  1.75578837,
        -0.17527665,  1.127985  ,  1.16480656,  0.60324222,  2.220063  ],
       [-1.3326412 , -1.01831572, -1.09038797,  0.35145548, -1.52407572,
         1.24380911,  0.53576183, -0.87476354,  0.94061172, -1.54695238],
       [-0.6141568 ,  0.34004292,  0.53421929,  0.93273841, -2.39181704,
         0.73553393, -0.37974303,  0.12472411,  0.31243104, -0.09388522],
       [ 0.63699391,  0.54513968, -1.45114066,  0.12302606,  0.8179262 ,
        -0.98861413, -1.37164055,  0.04710424,  0.35452202, -1.77008598],
       [ 0.05821616, -0.10157081, -0.0177392 , -0.80556969, -0.644111  ,
         1.62028286,  0.12565102,  0.83290996,  1.06604483,  0.88367563],
       [ 1.82724499, -0.625139  , -1.00688955, -1.31627874, -0.81109591,
         0.96967537,  0.75956849, -0.80162364,  0.44827989,  2.05341272],
       [-1.7788185 ,  0.79750866, -0.29012385,  0.60888444, -1.05643106,
         0.17126915, -2.24288296,  0.30024122, -0.44027555,  2.33346992]])
np.all(stock_change[0:2,0:5]>0)
False
np.any(stock_change[0:2,0:5]>0)
True
temp=stock_change[0:4,0:4]
np.where(temp>0,1,0)
array([[0, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 0, 1],
       [0, 1, 1, 1]])
np.where(np.logical_and(temp > 0.5, temp < 1), 1, 0)
array([[0, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 1, 1]])
np.where(np.logical_or(temp > 0.5, temp < -0.5), 1, 0)
array([[1, 1, 0, 1],
       [1, 1, 1, 0],
       [1, 1, 1, 0],
       [1, 0, 1, 1]])
temp
array([[-0.5889853 , -0.93535336, -0.0675299 , -0.72403031],
       [-0.99235101,  0.70940651, -0.66934701, -0.33760595],
       [-1.3326412 , -1.01831572, -1.09038797,  0.35145548],
       [-0.6141568 ,  0.34004292,  0.53421929,  0.93273841]])
np.max(temp)
0.9327384109856901
np.max(temp,axis=1)
array([-0.0675299 ,  0.70940651,  0.35145548,  0.93273841])
np.max(temp, axis=0)
array([-0.5889853 ,  0.70940651,  0.53421929,  0.93273841])
np.std(temp,axis=1)
array([0.320048  , 0.63915226, 0.65926327, 0.56843862])
np.argmax(temp,axis=1)
array([2, 1, 3, 3])

数组间的运算

arr=np.array([[1,2,3,4],[5,6,7,8]])
arr
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
arr+10
array([[11, 12, 13, 14],
       [15, 16, 17, 18]])
arr*10
array([[10, 20, 30, 40],
       [50, 60, 70, 80]])
arr/10
array([[0.1, 0.2, 0.3, 0.4],
       [0.5, 0.6, 0.7, 0.8]])
### 矩阵的计算
a = np.array([[80, 86],
[82, 80],
[85, 78],
[90, 90],
[86, 82],
[82, 90],
[78, 80],
[92, 94]])
b = np.array([[0.7], [0.3]])
a_mat=np.mat(a)
a_mat
matrix([[80, 86],
        [82, 80],
        [85, 78],
        [90, 90],
        [86, 82],
        [82, 90],
        [78, 80],
        [92, 94]])
np.matmul(a,b)
array([[81.8],
       [81.4],
       [82.9],
       [90. ],
       [84.8],
       [84.4],
       [78.6],
       [92.6]])
np.dot(a,b)
array([[81.8],
       [81.4],
       [82.9],
       [90. ],
       [84.8],
       [84.4],
       [78.6],
       [92.6]])
a@b
array([[81.8],
       [81.4],
       [82.9],
       [90. ],
       [84.8],
       [84.4],
       [78.6],
       [92.6]])
b_mat=np.mat(b)
a_mat*b_mat
matrix([[81.8],
        [81.4],
        [82.9],
        [90. ],
        [84.8],
        [84.4],
        [78.6],
        [92.6]])

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,875评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,569评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,475评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,459评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,537评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,563评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,580评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,326评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,773评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,086评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,252评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,921评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,566评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,190评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,435评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,129评论 2 366
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,125评论 2 352