numpy

导入numpy并查看版本

In [2]:
import numpy as np 
np.__version__
Out[2]:
'1.13.1'

什么是numpy?

Numpy即Numeric Python,python经过扩展可以支持数组和矩阵类型,并且具有大量的函数可以计算这些数组和矩阵。这些数组一般是多维的,而这个扩展的程序包就是numpy。

【注】是数据分析和机器学习中最基本的工具,后面许多API和工具都是建立在他得基础上,如:pandas、scipy、matplotlib等

一、创建ndarray

numpy中最基础数据结构就是ndarray:即数组

1. 使用np.array()由python list创建

In [3]:
data = [1,2,3]
nd = np.array(data)
nd
Out[3]:
array([1, 2, 3])
In [3]:
data
Out[3]:
[1, 2, 3]
In [4]:
type(nd),type(data)
Out[4]:
(numpy.ndarray, list)
In [5]:
nd.dtype # 数组中的元素的类型
Out[5]:
dtype('int32')
In [8]:
nd2 = np.array([1,2,3,4.5,True,"1234"])
nd2
Out[8]:
array(['1', '2', '3', '4.5', 'True', '1234'],
      dtype='<U32')
In [7]:
nd2.dtype
Out[7]:
dtype('float64')

注意:1、数组中所有的元素类型相同 2、如果通过列表来创建的时候,列表中元素不一样,会被统一成某种类型(优先级:str>float>int)

In [9]:
# 注意:图片也可在numpy里面用数组来表示
# 引入一张图片
import matplotlib.pyplot as plt # 这个是用于处理数据可视化的一个工具
In [165]:
girl = plt.imread("./source/girl.jpg")
girl # 彩色图片是一个三维的数组
Out[165]:
array([[[225, 231, 231],
        [229, 235, 235],
        [222, 228, 228],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [217, 220, 173]],

       [[224, 230, 230],
        [229, 235, 235],
        [223, 229, 229],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [217, 220, 173]],

       [[224, 230, 230],
        [229, 235, 235],
        [223, 229, 229],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [219, 221, 174]],

       ..., 
       [[175, 187, 213],
        [180, 192, 218],
        [175, 187, 213],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [156, 163, 181]],

       [[175, 187, 213],
        [180, 192, 218],
        [174, 186, 212],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [155, 162, 180]],

       [[177, 189, 215],
        [181, 193, 219],
        [174, 186, 212],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [156, 163, 181]]], dtype=uint8)
In [11]:
girl.shape # 数据的形状
Out[11]:
(900, 1440, 3)
In [13]:
plt.imshow(girl)
plt.show()
 
               
In [14]:
boy = np.array([[0.21,0.14,0.12343,0.14],
                [0.21,0.14,0.12343,0.14],
                [0.21,0.14,0.12343,0.14],
               ])
In [17]:
plt.imshow(boy,cmap="gray")
plt.show()
 
               
In [19]:
g = girl[:200,:300]
In [20]:
plt.imshow(g)
plt.show()
In [ ]:

2. 使用np的routines函数创建

1)np.ones(shape,dtype=None,order='C')

In [26]:
# 参数shape数据的形状传一个元组,元组的第0个元素代表第0维中的元素个数,依次类推
np.ones((3,2,4,5,6,7,8))
 
               
In [28]:
ones
Out[28]:
array([[[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        ..., 
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        ..., 
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        ..., 
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       ..., 
       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        ..., 
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        ..., 
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]],

       [[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        ..., 
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]]])
In [27]:
ones = np.ones((168,59,3))
plt.imshow(ones)
plt.show()
In [29]:
ones[:,:,1:] = 0 # 数组是可以切片赋值的
In [34]:
ones[:,:,0] = 0.3
In [35]:
ones
Out[35]:
array([[[ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        ..., 
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ]],

       [[ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        ..., 
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ]],

       [[ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        ..., 
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ]],

       ..., 
       [[ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        ..., 
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ]],

       [[ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        ..., 
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ]],

       [[ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        ..., 
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ],
        [ 0.3,  0. ,  0. ]]])
In [36]:
plt.imshow(ones)
plt.show()
In [ ]:

2)np.zeros(shape,dtype="float",order="C")

In [40]:
np.zeros((2,3),dtype="int32")
Out[40]:
array([[0, 0, 0],
       [0, 0, 0]])

3)np.full(shape,fill_value,dtype=None)

In [43]:
np.full((3,4),12,dtype="float")
Out[43]:
array([[ 12.,  12.,  12.,  12.],
       [ 12.,  12.,  12.,  12.],
       [ 12.,  12.,  12.,  12.]])

4)np.eye(N,M,k=0,dtype='float')

In [44]:
np.eye(4)
Out[44]:
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

5)np.linspace(start,stop,num=50)

In [49]:
np.linspace(0,10,10)
Out[49]:
array([  0.        ,   1.11111111,   2.22222222,   3.33333333,
         4.44444444,   5.55555556,   6.66666667,   7.77777778,
         8.88888889,  10.        ])
In [50]:
np.logspace(1,100,10)
Out[50]:
array([  1.00000000e+001,   1.00000000e+012,   1.00000000e+023,
         1.00000000e+034,   1.00000000e+045,   1.00000000e+056,
         1.00000000e+067,   1.00000000e+078,   1.00000000e+089,
         1.00000000e+100])
In [ ]:

6)np.arange([start,]stop,[step,]dtype=None) "[]"中是可选项

In [54]:
np.arange(0,100,5)
Out[54]:
array([ 0,  5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
       85, 90, 95])

7)np.random.randint(low,high=None,size=None,dtype='I')

In [67]:
np.random.randint(0,10,size=(10,5)) # low下限 high上限 size形状(元组)
Out[67]:
array([[0, 7, 8, 1, 2],
       [7, 5, 5, 6, 4],
       [0, 3, 5, 8, 8],
       [7, 8, 3, 4, 0],
       [0, 7, 7, 3, 3],
       [1, 3, 3, 1, 4],
       [2, 2, 5, 9, 3],
       [3, 0, 9, 7, 0],
       [9, 8, 4, 1, 8],
       [0, 8, 8, 0, 3]])

8)np.random.randn(d0,d1,...,dn) 从第一维度到第n维度生成一个数组,数组中的数字符合标准正态分布

