numpy数组基础

  1. 数组的属性
import numpy as np
a = np.array([1,2,3])
a
#array([1,2,3])

几个主要属性

>>> a.ndim#秩
1
>>> a.dtype#数据类型
dtype('int64')
>>> a.shape#型
(3,)
np.zeros((3,4))
Out[12]: 
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])
np.ones((3,3))
Out[13]: 
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])
np.arange(0,10)
Out[14]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(1,10)
Out[15]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(1,10,2)
Out[16]: array([1, 3, 5, 7, 9])
np.arage(0,12).reshape(3,4)
np.arange(0,12).reshape(3,4)#拆分为二维数组
Out[20]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
np.random.randint(3)
Out[21]: 1
np.random.randint(3)
Out[22]: 1
np.random.randint(3)
Out[23]: 0
np.random.randint(3)
Out[24]: 0
np.random.randint(3)
Out[25]: 0
np.random.randint(3)
Out[26]: 0
np.random.randint(3)
Out[27]: 1
np.random.randint(3)
Out[28]: 2
np.random.randint(3)
Out[29]: 1
np.random.randint(3)
Out[30]: 0
np.random.randint(3)
Out[31]: 2
np.random.randint(3)
Out[32]: 0
np.random.random(3)
Out[33]: array([ 0.51661222,  0.59988741,  0.76954943])
np.random.random((3,3))
Out[35]: 
array([[ 0.30093451,  0.24386498,  0.77098505],
       [ 0.19329373,  0.86999311,  0.21056814],
       [ 0.69949815,  0.03438923,  0.267541  ]])
  1. 数组的运算
    可以数加减乘除数组
    数组加减乘除数组
    可以数组自加 A +=1
    自乘 A *=2

  2. 索引与切片

a = np.arange(10)
a
Out[37]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a[0]
Out[38]: 0
a[9]
Out[39]: 9
a[10]
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-40-7c7cb9812849>", line 1, in <module>
    a[10]
IndexError: index 10 is out of bounds for axis 0 with size 10
a[-1]
Out[41]: 9
a[-9]
Out[42]: 1
b = np.arange(9).reshape((3,3))
b
Out[45]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
b[1][1]
Out[46]: 4

切片是指抽取数组中的一部分生成新的数组

b = np.arange(9).reshape((3,3))
b
Out[45]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
b[1][1]
Out[46]: 4
b = np.arange(9)
b[1:3]
Out[48]: array([1, 2])
b[0:3]
Out[49]: array([0, 1, 2])
b[0:8:2]
Out[50]: array([0, 2, 4, 6])
b = np.arange(9).reshape((3,3))
b[0,]
Out[52]: array([0, 1, 2])
b[0:1,0:1]
Out[53]: array([[0]])
b[0:3,0:3]
Out[54]: 
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
b[[0,2],0:2]
Out[55]: 
array([[0, 1],
       [6, 7]])

遍历数组

for i in a:
    print(i)

0
1
2
3
4
5
6
7
8
9
for row in b:
    print(b)

[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[0 1 2]
 [3 4 5]
 [6 7 8]]
[[0 1 2]
 [3 4 5]
 [6 7 8]]

条件

a = np.random.random((4,4))
a
Out[62]: 
array([[ 0.70432403,  0.2960082 ,  0.95635857,  0.74913378],
       [ 0.39139985,  0.67790113,  0.96460877,  0.33186986],
       [ 0.23856203,  0.14917232,  0.74323987,  0.36831967],
       [ 0.38775951,  0.27373957,  0.92410703,  0.71536022]])
a < 0.5
Out[63]: 
array([[False,  True, False, False],
       [ True, False, False,  True],
       [ True,  True, False,  True],
       [ True,  True, False, False]], dtype=bool)
a[a<0.5]
Out[64]: 
array([ 0.2960082 ,  0.39139985,  0.33186986,  0.23856203,  0.14917232,
        0.36831967,  0.38775951,  0.27373957])

链接数组

np.vstack((a,b))
Out[67]: 
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 1.,  1.,  1.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
np.hstack((a,b))
Out[68]: 
array([[ 1.,  1.,  1.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  0.,  0.,  0.],
       [ 1.,  1.,  1.,  0.,  0.,  0.]])
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.array([7,8,9])
np.column_stack((a,b,c))
Out[72]: 
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])
np.row_stack((a,b,c))
Out[73]: 
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

数组切分

[a1,a2,a3] = np.split(a,[1,3],axis=0)#横向切
a
Out[83]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
a1
Out[84]: array([[0, 1, 2, 3]])
a2
Out[85]: 
array([[ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
a3
Out[86]: array([[12, 13, 14, 15]])
[a1,a2,a3] = np.split(a,[1,3],axis=1)#横向切
a1
Out[88]: 
array([[ 0],
       [ 4],
       [ 8],
       [12]])
a2
Out[89]: 
array([[ 1,  2],
       [ 5,  6],
       [ 9, 10],
       [13, 14]])
a3
Out[90]: 
array([[ 3],
       [ 7],
       [11],
       [15]])
[a1,a2,a3] = np.split(a,[0,2],axis=1)#横向切
a1
Out[92]: array([], shape=(4, 0), dtype=int64)
a2
Out[93]: 
array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]])
a3
Out[94]: 
array([[ 2,  3],
       [ 6,  7],
       [10, 11],
       [14, 15]])
[a1,a2,a3] = np.split(a,[2,3],axis=1)#横向切
a1
Out[96]: 
array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13]])
a2
Out[97]: 
array([[ 2],
       [ 6],
       [10],
       [14]])
a3
Out[98]: 
array([[ 3],
       [ 7],
       [11],
       [15]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值