Python之Numpy学习(1)

astype(dtype):显示的转换数组的数据类型,该方法总会生成一个新数组

In [6]: arr = np.arange(10,dtype=np.float)

In [7]: arr
Out[7]: array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])

In [8]: arr = arr.astype(np.int)

In [9]: arr
Out[9]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

对于数组元素是字符串也同样适用:

In [10]: s = np.array(['1.24','3.56','24'])

In [11]: s = s.astype(float)

In [12]: s
Out[12]: array([ 1.24,  3.56, 24.  ])

对于numpy的标量算术运算会把参数传递给数组的每一个元素

In [18]: arr1 = np.arange(12).reshape(3,4)

In [19]: arr2 = np.arange(12,24).reshape(3,4)

In [20]: arr1
Out[20]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [21]: arr2
Out[21]:
array([[12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])

In [22]: arr1 + arr2 #对应每个元素相加
Out[22]:
array([[12, 14, 16, 18],
       [20, 22, 24, 26],
       [28, 30, 32, 34]])

In [24]: arr1 * arr2#对应每个元素相乘
Out[24]:
array([[  0,  13,  28,  45],
       [ 64,  85, 108, 133],
       [160, 189, 220, 253]])

In [25]: arr1 + 2 #每个元素都加2
Out[25]:
array([[ 2,  3,  4,  5],
       [ 6,  7,  8,  9],
       [10, 11, 12, 13]])

In [26]: arr1 / 2 #每个元素都除2
Out[26]:
array([[0. , 0.5, 1. , 1.5],
       [2. , 2.5, 3. , 3.5],
       [4. , 4.5, 5. , 5.5]])

若想要实现矩阵的乘法需要借助于numpy的dot()方法或者使用@作为中间操作符(两个矩阵要满足乘法要求)

In [28]: arr1 = np.arange(6).reshape(2,3)

In [29]: arr2 = np.arange(6).reshape(3,2)

In [30]: np.dot(arr1,arr2)
Out[30]:
array([[10, 13],
       [28, 40]])

numpy支持同尺寸数组之间的比较,会返回一个布尔值数组

In [38]: arr1 = np.random.randint(0,6,(2,3))

In [39]: arr2 = np.random.randint(0,6,(2,3))

In [40]: arr1
Out[40]:
array([[2, 4, 0],
       [4, 3, 2]])

In [41]: arr2
Out[41]:
array([[5, 2, 1],
       [0, 0, 2]])

In [42]: arr1 > arr2
Out[42]:
array([[False,  True, False],
       [ True,  True, False]])

In [43]: arr1 > 5#标量比较会将每个元素要进行比较
Out[43]:
array([[False, False, False],
       [False, False, False]])

对于numpy数组的切片方法与Python中列表切片方法基本一致,但是值得注意的是numpy的数组切片是原数组的视图,换句话说,修改切片会修改原数组

In [51]: arr = np.arange(10)

In [52]: arr
Out[52]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [53]: ls = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [54]: arr_s = arr[:5]

In [55]: arr_s
Out[55]: array([0, 1, 2, 3, 4])

In [56]: arr_s[0] = 10  #改变数组切片会改变原数组

In [57]: arr
Out[57]: array([10,  1,  2,  3,  4,  5,  6,  7,  8,  9])

In [58]: ls_s = ls[:5]

In [59]: ls_s
Out[59]: [0, 1, 2, 3, 4]

In [60]: ls_s[0] = 10#改变列表切片不会改变原数组

In [61]: ls
Out[61]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

同理在对切片赋值时,原数组的整个切片区域都将被赋值

In [84]: arr = np.arange(12).reshape(2,2,3)

In [85]: arr
Out[85]:
array([[[ 0,  1,  2],
        [ 3,  4,  5]],

       [[ 6,  7,  8],
        [ 9, 10, 11]]])

In [86]: arr[0,:,1:]#对于多维数组的切片更灵活,但是思想与列表一致
Out[86]:
array([[1, 2],
       [4, 5]])

In [87]: arr[0,:,1:] = 250#整个区域都将被赋值

In [88]: arr
Out[88]:
array([[[  0, 250, 250],
        [  3, 250, 250]],

       [[  6,   7,   8],
        [  9,  10,  11]]])

所以在数组中切片要想得到的是数组的拷贝需要用道copy()方法

In [68]: arr = np.arange(10)

In [69]: arr
Out[69]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [70]: arr_copy = arr[:].copy()#使用copy方法返回的是原数组的备份,两者是独立存在的

In [71]: arr_copy
Out[71]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [72]: arr_copy[0] = 10

In [73]: arr
Out[73]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [74]: arr_copy
Out[74]: array([10,  1,  2,  3,  4,  5,  6,  7,  8,  9])

在数组中一个比较方便的操作是可以直接进行布尔索引(生成的是数组拷贝,原数组并没有任何变化)

array([[[  0, 250, 250],
        [  3, 250, 250]],

       [[  6,   7,   8],
        [  9,  10,  11]]])
In [89]: arr = np.random.randn(4,5)

In [90]: nums = np.arange(4)

In [91]: arr
Out[91]:
array([[ 0.96815772,  1.09588192, -0.75565206, -0.33541785, -1.55058099],
       [ 0.79433089,  1.87963211,  0.28907563,  1.41863177, -1.20095493],
       [ 2.21006144, -0.80173754,  1.30744596,  0.06783745,  1.01946617],
       [ 0.61298953, -0.94047895, -0.63993907, -0.63451676,  1.63448396]])

In [94]: arr[nums%2==0]#0,2
Out[94]:
array([[ 0.96815772,  1.09588192, -0.75565206, -0.33541785, -1.55058099],
       [ 2.21006144, -0.80173754,  1.30744596,  0.06783745,  1.01946617]])

利用布尔索引操作数组相当灵活

同时与别与列表的常规索引,numpy数组可以实现神奇索引,简单来说可以通过传递一个包含指明所需顺序的列表或数组来完成,如下。

In [2]: arr = np.arange(24).reshape(6,4)

In [3]: arr
Out[3]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23]])

In [4]: arr[[0,2,4]]#传递一个列表获取数据
Out[4]:
array([[ 0,  1,  2,  3],
       [ 8,  9, 10, 11],
       [16, 17, 18, 19]])

In [6]: arr[[0,2,4],[1,2,3]]#传递一个列表获取数据
Out[6]: array([ 1, 10, 19])

 

你可能感兴趣的:(Python学习)