In [70]:
np.random.randn(5,6,10) # N(0,1)
Out[70]:
array([[[-0.09358215, -1.49899141,  1.98769631,  1.51193985, -0.10427579,
         -0.39974708, -0.0359005 ,  0.73853509,  1.57513744,  0.2359057 ],
        [-0.7041393 ,  0.5482207 , -1.47905978, -1.86071464,  0.24794821,
         -1.85676   ,  0.10820599, -0.32210508,  0.22393499, -0.66522583],
        [ 1.60692141,  1.08030533, -0.82684758,  0.11892321,  1.60067252,
          0.78420728, -1.02287912,  0.13980319, -0.35365909,  1.89048322],
        [-0.79423431, -1.13066957,  0.29044997,  1.83286616,  1.01731098,
          0.36688754, -0.78547307, -0.03262272, -1.63958452,  0.52841964],
        [ 0.63854724, -0.83270504,  2.33109623, -2.0670137 , -1.10680899,
          0.06584844,  0.39706437,  0.55065026, -0.59335951,  0.22210873],
        [ 0.41064818,  1.68676676,  0.30244373,  0.0282829 ,  0.19600649,
         -0.68277526,  0.06810738,  0.53271374, -0.4204973 ,  0.71823602]],

       [[-0.32284878, -0.76235291,  0.98673175,  1.91000432,  0.76937097,
          0.16359419, -1.17058448,  0.73993218,  1.29904232, -1.41618621],
        [-0.37633528,  0.07615863,  0.37582282, -1.63055539,  1.37079158,
         -0.61601884,  0.39195051,  1.39733195, -0.60085489,  0.04125237],
        [ 0.26121747,  0.4540636 , -0.94938301, -1.44742311, -0.46626518,
          0.47067078, -1.49014315, -0.10415939,  2.05483157,  0.25061365],
        [-0.67645451, -0.78386469,  0.9837996 ,  1.10329857, -0.63704624,
          1.36715299,  1.3569239 , -0.72635522,  0.51392834,  0.82614783],
        [-0.23384279, -1.28411627, -0.05939828,  0.44944251,  1.189167  ,
          0.87113199,  0.53838983,  0.41908957, -0.74518935,  1.16339158],
        [-0.18668839, -0.4712675 , -0.06185288,  0.8182108 , -1.18638766,
         -1.73182272, -0.19059271, -0.11019542, -0.45788589,  1.6559319 ]],

       [[ 0.13107506,  0.09003381, -0.04046429, -0.87251826, -2.50623334,
         -0.06766936,  0.87597133, -1.53979928, -0.93540774,  0.45923245],
        [ 0.47169492,  0.16740181,  0.46952713,  1.19222423, -1.3851465 ,
          1.99008688,  1.50716764, -0.89799582, -0.05838133,  0.40384442],
        [-0.54836846, -0.42615348, -0.5839804 ,  0.03183017, -0.18666256,
          0.26916727, -0.20821308,  0.65122173, -0.21675408, -0.93981481],
        [ 0.39832431, -1.3087661 , -0.58729167,  1.12946505,  0.32596195,
         -0.07288169, -0.60721266,  2.33748491, -1.43005908, -0.99483587],
        [ 1.08070407,  1.06087286,  0.53105574,  0.15470968,  0.56741346,
         -0.43695276, -0.51750765,  1.13183184,  0.49298563, -0.87541634],
        [-2.06517303, -0.0877284 , -1.22371533, -1.59688986, -0.4547517 ,
         -0.40808438, -0.35837045, -1.54584721, -0.19055862, -1.0929987 ]],

       [[ 0.75912634,  0.67984287,  0.86651616,  1.44612227, -0.50480375,
         -0.86431858, -1.49997546,  0.60060329, -0.76905209, -0.1248285 ],
        [-0.08862881,  0.79658324, -0.54596008, -0.47540929, -1.45907461,
          0.07354412, -1.26479651,  1.40530143,  0.60891271,  1.07599106],
        [ 0.44097311,  0.10543125, -1.64720857, -0.49178975, -0.40010248,
          0.60104546,  0.61886559,  0.09964341, -0.07654484,  0.01466248],
        [ 0.38690965, -0.64976725, -0.88243344,  0.43953291,  0.42834801,
          0.23491374, -0.18973705,  1.27917483,  0.82983269,  0.79430021],
        [ 0.91834613,  1.12247777,  0.08971992, -0.45914651,  0.79840117,
          1.23251681, -0.12130413, -0.13878377, -0.09306616,  0.43146146],
        [-0.86534235,  1.20442112,  0.80460288,  0.23146194, -1.37131063,
          0.39225933, -2.64398314, -0.37121153, -0.98828702, -0.48872689]],

       [[-0.95197633,  0.40663767,  0.30596473, -1.13367237,  0.56080945,
         -0.18929488, -1.00550724,  1.82465218,  0.97053687, -0.18880776],
        [ 0.56076673,  0.45680001,  0.04403547, -0.33633002,  1.05787698,
         -0.95026294,  1.55784308, -0.04700079, -1.13309294, -1.52973685],
        [-1.05473426, -0.35119816, -0.41996786, -1.41545262,  1.37013952,
          0.44812898,  0.44133247, -0.69188934, -1.6819927 ,  0.03398214],
        [ 0.2265335 , -1.92836525, -0.31944456, -0.37962678, -0.58608731,
          1.32122153,  1.55326426,  0.49838209,  0.45043623, -1.54473891],
        [ 0.97935231, -0.42807334, -1.29984072,  0.23391228, -1.18660991,
         -0.19844072, -1.83693156, -0.87926028, -1.41108227, -1.66544272],
        [ 1.51680404, -0.90072061, -1.30384261,  1.46910357,  1.55454131,
         -0.14601832,  0.15141248, -1.87883605,  0.0924951 ,  1.28106374]]])

9)np.random.normal(loc=0.0,scale=1.0,size=None)

In [73]:
np.random.normal(450,100,size=(90,10))
Out[73]:
array([[ 336.75387404,  391.65683168,  498.76644615,  386.17886947,
         327.48245651,  527.80674828,  539.26940105,  369.64475214,
         415.14180776,  387.8046801 ],
       [ 436.64172537,  512.42693445,  509.03160498,  428.37233551,
         384.03217165,  450.93849607,  490.15376541,  603.00894225,
         494.53309375,  481.88682217],
       [ 385.30889406,  476.28508918,  490.30271889,  473.79751503,
         567.20252837,  397.00172353,  412.19518771,  470.26351719,
         585.94598243,  406.30114867],
       [ 662.51560347,  455.63555885,  335.75668217,  553.51109653,
         527.50580761,  418.68255566,  433.27222119,  355.5035559 ,
         552.79303757,  365.12473145],
       [ 545.41809506,  542.81120741,  418.08540191,  502.08035002,
         489.61540484,  603.48703621,  442.5173826 ,  310.62586184,
         397.59667339,  407.18629259],
       [ 536.62705543,  373.17087601,  387.53239657,  493.50100186,
         434.69796839,  390.03931656,  443.45656768,  388.89231904,
         424.15132003,  540.91910513],
       [ 440.52306579,  391.71451744,  437.55706601,  469.27630621,
         406.99478958,  362.44118414,  536.48731847,  572.90132295,
         481.58805791,  478.47964791],
       [ 385.14707894,  469.12943309,  305.70922198,  460.74707662,
         502.95838503,  431.71240517,  342.7916848 ,  407.50858253,
         290.838901  ,  527.05185043],
       [ 385.05812408,  441.5621423 ,  479.94238267,  475.70258941,
         548.79318074,  339.69251585,  457.66895693,  407.6839961 ,
         453.96735266,  355.04312047],
       [ 240.57346971,  462.02119378,  557.47031693,  365.5621482 ,
         421.48731128,  337.17012275,  538.99367394,  291.96240587,
         583.40810146,  392.68739514],
       [ 262.77027625,  389.19618291,  431.91418188,  518.30939231,
         535.60696944,  265.01106923,  205.66965664,  477.7160443 ,
         402.08998341,  461.94465525],
       [ 357.43465369,  439.3195828 ,  253.53609204,  511.6280141 ,
         391.66839943,  381.00739543,  394.91400566,  532.95515557,
         483.49393099,  374.07144475],
       [ 315.81211778,  421.4360574 ,  373.72983086,  382.76578134,
         324.43190939,  426.33506066,  380.28268705,  397.3292164 ,
         329.78748304,  503.23789543],
       [ 285.70801477,  469.69661703,  445.8417405 ,  551.44664511,
         369.90599608,  358.48253945,  402.7896672 ,  573.84596855,
         419.84192647,  460.71713048],
       [ 580.08121766,  450.31393694,  382.84384675,  585.53508672,
         460.57849068,  405.79733799,  171.29825334,  456.17549381,
         525.86256043,  412.1363241 ],
       [ 461.60455293,  151.86396147,  415.79059815,  478.80770885,
         303.21793632,  468.37932735,  448.89482993,  597.60308308,
         398.06184732,  469.54027055],
       [ 633.05994548,  367.54906343,  586.66431898,  532.71822618,
         384.30045719,  471.01613892,  587.02823268,  408.6937843 ,
         490.70406224,  301.23699981],
       [ 557.01114   ,  478.46470521,  359.19499266,  387.36093877,
         392.86815293,  547.70014298,  592.9733081 ,  430.08036393,
         503.47793168,  421.89404986],
       [ 506.10167685,  439.12995299,  494.33281733,  345.60884943,
         462.86859726,  434.69600809,  389.5733517 ,  559.97030485,
         469.61006439,  470.99794124],
       [ 400.74372025,  377.36611363,  440.39145837,  546.22740815,
         446.26322926,  554.6736657 ,  583.22012567,  592.92070545,
         538.93499161,  282.85180835],
       [ 487.93671662,  141.59169637,  439.00168061,  451.57280006,
         545.33727124,  374.18559104,  425.96319208,  535.5891261 ,
         412.10195375,  376.20679553],
       [ 328.29975372,  437.43640405,  267.43331932,  417.88751835,
         623.61080152,  581.53803664,  500.60264026,  357.12209808,
         430.17787952,  541.08416585],
       [ 472.62011773,  456.44480533,  333.70041343,  575.68893734,
         483.15426987,  414.06087041,  442.73896679,  476.77790667,
         557.7860946 ,  602.44875414],
       [ 402.43823668,  367.76123377,  434.69216975,  393.08813671,
         584.32419951,  360.853422  ,  555.40311867,  450.11737225,
         318.99533699,  253.97578605],
       [ 390.31529063,  568.87396775,  551.58155749,  498.13129547,
         656.60712073,  517.22802125,  376.85580205,  296.18382599,
         437.453069  ,  507.81430915],
       [ 472.41212452,  333.78376315,  320.0178681 ,  539.43156964,
         412.89365481,  350.07191578,  472.15131164,  496.32800658,
         362.46820011,  399.043498  ],
       [ 563.27488787,  544.29387737,  364.05194481,  381.92267492,
         458.74888938,  437.37408036,  375.25486806,  435.20877258,
         568.03557044,  407.30568794],
       [ 656.1503502 ,  310.95317562,  495.12074898,  528.93019423,
         286.4628272 ,  327.45044748,  508.11552734,  311.80841338,
         460.5253883 ,  516.5762789 ],
       [ 420.43753082,  386.78902839,  555.86664412,  361.12743763,
         532.04777745,  547.14759241,  457.65703769,  581.46595806,
         345.43303389,  436.91156777],
       [ 383.12409576,  405.42886366,  456.65786045,  430.52458332,
         264.60953026,  386.5731847 ,  515.56284123,  473.60876973,
         483.56760088,  427.29662144],
       [ 299.09917474,  298.90384684,  313.14909255,  485.09782479,
         359.66573231,  528.76712485,  372.6380624 ,  496.60810494,
         331.09851695,  293.35089898],
       [ 562.00358333,  402.43286237,  533.74183027,  297.93570041,
         429.96840678,  458.26365946,  338.38340497,  397.53714394,
         398.22398763,  356.31445468],
       [ 404.77436667,  384.16332639,  517.37154695,  376.1193488 ,
         528.47036871,  310.88788993,  474.88444046,  599.45941831,
         159.65492388,  513.85166262],
       [ 586.79169996,  315.1436137 ,  571.37198315,  401.64412536,
         499.14806795,  353.17259466,  346.85185868,  441.90230762,
         399.66471328,  359.00340789],
       [ 554.53046397,  588.65848933,  158.20879966,  300.23079482,
         477.31516759,  422.05477671,  262.69791109,  226.91873827,
         347.23289159,  509.05077155],
       [ 563.92242222,  379.17608475,  371.22672165,  471.86392331,
         332.70531436,  267.45904342,  466.03347237,  413.37683235,
         639.31512283,  489.67462391],
       [ 422.5460759 ,  423.98173223,  380.79628393,  395.64056863,
         526.35578902,  415.15127071,  292.4837271 ,  357.0199034 ,
         576.95240606,  337.70511632],
       [ 323.6864567 ,  439.03720616,  356.49820378,  379.98997979,
         491.69294389,  452.94410429,  434.60975151,  422.65937394,
         575.07419808,  346.5540995 ],
       [ 363.05583763,  519.22694589,  555.72713564,  521.18823145,
         486.64236301,  362.99434703,  508.12612162,  416.08365947,
         355.96065708,  544.18346451],
       [ 532.99878409,  453.0085492 ,  600.56294399,  396.98757863,
         503.14897901,  247.90875336,  496.14510599,  410.53653883,
         441.41998089,  392.91958738],
       [ 415.66468609,  687.49147825,  475.50418028,  423.85438642,
         578.17001518,  241.02835169,  516.94252816,  572.80451212,
         368.70643657,  435.50322231],
       [ 467.78924383,  409.2163454 ,  391.59584661,  653.29582704,
         417.34844942,  404.70467089,  372.8178365 ,  527.88773237,
         397.43735984,  557.50217064],
       [ 492.38799154,  556.77439415,  409.99956099,  407.8930006 ,
         472.68349636,  631.49797654,  403.78552361,  633.68004476,
         567.5998023 ,  423.75540134],
       [ 350.1296884 ,  366.95278366,  496.76113889,  303.47862938,
         610.48187848,  276.91017664,  358.4482971 ,  530.03442731,
         331.05649739,  428.44382145],
       [ 302.98888858,  333.32300577,  348.19877722,  332.07587914,
         385.85714973,  581.79531664,  619.64409008,  420.18390534,
         543.04405937,  207.28757858],
       [ 523.43849515,  525.24403129,  400.84224262,  391.87383153,
         455.72001982,  403.02339669,  533.58746556,  480.31046377,
         566.23280914,  531.6247242 ],
       [ 546.72124222,  266.81594383,  504.1512776 ,  399.49133501,
         394.6952917 ,  582.93328833,  353.94079536,  383.81121652,
         299.87475231,  544.07238942],
       [ 430.91197558,  576.5203868 ,  286.03048542,  438.30897557,
         412.04042185,  353.10917543,  413.63772975,  647.14038542,
         591.25455248,  556.45493415],
       [ 334.92019753,  767.80389948,  381.982054  ,  234.44691863,
         391.31525815,  415.83189699,  336.99750123,  358.8042095 ,
         381.00677442,  488.71361319],
       [ 704.62956747,  563.13759423,  468.97808849,  366.55222553,
         352.5096905 ,  481.97065088,  391.47128438,  513.20375963,
         462.6762823 ,  242.05034098],
       [ 202.21029285,  367.3616573 ,  339.35849879,  448.42933906,
         545.00966775,  427.02261553,  582.71591752,  332.08112035,
         537.13389779,  448.60400089],
       [ 352.3626168 ,  370.17395382,  537.06004889,  360.70509766,
         463.74176473,  380.54643498,  402.61484566,  636.45985458,
         564.3316482 ,  440.59228808],
       [ 387.32355487,  357.56699865,  470.26784266,  487.17456386,
         490.82817128,  441.86735025,  380.3326202 ,  354.96961139,
         270.70095169,  292.08762615],
       [ 625.32154002,  464.92079704,  571.73110861,  480.93378021,
         399.57290238,  509.90861872,  373.5489584 ,  598.89819313,
         433.59308674,  488.8606805 ],
       [ 392.145978  ,  354.88786541,  540.75265742,  381.81679892,
         425.6351992 ,  362.17580821,  585.83057712,  511.53531085,
         653.8656794 ,  422.62056854],
       [ 232.81061492,  199.09341681,  529.57896022,  448.93288877,
         650.60507882,  530.00122388,  418.58174858,  529.24922256,
         470.39556704,  558.03710062],
       [ 495.79375113,  572.88253659,  413.35545587,  587.07280359,
         316.14414103,  443.69709127,  527.5582951 ,  517.65936524,
         476.80660548,  444.71044519],
       [ 271.75477577,  536.86008145,  408.810841  ,  491.06158182,
         392.34342951,  333.40552142,  435.98402952,  494.12765306,
         302.25867882,  413.16910196],
       [ 490.09581074,  654.15641547,  682.55637762,  516.74030117,
         533.25353608,  338.46981277,  368.4183214 ,  506.66488095,
         360.45027068,  377.36214527],
       [ 375.81469355,  476.89089893,  606.8629692 ,  518.68947468,
         467.74276059,  529.43810778,  498.40564011,  387.85758575,
         633.6556068 ,  445.90576367],
       [ 478.75637022,  502.83329922,  450.04894959,  216.72545155,
         599.93872877,  547.87948275,  415.38509935,  442.78702762,
         596.67699753,  524.21126813],
       [ 476.9277205 ,  356.55711545,  543.3025581 ,  423.16062654,
         474.64864836,  469.54505304,  456.13139638,  654.4820455 ,
         446.91303815,  432.77582977],
       [ 538.92672353,  442.13955141,  274.6244844 ,  443.04907538,
         298.87533339,  553.86182502,  534.10189334,  515.00164259,
         539.4139928 ,  617.8140593 ],
       [ 413.7268355 ,  398.93574269,  490.48668015,  428.99554965,
         419.53672363,  578.58683823,  447.49931945,  506.75696192,
         680.9650155 ,  390.91275773],
       [ 635.38883004,  606.62387273,  436.02445671,  456.31083202,
         562.3119456 ,  700.01303405,  528.83258492,  347.8618614 ,
         329.94310665,  524.72270227],
       [ 492.09063878,  437.79015883,  334.83244662,  423.81213355,
         514.58462846,  354.98881028,  496.54269628,  380.12079065,
         692.68634957,  131.92190412],
       [ 541.55462662,  486.58439816,  350.03725702,  343.92063402,
         518.45160613,  525.25232862,  500.30257393,  505.9165192 ,
         506.27762214,  558.51972707],
       [ 148.76076333,  359.30892236,  352.79721959,  359.75849769,
         565.75769694,  334.55845213,  308.24224061,  585.54618549,
         376.29403958,  297.30109537],
       [ 548.50524478,  408.22541498,  555.07723848,  574.53824632,
         364.96537099,  413.13696862,  272.37723348,  449.16127374,
         297.53516467,  597.15942575],
       [ 385.72806427,  534.71300808,  387.40318991,  651.15217971,
         291.23741164,  384.2158285 ,  315.09068148,  442.45961467,
         492.27647909,  460.69605376],
       [ 629.90550506,  343.950831  ,  301.3552114 ,  529.69630767,
         487.02992895,  331.85191798,  490.9980054 ,  472.38865483,
         400.74688824,  352.32038419],
       [ 517.93709105,  553.03631218,  400.54896818,  440.90917696,
         442.99725421,  793.65481379,  358.42170346,  360.83078563,
         538.4462266 ,  368.49753524],
       [ 433.24211466,  415.34884469,  459.94103542,  520.0266389 ,
         398.24921251,  341.16645897,  500.68633514,  348.70809112,
         416.59370821,  584.08917009],
       [ 560.89716988,  206.92628065,  471.89949533,  347.5433062 ,
         350.22630392,  365.63062474,  415.57318944,  523.97103689,
         438.24598122,  370.92473504],
       [ 328.53756503,  543.60268089,  409.228765  ,  491.48612273,
         345.5301853 ,  501.44253259,  509.53835998,  405.86788979,
         440.38751232,  403.25486474],
       [ 512.98340321,  542.13411813,  384.32797904,  387.20702584,
         526.65405353,  473.49530246,  424.72977929,  402.76052588,
         418.04609016,  293.54247962],
       [ 493.42547637,  445.14404001,  274.47062719,  596.01746299,
         594.45480972,  588.94045152,  432.9293811 ,  378.67675649,
         476.89453964,  692.10714854],
       [ 366.63429888,  403.84551935,  452.6876537 ,  608.19871786,
         630.29967233,  477.37191349,  460.2925561 ,  352.84800993,
         587.66833373,  533.66931725],
       [ 466.25915526,  362.95962698,  499.85218527,  494.03330708,
         517.5487912 ,  423.07948021,  501.7894371 ,  592.04494501,
         442.38605944,  361.78422153],
       [ 599.65545836,  502.59632122,  347.85646682,  536.34806351,
         524.99002225,  564.32573006,  597.54073454,  499.72179667,
         384.0447522 ,  357.66740199],
       [ 358.18364835,  614.55856299,  331.39749545,  544.96689132,
         387.95519893,  615.91556795,  407.39395531,  489.39168653,
         403.26857457,  443.29172626],
       [ 184.40429076,  337.95032486,  398.97442347,  651.46486733,
         474.16394589,  442.79188043,  471.39102921,  425.80834737,
         461.47353321,  405.39602659],
       [ 454.13908741,  454.987639  ,  600.81463963,  320.83575023,
         472.79988826,  328.59425693,  454.3570192 ,  471.06717183,
         468.69206013,  452.99588274],
       [ 397.96344693,  436.74684101,  405.53298363,  570.284488  ,
         574.11712298,  335.93003717,  617.50128227,  513.13196484,
         470.28696207,  409.31084751],
       [ 477.66303456,  175.25542754,  390.42799839,  475.04379466,
         425.05436348,  554.13137457,  585.63345916,  467.09842678,
         643.43489703,  467.54440723],
       [ 379.25207371,  440.59630172,  432.52563799,  531.3449845 ,
         433.56282293,  401.89999467,  523.46903089,  540.39581431,
         454.97304261,  593.69682356],
       [ 525.62434681,  500.4781448 ,  494.85280308,  253.04671084,
         343.2435028 ,  644.99356706,  431.55721944,  331.98020261,
         697.38278026,  450.62469306],
       [ 424.16997074,  569.15397041,  430.27006071,  419.31827785,
         526.10144916,  584.22185695,  448.46508265,  650.852166  ,
         642.39370537,  469.55896411],
       [ 465.29837453,  540.95881371,  444.14809838,  445.0962054 ,
         362.37844302,  258.74353808,  553.51502862,  490.18890815,
         441.69462936,  539.81971905],
       [ 604.58124468,  485.01384715,  436.1798742 ,  426.84935225,
         482.24847514,  497.68124061,  461.66380355,  521.21634603,
         471.30187429,  603.96281144]])

10)np.random.random(size=None)

In [74]:
np.random.random(size=10) # 生成0-1之间的浮点数
Out[74]:
array([ 0.67428546,  0.47222544,  0.20237801,  0.39959138,  0.13957155,
        0.0013156 ,  0.97975301,  0.38834253,  0.47289323,  0.58902421])

用随机数生成图片

In [78]:
boy = np.random.random((667,480,3))
plt.imshow(boy,cmap="gray")
plt.show()

二、ndarray的属性

数组的常用属性:

维度 ndim, 大小 size, 形状 shape, 元素类型 dtype, 每项大小 itemsize, 数据 data

In [79]:
tigger = plt.imread("./source/tigger.jpg")
tigger
Out[79]:
array([[[232, 228, 227],
        [232, 228, 227],
        [232, 228, 227],
        ..., 
        [221, 216, 212],
        [221, 216, 212],
        [221, 216, 212]],

       [[230, 226, 225],
        [231, 227, 226],
        [231, 227, 226],
        ..., 
        [221, 216, 212],
        [221, 216, 212],
        [221, 216, 212]],

       [[228, 224, 223],
        [229, 225, 224],
        [229, 225, 224],
        ..., 
        [221, 216, 212],
        [221, 216, 212],
        [221, 216, 212]],

       ..., 
       [[200, 195, 191],
        [207, 202, 198],
        [215, 210, 206],
        ..., 
        [159, 166, 182],
        [168, 175, 191],
        [157, 164, 180]],

       [[205, 200, 196],
        [202, 197, 193],
        [204, 199, 195],
        ..., 
        [147, 165, 179],
        [142, 161, 176],
        [136, 155, 170]],

       [[205, 200, 196],
        [202, 197, 193],
        [204, 199, 195],
        ..., 
        [147, 165, 179],
        [142, 161, 176],
        [136, 155, 170]]], dtype=uint8)
In [80]:
tigger.ndim
Out[80]:
3
In [81]:
girl.ndim
Out[81]:
3
In [84]:
nd
Out[84]:
array([1, 2, 3])
In [83]:
nd.size
Out[83]:
3
In [85]:
tigger.shape
Out[85]:
(786, 1200, 3)
In [88]:
nd.dtype
Out[88]:
dtype('int32')
In [89]:
tigger.dtype
Out[89]:
dtype('uint8')
In [86]:
nd.itemsize
Out[86]:
4
In [92]:
tigger.itemsize
Out[92]:
1
In [94]:
nd.data
Out[94]:
<memory at 0x000001C74AA22408>
In [93]:
tigger.data
Out[93]:
<memory at 0x000001C749E32A98>

三、ndarray的基本操作

1、索引

In [96]:
l = [1,3,4,6,7,8,0]
l[-1]
Out[96]:
0
In [98]:
nd = np.random.randint(0,4,size=4)
nd
Out[98]:
array([1, 2, 3, 1])
In [101]:
nd[0]
Out[101]:
1
In [104]:
lp = [[1,2,4],
    [4,5,6]
    ]
lp
Out[104]:
[[1, 2, 4], [4, 5, 6]]
In [107]:
lp[0][1]
Out[107]:
2
In [108]:
lp[0,1]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-56bfe0f81cf9> in <module>()
----> 1 lp[0,1]

TypeError: list indices must be integers or slices, not tuple

In [ ]:
In [109]:
nd = np.random.randint(0,10,size=(6,6))
nd
Out[109]:
array([[2, 7, 2, 9, 3, 3],
       [6, 8, 7, 6, 8, 0],
       [5, 8, 2, 5, 8, 5],
       [2, 7, 1, 2, 9, 6],
       [3, 7, 1, 0, 1, 2],
       [1, 9, 0, 0, 7, 8]])
In [110]:
nd[0]
Out[110]:
array([2, 7, 2, 9, 3, 3])
In [111]:
nd[0][1]
Out[111]:
7
In [112]:
nd[0,1] # 先访问第0个维度的第0个,再访问第1个维度中第1
Out[112]:
7
In [113]:
nd[1,2,3] # 维度不足3个,不这样访问
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-113-e998ed9d4f35> in <module>()
----> 1 nd[1,2,3]

IndexError: too many indices for array

In [114]:
nd[1,4] = 20000
nd
Out[114]:
array([[    2,     7,     2,     9,     3,     3],
       [    6,     8,     7,     6, 20000,     0],
       [    5,     8,     2,     5,     8,     5],
       [    2,     7,     1,     2,     9,     6],
       [    3,     7,     1,     0,     1,     2],
       [    1,     9,     0,     0,     7,     8]])
In [116]:
nd
Out[116]:
array([[    2,     7,     2,     9,     3,     3],
       [    6,     8,     7,     6, 20000,     0],
       [    5,     8,     2,     5,     8,     5],
       [    2,     7,     1,     2,     9,     6],
       [    3,     7,     1,     0,     1,     2],
       [    1,     9,     0,     0,     7,     8]])
In [118]:
nd[[1,1,1,1,1]]
nd[[1,4,5,2,0]] # 连续访问多个,可以把你要访问的那些下标写成一个列表
Out[118]:
array([[    6,     8,     7,     6, 20000,     0],
       [    3,     7,     1,     0,     1,     2],
       [    1,     9,     0,     0,     7,     8],
       [    5,     8,     2,     5,     8,     5],
       [    2,     7,     2,     9,     3,     3]])

2、切片

In [120]:
nd = np.random.randint(0,10,size=(4,4))
nd
Out[120]:
array([[7, 5, 7, 2],
       [7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [126]:
nd[0:5] # 区间左闭右开
Out[126]:
array([[7, 5, 7, 2],
       [7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [129]:
l[0:10]
Out[129]:
[1, 3, 4, 6, 7, 8, 0]
In [130]:
nd[1:]
Out[130]:
array([[7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [134]:
nd[::-1]
Out[134]:
array([[2, 1, 8, 2],
       [5, 9, 9, 4],
       [7, 5, 9, 6],
       [7, 5, 7, 2]])
In [135]:
nd
Out[135]:
array([[7, 5, 7, 2],
       [7, 5, 9, 6],
       [5, 9, 9, 4],
       [2, 1, 8, 2]])
In [138]:
nd[:,0:2]
Out[138]:
array([[7, 5],
       [7, 5],
       [5, 9],
       [2, 1]])

把girl调头

In [139]:
girl
Out[139]:
array([[[225, 231, 231],
        [229, 235, 235],
        [222, 228, 228],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [217, 220, 173]],

       [[224, 230, 230],
        [229, 235, 235],
        [223, 229, 229],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [217, 220, 173]],

       [[224, 230, 230],
        [229, 235, 235],
        [223, 229, 229],
        ..., 
        [206, 213, 162],
        [211, 213, 166],
        [219, 221, 174]],

       ..., 
       [[175, 187, 213],
        [180, 192, 218],
        [175, 187, 213],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [156, 163, 181]],

       [[175, 187, 213],
        [180, 192, 218],
        [174, 186, 212],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [155, 162, 180]],

       [[177, 189, 215],
        [181, 193, 219],
        [174, 186, 212],
        ..., 
        [155, 162, 180],
        [153, 160, 178],
        [156, 163, 181]]], dtype=uint8)
In [145]:
girl2 = girl[::-3]
In [146]:
plt.imshow(girl2)
plt.show()
In [166]:
girl3 = girl[::2,::-2]
plt.imshow(girl3)
plt.show()
In [167]:
girl4 = girl[:,:,::1]
girl4[:,:,:2] = 0
plt.imshow(girl4)
plt.show()
In [ ]:

3、变形

reshape()

resize()

In [170]:
tigger
Out[170]:
array([[[232, 228, 227],
        [232, 228, 227],
        [232, 228, 227],
        ..., 
        [221, 216, 212],
        [221, 216, 212],
        [221, 216, 212]],

       [[230, 226, 225],
        [231, 227, 226],
        [231, 227, 226],
        ..., 
        [221, 216, 212],
        [221, 216, 212],
        [221, 216, 212]],

       [[228, 224, 223],
        [229, 225, 224],
        [229, 225, 224],
        ..., 
        [221, 216, 212],
        [221, 216, 212],
        [221, 216, 212]],

       ..., 
       [[200, 195, 191],
        [207, 202, 198],
        [215, 210, 206],
        ..., 
        [159, 166, 182],
        [168, 175, 191],
        [157, 164, 180]],

       [[205, 200, 196],
        [202, 197, 193],
        [204, 199, 195],
        ..., 
        [147, 165, 179],
        [142, 161, 176],
        [136, 155, 170]],

       [[205, 200, 196],
        [202, 197, 193],
        [204, 199, 195],
        ..., 
        [147, 165, 179],
        [142, 161, 176],
        [136, 155, 170]]], dtype=uint8)
In [171]:
tigger.shape
Out[171]:
(786, 1200, 3)
In [172]:
nd = np.random.randint(0,20,size=12)
nd (1*12)
Out[172]:
array([ 2,  1,  0, 17, 18, 13, 11,  7,  8, 19,  2, 16])
In [173]:
nd.reshape((3,4))
Out[173]:
array([[ 2,  1,  0, 17],
       [18, 13, 11,  7],
       [ 8, 19,  2, 16]])
In [174]:
nd
Out[174]:
array([ 2,  1,  0, 17, 18, 13, 11,  7,  8, 19,  2, 16])
In [175]:
nd.reshape((4,4)) # 元素总数不一致,是不能相互转变的
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-175-133d72e119fa> in <module>()
----> 1 nd.reshape((4,4))

ValueError: cannot reshape array of size 12 into shape (4,4)

In [176]:
nd.reshape((1,6))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-176-706a52b78a91> in <module>()
----> 1 nd.reshape((1,6))

ValueError: cannot reshape array of size 12 into shape (1,6)

In [178]:
nd.reshape((1,1,2,6))
Out[178]:
array([[[[ 2,  1,  0, 17, 18, 13],
         [11,  7,  8, 19,  2, 16]]]])
In [179]:
nd.resize((3,4))
In [180]:
nd
Out[180]:
array([[ 2,  1,  0, 17],
       [18, 13, 11,  7],
       [ 8, 19,  2, 16]])

【注意】

1、resize()和reshape()都是对数组进行变形,resize()是在原来数组上直接变形,reshape()生成一个新的数组

2、变形的时候,新数组的总元素个数要和原来的保持一致

拼图游戏

In [181]:
girl = plt.imread("./source/girl2.jpg")
plt.imshow(girl)
plt.show()
In [186]:
tigger[150:450,200:500] = girl
plt.imshow(tigger)
plt.show()
In [ ]:

4、级联

In [4]:
nd1 = np.random.randint(0,100,size=(4,4))
nd2 = np.random.randint(0,100,size=(4,3))
print(nd1)
print(nd2)
[[86 87 27  4]
 [30 93 58 74]
 [37 83 74 48]
 [ 9  4 81 19]]
[[10 36 77]
 [41  5 32]
 [38 30  6]
 [17 20 39]]

级联:就是按照指定的维度把两个数组链接在一起

级联注意点:

1、参与级联的数组要构成一个列表(或元组)

2、维度必须一样

3、形状必须相符(如:二维数组axis=0时,列数必须一样,axis=1的时候行数必须一样)

4、级联的默认方向shape的第一个值所代表的那个维度(axis默认为0)

In [8]:
np.concatenate([nd1,nd2],axis=1) 
# 参数一,参与级联的那些数组;参数二,级联的维度
Out[8]:
array([[86, 87, 27,  4, 10, 36, 77],
       [30, 93, 58, 74, 41,  5, 32],
       [37, 83, 74, 48, 38, 30,  6],
       [ 9,  4, 81, 19, 17, 20, 39]])
In [13]:
nd3 = np.random.randint(0,100,size=(6,4))
nd3
Out[13]:
array([[55, 91,  5, 82],
       [95,  0, 17, 78],
       [91, 89, 27, 35],
       [37, 82, 78, 61],
       [ 6, 58, 14, 62],
       [21, 62, 45, 35]])
In [14]:
np.concatenate([nd1,nd3])
Out[14]:
array([[86, 87, 27,  4],
       [30, 93, 58, 74],
       [37, 83, 74, 48],
       [ 9,  4, 81, 19],
       [55, 91,  5, 82],
       [95,  0, 17, 78],
       [91, 89, 27, 35],
       [37, 82, 78, 61],
       [ 6, 58, 14, 62],
       [21, 62, 45, 35]])

hstack()和vstack()

hstack()把列数组改成一个行数组(把所有行上数组拼接成一个一行的数组)

vstack()把一个单行的数组拆成多行(每个行上只有一个数字)

In [16]:
nd3 = np.random.randint(0,10,size=(10,1))
nd3
Out[16]:
array([[0],
       [1],
       [6],
       [2],
       [1],
       [8],
       [1],
       [0],
       [4],
       [6]])
In [18]:
np.hstack(nd3)
Out[18]:
array([0, 1, 6, 2, 1, 8, 1, 0, 4, 6])
In [19]:
nd4 = np.random.randint(0,50,size=(4,3))
nd4
Out[19]:
array([[16, 41, 36],
       [45, 18,  3],
       [37,  9,  0],
       [43, 18, 46]])
In [20]:
np.hstack(nd4)
Out[20]:
array([16, 41, 36, 45, 18,  3, 37,  9,  0, 43, 18, 46])
In [21]:
np.vstack(nd4)
Out[21]:
array([[16, 41, 36],
       [45, 18,  3],
       [37,  9,  0],
       [43, 18, 46]])
In [22]:
nd5 = np.random.randint(0,10,size=10)
nd5
Out[22]:
array([2, 6, 3, 0, 2, 0, 3, 0, 7, 1])
In [23]:
np.vstack(nd5)
Out[23]:
array([[2],
       [6],
       [3],
       [0],
       [2],
       [0],
       [3],
       [0],
       [7],
       [1]])

5、切分

split()

vsplit()

hsplit()

In [28]:
nd = np.random.randint(0,100,size=(5,6))
nd
Out[28]:
array([[63, 92, 23, 12, 35, 69],
       [65, 30, 73, 78, 36, 70],
       [56,  9, 16, 53, 89, 23],
       [57, 79, 35, 68, 68, 16],
       [19,  0, 30, 77, 81, 20]])
In [27]:
np.hsplit(nd,[1,3]) # 代表在水平方向切分
# 参数一,被切的数组  参数二,切分点(列表,在切分点前切分)
Out[27]:
[array([[54],
        [44],
        [32],
        [37],
        [69]]), array([[51, 80],
        [ 1, 90],
        [65,  0],
        [ 0, 52],
        [64, 32]]), array([[37, 40, 25],
        [91, 88,  6],
        [ 8, 63, 78],
        [44, 48, 29],
        [19,  4, 17]])]
In [29]:
np.vsplit(nd,[1,4]) # 纵向上切分 参数和横向类似
Out[29]:
[array([[63, 92, 23, 12, 35, 69]]), array([[65, 30, 73, 78, 36, 70],
        [56,  9, 16, 53, 89, 23],
        [57, 79, 35, 68, 68, 16]]), array([[19,  0, 30, 77, 81, 20]])]
In [30]:
np.split(nd,[1,3],axis=0) # 切行
Out[30]:
[array([[63, 92, 23, 12, 35, 69]]), array([[65, 30, 73, 78, 36, 70],
        [56,  9, 16, 53, 89, 23]]), array([[57, 79, 35, 68, 68, 16],
        [19,  0, 30, 77, 81, 20]])]
In [32]:
np.split(nd,(1,3),axis=1) # 切列
Out[32]:
[array([[63],
        [65],
        [56],
        [57],
        [19]]), array([[92, 23],
        [30, 73],
        [ 9, 16],
        [79, 35],
        [ 0, 30]]), array([[12, 35, 69],
        [78, 36, 70],
        [53, 89, 23],
        [68, 68, 16],
        [77, 81, 20]])]

6、副本

In [33]:
nd = np.random.randint(0,100,size=6)
nd
Out[33]:
array([13, 89, 33, 62, 74, 55])
In [34]:
nd1 = nd 
In [36]:
nd1[0] = 1000
nd1
Out[36]:
array([1000,   89,   33,   62,   74,   55])
In [38]:
nd # 此时nd1和nd变量引用是同一个数组对象(nd和nd1中存储的地址都是一样的)
# 在某种意义上这也是一种浅拷贝
Out[38]:
array([1000,   89,   33,   62,   74,   55])

用copy()函数对数组对象创建副本

In [40]:
nd2 = nd.copy()
nd2
Out[40]:
array([1000,   89,   33,   62,   74,   55])
In [41]:
nd2[2] = 2000
nd2
Out[41]:
array([1000,   89, 2000,   62,   74,   55])
In [42]:
nd
Out[42]:
array([1000,   89,   33,   62,   74,   55])
In [43]:
nd1
Out[43]:
array([1000,   89,   33,   62,   74,   55])

用列表创建数组的过程有木有创建副本

In [44]:
l = [1,2,3,4]
nd = np.array(l) # 把l拷贝了一份放在了数组nd中
In [45]:
nd[0] = 100
nd
Out[45]:
array([100,   2,   3,   4])
In [46]:
l
Out[46]:
[1, 2, 3, 4]
In [ ]:

四、ndarray的聚合操作

1、求和

In [55]:
nd = np.random.randint(0,10,size=(3,4))
nd
Out[55]:
array([[2, 8, 0, 9],
       [8, 2, 4, 6],
       [4, 0, 3, 6]])
In [56]:
np.sum(nd,axis=0)  # 把行按照列标对应加起来
Out[56]:
array([14, 10,  7, 21])
In [57]:
np.sum(nd,axis=1) # 把列按照对应行标加起来
Out[57]:
array([19, 20, 13])
In [59]:
np.sum(nd) # 把所有的元素加起来
Out[59]:
52
In [60]:
nd.sum()
Out[60]:
52

2、最值

In [69]:
nd
Out[69]:
array([[2, 8, 0, 9],
       [8, 2, 4, 6],
       [4, 0, 3, 6]])
In [63]:
np.max(nd)
Out[63]:
9
In [65]:
np.max(nd,axis=0) # 求每个列里面最大值
Out[65]:
array([8, 8, 4, 9])
In [66]:
np.max(nd,axis=1) # 求每一行里面的最大值
Out[66]:
array([9, 8, 6])
In [67]:
np.argmax(nd)
Out[67]:
3
In [68]:
np.argmin(nd)
Out[68]:
2
In [71]:
np.argmax(nd,axis=0) # 在列上求最大值并返回最大值的行标
Out[71]:
array([1, 0, 1, 0], dtype=int64)
In [72]:
np.argmin(nd,axis=1) # 在每个行上求最小值返回列标
Out[72]:
array([2, 1, 1], dtype=int64)

3、其他聚合操作

Function Name    NaN-safe Version    Description
np.sum    np.nansum    Compute sum of elements
np.prod    np.nanprod    Compute product of elements
np.mean    np.nanmean    Compute mean of elements
np.std    np.nanstd    Compute standard deviation
np.var    np.nanvar    Compute variance
np.min    np.nanmin    Find minimum value
np.max    np.nanmax    Find maximum value
np.argmin    np.nanargmin    Find index of minimum value
np.argmax    np.nanargmax    Find index of maximum value
np.median    np.nanmedian    Compute median of elements
np.percentile    np.nanpercentile    Compute rank-based statistics of elements
np.any    N/A    Evaluate whether any elements are true
np.all    N/A    Evaluate whether all elements are true
np.power 幂运算
In [73]:
nd = np.array([1,2,3,np.nan])
nd
Out[73]:
array([  1.,   2.,   3.,  nan])
In [76]:
np.nan + 100
np.nan*100
# nan表示缺失(默认是浮点型),缺失和任何数做运算结果都是缺失
Out[76]:
nan
In [74]:
nd.sum()
Out[74]:
nan
In [77]:
np.nansum(nd) # 以nan开头的聚合方法,可在聚合运算的直接把nan剔除
Out[77]:
6.0
思考题:给定一个4维矩阵,如何得到最后两维的和?
In [78]:
nd = np.random.randint(0,10,size=(2,3,2,2))
nd
Out[78]:
array([[[[4, 9],
         [5, 1]],

        [[2, 1],
         [7, 5]],

        [[2, 7],
         [4, 9]]],


       [[[8, 9],
         [6, 4]],

        [[7, 3],
         [5, 4]],

        [[5, 4],
         [7, 6]]]])
In [81]:
nd.sum(axis=-1)
nd.sum(axis=-2)
nd.sum(axis=(-1,-2))
Out[81]:
array([[19, 15, 22],
       [27, 19, 22]])
思考题:如何根据第3列来对一个5*5矩阵排序?
In [84]:
nd = np.random.randint(0,20,size=(5,5))
nd
Out[84]:
array([[ 5, 19,  1,  4, 12],
       [13, 16, 17, 18, 17],
       [19, 12,  4, 19, 10],
       [16, 11, 18,  3, 14],
       [14,  0, 15, 15, 10]])
In [85]:
nd[[3,0,4,1,2]] #??
Out[85]:
array([[16, 11, 18,  3, 14],
       [ 5, 19,  1,  4, 12],
       [14,  0, 15, 15, 10],
       [13, 16, 17, 18, 17],
       [19, 12,  4, 19, 10]])
In [92]:
nd
Out[92]:
array([[ 1,  4,  5, 12, 19],
       [13, 16, 17, 17, 18],
       [ 4, 10, 12, 19, 19],
       [ 3, 11, 14, 16, 18],
       [ 0, 10, 14, 15, 15]])
In [94]:
sort_ind = np.argsort(nd[:,3]) # 排序并且返回排序以后的下标序列
sort_ind
Out[94]:
array([0, 4, 3, 1, 2], dtype=int64)
In [95]:
nd[sort_ind]
. . .
思路
In [ ]:

五、ndarray的矩阵操作

1. 基本矩阵操作

1)算术运算(即加减乘除)
In [96]:
nd = np.random.randint(0,10,size=(5,5))
nd
Out[96]:
array([[9, 0, 4, 2, 7],
       [7, 3, 9, 6, 6],
       [5, 9, 6, 3, 0],
       [4, 5, 5, 8, 3],
       [2, 5, 3, 3, 3]])
In [97]:
nd + 3  # 加运算,启动广播机制把缺失的地方补全
Out[97]:
array([[12,  3,  7,  5, 10],
       [10,  6, 12,  9,  9],
       [ 8, 12,  9,  6,  3],
       [ 7,  8,  8, 11,  6],
       [ 5,  8,  6,  6,  6]])
In [98]:
nd*2 # 乘法不需要广播机制
Out[98]:
array([[18,  0,  8,  4, 14],
       [14,  6, 18, 12, 12],
       [10, 18, 12,  6,  0],
       [ 8, 10, 10, 16,  6],
       [ 4, 10,  6,  6,  6]])
In [99]:
nd/2
Out[99]:
array([[ 4.5,  0. ,  2. ,  1. ,  3.5],
       [ 3.5,  1.5,  4.5,  3. ,  3. ],
       [ 2.5,  4.5,  3. ,  1.5,  0. ],
       [ 2. ,  2.5,  2.5,  4. ,  1.5],
       [ 1. ,  2.5,  1.5,  1.5,  1.5]])
2)矩阵积
In [103]:
nd1 = np.random.randint(0,5,size=(4,3))
nd2 = np.random.randint(0,5,size=(3,4))
print(nd1)
print(nd2)
. . .
In [104]:
np.dot(nd1,nd2)
. . .

2. 广播机制

ndarray的广播机制的两条规则:

  • 1、为缺失维度补1
  • 2、假定缺失的元素用已有值填充
In [105]:
m = np.random.randint(0,10,size=(2,3))
m
Out[105]:
array([[3, 6, 9],
       [4, 7, 7]])
In [106]:
a = np.arange(3)
a
Out[106]:
array([0, 1, 2])
In [107]:
# 求m+a
m+a
Out[107]:
array([[ 3,  7, 11],
       [ 4,  8,  9]])
In [108]:
a = np.arange(3).reshape((3,1))
a
Out[108]:
array([[0],
       [1],
       [2]])
In [109]:
b = np.arange(3)
b
Out[109]:
array([0, 1, 2])
In [110]:
a+b
Out[110]:
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4]])

0 0 0 0 1 2

1 1 1 + 0 1 2

2 2 2 0 1 2

In [111]:
a + 3
Out[111]:
array([[3],
       [4],
       [5]])
In [112]:
a = np.random.randint(0,5,size=(3,3))
a
Out[112]:
array([[1, 0, 0],
       [2, 4, 2],
       [0, 3, 1]])
In [113]:
b = np.random.randint(0,5,size=(2,2))
b
Out[113]:
array([[4, 1],
       [2, 2]])
In [114]:
a + b # 如果行和列个数均不相同无法进行补全,不能相加
-------------------------------------------------------------
ValueError                  Traceback (most recent call last)
<ipython-input-114-f96fb8f649b6> in <module>()
----> 1 a + b

ValueError: operands could not be broadcast together with shapes (3,3) (2,2) 

排序

In [120]:
nd = np.random.randint(0,100,size=(10))
nd
Out[120]:
array([70, 90, 72, 23, 55, 85, 24, 88, 91, 13])
In [119]:
nd.sort() # 改变原来的数组
In [118]:
nd
Out[118]:
array([13, 26, 35, 43, 46, 59, 76, 77, 81, 94])
In [121]:
np.sort(nd)# 不会改变原来的数组,而是生成一个新的数组
Out[121]:
array([13, 23, 24, 55, 70, 72, 85, 88, 90, 91])
In [122]:
nd
Out[122]:
array([70, 90, 72, 23, 55, 85, 24, 88, 91, 13])
In [123]:
nd1 = np.random.randint(0,100,size=(4,4))
nd1
Out[123]:
array([[95, 82, 52, 15],
       [70, 87,  2, 54],
       [52, 66, 26, 95],
       [ 9, 49, 63, 48]])
In [126]:
np.sort(nd1,axis=1) # axis=1排的是列标 axis=0排的是行标
Out[126]:
array([[15, 52, 82, 95],
       [ 2, 54, 70, 87],
       [26, 52, 66, 95],
       [ 9, 48, 49, 63]])
In [127]:
np.sort(nd1,axis=0)
Out[127]:
array([[ 9, 49,  2, 15],
       [52, 66, 26, 48],
       [70, 82, 52, 54],
       [95, 87, 63, 95]])
In [128]:
# argsort()排的是下标
np.argsort(nd1,axis=0)
Out[128]:
array([[3, 3, 1, 0],
       [2, 2, 2, 3],
       [1, 0, 0, 1],
       [0, 1, 3, 2]], dtype=int64)
In [ ]:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值