numpy练习

1
import numpy as np
1
np.__version__
'1.19.2'
1
import pandas as pd
1
pd.__version__
'1.1.3'
1
import requests as requests
1
requests.__version__
'2.24.0'

创建ndarry

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

1
a = [1,2,3,6,7,8,5,4]
1
a
[1, 2, 3, 6, 7, 8, 5, 4]
1
a.sum()
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-17-393d3db239ee> in <module>
----> 1 a.sum()


AttributeError: 'list' object has no attribute 'sum'
1
type(a)
list
1
2
nd = np.array(a)
nd
array([1, 2, 3, 6, 7, 8, 5, 4])
1
nd.sum()
36
1
nd.var()
5.25
1
type(nd)
numpy.ndarray
1
2
a2 = [[1,3,5,7],[2,4,6,8]]
a2
[[1, 3, 5, 7], [2, 4, 6, 8]]
1
2
nd2 = np.array(a2)
nd2
array([[1, 3, 5, 7],
       [2, 4, 6, 8]])
1
2
x = np.arange(0,10000,1)
x
array([   0,    1,    2, ..., 9997, 9998, 9999])
1
%time x.sum()
Wall time: 0 ns

49995000
1
%timeit x.sum()
6.55 µs ± 265 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
1
2
3
4
5
6
7
# python 代码
def sum2(x):
ret = 0
for i in x:
ret += i
return ret

1
sum2(x)
49995000
1
%time sum2(x)
Wall time: 2 ms

49995000
1
%timeit sum2(x)
2.11 ms ± 34.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

2、使用np的routines函数创建

包含以下创建方法
(1) np.ones(shape,dtype=None,order=’C’)

1
np.ones(shape=(5,5), dtype=np.int8)
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]], dtype=int8)

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

1
np.zeros(shape=(2,3,4),dtype=np.float16)
array([[[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]],

       [[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]]], dtype=float16)

(3)、np.full(shape,fill_vlue,dtype=float,order=’C’)

1
np.full((3,5),fill_value=3.14)
array([[3.14, 3.14, 3.14, 3.14, 3.14],
       [3.14, 3.14, 3.14, 3.14, 3.14],
       [3.14, 3.14, 3.14, 3.14, 3.14]])

(4) np.eye(N,M=None,order=’C’) 对角线为1,其他位置为0

1
np.eye(5)
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])
1
np.eye(4)
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

(5) np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

1
2
# 等差数列
np.linspace(0,100,21)
array([  0.,   5.,  10.,  15.,  20.,  25.,  30.,  35.,  40.,  45.,  50.,
        55.,  60.,  65.,  70.,  75.,  80.,  85.,  90.,  95., 100.])

(6) np.arange([start, ]stop, [step, ]dtype=None)

1
np.arange(1,100,3)
array([ 1,  4,  7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49,
       52, 55, 58, 61, 64, 67, 70, 73, 76, 79, 82, 85, 88, 91, 94, 97])

(7) np.random.randint(low, high=None, size=None, dtype=’l’)

1
np.random.randint(0,100,size=(5,5))
array([[10, 32,  4, 81, 44],
       [84, 23, 43, 55, 31],
       [69, 39, 69, 93, 58],
       [82, 70, 52, 44, 70],
       [ 6, 35,  0, 88, 66]])

(8) np.random.randn(d0, d1, …, dn)
标准正态分布

1
2
3
4
# normal 正常,正太
# dimession 维度
# 平均值是0,方差是1
np.random.randn(4,5)
array([[ 1.51295648, -1.09616304,  0.36172254,  1.39316818,  0.3398295 ],
       [-0.14088159, -1.11479244,  3.1749888 , -1.60456107,  0.01919242],
       [ 1.70563537, -0.08981991,  0.86159575, -0.67392191, -0.83479524],
       [ 1.40177443, -0.21961461,  0.37905177,  0.38807823, -1.46406963]])

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

1
2
nd = np.random.normal(loc = 175,scale=10,size = 10000).round(2)
nd
array([168.17, 179.87, 160.22, ..., 189.7 , 176.07, 184.78])
1
nd.mean()
174.823095
1
nd.var()
99.38659825097501
1
2
# 标准差,是方差的开平发
nd.std()
9.96928273503039

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

生成0到1的随机数,左闭右开

1
np.random.random(100)
array([0.93757823, 0.62721907, 0.96136819, 0.00109472, 0.38287479,
       0.99922793, 0.41347303, 0.44696331, 0.8678766 , 0.75720445,
       0.91031049, 0.28725956, 0.09277962, 0.42108861, 0.95086594,
       0.2442309 , 0.16028601, 0.52835949, 0.04226749, 0.27488674,
       0.70462444, 0.86397879, 0.49479301, 0.40478902, 0.21040264,
       0.06995136, 0.94236269, 0.01365573, 0.46885671, 0.8991946 ,
       0.70356631, 0.62998806, 0.45487313, 0.33752381, 0.15442796,
       0.7105061 , 0.23857533, 0.56028816, 0.16769099, 0.09568056,
       0.63943094, 0.40809231, 0.76461533, 0.00732675, 0.74878219,
       0.99043614, 0.09705811, 0.13128479, 0.10815912, 0.6618818 ,
       0.93141231, 0.56540397, 0.4289353 , 0.74339137, 0.29819775,
       0.85858313, 0.63671793, 0.83064404, 0.50316277, 0.76552906,
       0.96041051, 0.47830712, 0.80995996, 0.87348506, 0.8947484 ,
       0.22966122, 0.32277804, 0.74232665, 0.41657303, 0.11329246,
       0.50888508, 0.51601069, 0.70082506, 0.30441819, 0.96700995,
       0.91830279, 0.34600223, 0.07133664, 0.46451369, 0.62684585,
       0.05843449, 0.89062756, 0.90818508, 0.5026754 , 0.27010533,
       0.43799603, 0.41560763, 0.95940647, 0.53062415, 0.78076234,
       0.41229535, 0.50446211, 0.56443117, 0.5252491 , 0.72683347,
       0.47240595, 0.20034323, 0.31343713, 0.85126688, 0.83168743])

ndarray()的属性

4个必记参数: ndim:维度 shape:形状(各维度的长度) size:总长度 dtype:元素类型

1
nd
array([168.17, 179.87, 160.22, ..., 189.7 , 176.07, 184.78])
1
nd.size
10000
1
nd.shape
(10000,)
1
nd.ndim
1
1
nd.dtype
dtype('float64')

ndarray的基本操作

1、索引

一维时于列表完全一致 ,多维时同理

1
2
nd2 = np.random.randint(0,150,size = (4,5))
nd2
array([[ 70,  56, 139,  39,  56],
       [ 47,  21,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
nd2[1,1]
21
1
nd2[1]
array([47, 21, 98, 63, 56])
1
2
nd2[1,1] = 88
nd2
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
nd2
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
nd2[0:3]
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4]])
1
nd2[-2:]
array([[87, 69, 74, 20,  4],
       [ 2, 95, 51, 43, 24]])
1
nd2[0:3,0:3]
array([[ 70,  56, 139],
       [ 47,  88,  98],
       [ 87,  69,  74]])
1
2
nd3 = nd2[:10]
nd3
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
nd3[::1]
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
nd3[::-1]
array([[  2,  95,  51,  43,  24],
       [ 87,  69,  74,  20,   4],
       [ 47,  88,  98,  63,  56],
       [ 70,  56, 139,  39,  56]])
1
nd3[::2]
array([[ 70,  56, 139,  39,  56],
       [ 87,  69,  74,  20,   4]])
1
from PIL import Image
1
2
cat = Image.open('./cat.jpg')
cat


output_75_0

1
2
cat_data = np.array(cat)
cat_data
array([[[187, 181, 167],
        [187, 181, 167],
        [187, 181, 167],
        ...,
        [231, 225, 209],
        [234, 228, 212],
        [237, 231, 215]],

       [[188, 182, 168],
        [188, 182, 168],
        [189, 183, 169],
        ...,
        [237, 231, 215],
        [237, 231, 215],
        [240, 234, 218]],

       [[190, 184, 170],
        [191, 185, 171],
        [191, 185, 171],
        ...,
        [234, 228, 214],
        [233, 227, 213],
        [234, 228, 214]],

       ...,

       [[199, 195, 184],
        [199, 195, 184],
        [201, 197, 186],
        ...,
        [219, 212, 202],
        [218, 211, 201],
        [218, 211, 201]],

       [[199, 195, 184],
        [201, 197, 186],
        [200, 196, 185],
        ...,
        [215, 208, 198],
        [220, 213, 203],
        [224, 217, 207]],

       [[201, 197, 186],
        [204, 200, 189],
        [201, 197, 186],
        ...,
        [212, 205, 195],
        [220, 213, 203],
        [227, 220, 210]]], dtype=uint8)
1
2
cat_data.shape
# 行 列 像素
(600, 404, 3)
1
type(cat_data)
numpy.ndarray
1
cat_data.dtype
dtype('uint8')
1
2**8
256
1
cat_data.max()
255
1
cat_data.min()
0
1
2
cat2 = cat_data[:,:,::-1]
cat2
array([[[167, 181, 187],
        [167, 181, 187],
        [167, 181, 187],
        ...,
        [209, 225, 231],
        [212, 228, 234],
        [215, 231, 237]],

       [[168, 182, 188],
        [168, 182, 188],
        [169, 183, 189],
        ...,
        [215, 231, 237],
        [215, 231, 237],
        [218, 234, 240]],

       [[170, 184, 190],
        [171, 185, 191],
        [171, 185, 191],
        ...,
        [214, 228, 234],
        [213, 227, 233],
        [214, 228, 234]],

       ...,

       [[184, 195, 199],
        [184, 195, 199],
        [186, 197, 201],
        ...,
        [202, 212, 219],
        [201, 211, 218],
        [201, 211, 218]],

       [[184, 195, 199],
        [186, 197, 201],
        [185, 196, 200],
        ...,
        [198, 208, 215],
        [203, 213, 220],
        [207, 217, 224]],

       [[186, 197, 201],
        [189, 200, 204],
        [186, 197, 201],
        ...,
        [195, 205, 212],
        [203, 213, 220],
        [210, 220, 227]]], dtype=uint8)
1
Image.fromarray(cat2)

output_84_0

1
cat_data.shape
(600, 404, 3)
1
2
cat3 = cat_data[::5,::5]
cat3
array([[[187, 181, 167],
        [183, 177, 163],
        [184, 178, 164],
        ...,
        [226, 223, 208],
        [229, 226, 209],
        [231, 225, 209]],

       [[189, 183, 169],
        [185, 179, 165],
        [191, 185, 171],
        ...,
        [227, 224, 209],
        [228, 224, 212],
        [227, 223, 211]],

       [[186, 180, 166],
        [187, 181, 167],
        [187, 181, 167],
        ...,
        [225, 222, 207],
        [226, 223, 208],
        [228, 224, 213]],

       ...,

       [[190, 187, 172],
        [192, 188, 176],
        [190, 186, 175],
        ...,
        [213, 204, 195],
        [205, 198, 188],
        [200, 193, 183]],

       [[200, 196, 185],
        [195, 191, 180],
        [201, 197, 186],
        ...,
        [219, 210, 201],
        [214, 207, 197],
        [207, 200, 190]],

       [[196, 192, 181],
        [198, 194, 183],
        [202, 198, 187],
        ...,
        [211, 204, 194],
        [202, 195, 185],
        [215, 208, 198]]], dtype=uint8)
1
Image.fromarray(cat_data[::5,::5])

output_87_0

1
2
3
4
#  红绿蓝 0,1,2
# 绿红蓝 1,0,2
#Image.fromarray(cat_data[:,:,[1,0,2]])
Image.fromarray(cat_data[::5,::5,[1,0,2]])

output_88_0

1
2
cat4 = cat_data[:,:,0]
cat4
array([[187, 187, 187, ..., 231, 234, 237],
       [188, 188, 189, ..., 237, 237, 240],
       [190, 191, 191, ..., 234, 233, 234],
       ...,
       [199, 199, 201, ..., 219, 218, 218],
       [199, 201, 200, ..., 215, 220, 224],
       [201, 204, 201, ..., 212, 220, 227]], dtype=uint8)
1
Image.fromarray(cat4)

output_90_0

1
2
import matplotlib.pyplot as plt
%matplotlib inline
1
plt.imshow(cat_data)
<matplotlib.image.AxesImage at 0x27adc67adc0>

output_92_1

1
plt.imshow(cat_data[::-1])
<matplotlib.image.AxesImage at 0x27adc9661c0>

output_93_1

1
plt.imshow(cat_data[::-15,::-15])
<matplotlib.image.AxesImage at 0x27adc9c1520>


output_94_1

变形

使用reshape函数,注意参数是一个tuple!

1
nd2
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
nd2.reshape(2,10)
array([[ 70,  56, 139,  39,  56,  47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4,   2,  95,  51,  43,  24]])
1
nd2.reshape(5,4)
array([[ 70,  56, 139,  39],
       [ 56,  47,  88,  98],
       [ 63,  56,  87,  69],
       [ 74,  20,   4,   2],
       [ 95,  51,  43,  24]])
1
cat_data.shape
(600, 404, 3)
1
cat_data.reshape(600, 404, 3)
array([[[187, 181, 167],
        [187, 181, 167],
        [187, 181, 167],
        ...,
        [231, 225, 209],
        [234, 228, 212],
        [237, 231, 215]],

       [[188, 182, 168],
        [188, 182, 168],
        [189, 183, 169],
        ...,
        [237, 231, 215],
        [237, 231, 215],
        [240, 234, 218]],

       [[190, 184, 170],
        [191, 185, 171],
        [191, 185, 171],
        ...,
        [234, 228, 214],
        [233, 227, 213],
        [234, 228, 214]],

       ...,

       [[199, 195, 184],
        [199, 195, 184],
        [201, 197, 186],
        ...,
        [219, 212, 202],
        [218, 211, 201],
        [218, 211, 201]],

       [[199, 195, 184],
        [201, 197, 186],
        [200, 196, 185],
        ...,
        [215, 208, 198],
        [220, 213, 203],
        [224, 217, 207]],

       [[201, 197, 186],
        [204, 200, 189],
        [201, 197, 186],
        ...,
        [212, 205, 195],
        [220, 213, 203],
        [227, 220, 210]]], dtype=uint8)
1
2
cat5 = cat_data.reshape(600, 404, 3)
plt.imshow(cat5)
<matplotlib.image.AxesImage at 0x27adca3c640>

output_101_1

1
2
3
4
# 高度 0、宽度 1、像素 2
# 转置,行和列调整
cat6 = np.transpose(cat_data,axes = (1,0,2))
plt.imshow(cat6)
<matplotlib.image.AxesImage at 0x27adda66340>


output_102_1

4、级联

np.concatenate() 级联需要注意的点:

  • 级联的参数是列表:一定要加中括号或小括号
  • 维度必须相同
  • 形状相符
  • 【重点】级联的方向默认是shape这个tuple的第一个值所代表的维度方向
  • 可通过axis参数改变级联的方向
1
nd2
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
np.concatenate([nd2,nd2])
array([[ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24],
       [ 70,  56, 139,  39,  56],
       [ 47,  88,  98,  63,  56],
       [ 87,  69,  74,  20,   4],
       [  2,  95,  51,  43,  24]])
1
plt.imshow(cat_data)
<matplotlib.image.AxesImage at 0x27addac09d0>

output_106_1

1
2
cat7 = cat_data[:,:174]
plt.imshow(cat7)
<matplotlib.image.AxesImage at 0x27adc958d00>

output_107_1

1
2
cat8 = cat_data[:,230:,::-1]
plt.imshow(cat8)
<matplotlib.image.AxesImage at 0x27addd5ffd0>

output_108_1

1
print(cat7.shape,cat8.shape)
(600, 174, 3) (600, 174, 3)
1
np.concatenate([cat7,cat8])
array([[[187, 181, 167],
        [187, 181, 167],
        [187, 181, 167],
        ...,
        [190, 186, 175],
        [193, 189, 178],
        [191, 187, 176]],

       [[188, 182, 168],
        [188, 182, 168],
        [189, 183, 169],
        ...,
        [192, 188, 177],
        [196, 192, 181],
        [194, 190, 179]],

       [[190, 184, 170],
        [191, 185, 171],
        [191, 185, 171],
        ...,
        [190, 186, 175],
        [194, 190, 179],
        [191, 187, 176]],

       ...,

       [[198, 209, 213],
        [204, 215, 219],
        [208, 219, 223],
        ...,
        [202, 212, 219],
        [201, 211, 218],
        [201, 211, 218]],

       [[197, 208, 212],
        [205, 216, 220],
        [211, 222, 226],
        ...,
        [198, 208, 215],
        [203, 213, 220],
        [207, 217, 224]],

       [[197, 208, 212],
        [203, 214, 218],
        [211, 222, 226],
        ...,
        [195, 205, 212],
        [203, 213, 220],
        [210, 220, 227]]], dtype=uint8)
1
display(cat7.shape,cat8.shape)
(456, 230, 3)

(456, 260, 3)
1
2
cat9 = np.concatenate([cat7,cat8],axis=1)
plt.imshow(cat9)
<matplotlib.image.AxesImage at 0x27addd70760>

output_112_1

np.hstack与np.vstack
水平级联与垂直级联,处理自己,进行维度的变更

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import numpy as np

# 给定一个3维矩阵,如何得到最后两维的和?

np.random.seed(0)

A = np.random.randint(0,10,(3,3,3))

# solution by passing a tuple of axes (introduced in numpy 1.7.0)

print(A)

# print(A[-3,-3,-3])

# print(A.shape)

# print(A.sum(axis = (0)))

# print(A.sum(axis = (1)))

# print(A.sum(axis = (2)))

# print(A.sum(axis = (2,1)))

# sum = A.sum(axis=(0,1))

# print(sum)
[[[5 0 3]
  [3 7 9]
  [3 5 2]]

 [[4 7 6]
  [8 8 1]
  [6 7 7]]

 [[8 1 5]
  [9 8 9]
  [4 3 0]]]
1
print(A.sum(axis = (0)))
[[17  8 14]
 [20 23 19]
 [13 15  9]]
1
print(A.sum)
<built-in method sum of numpy.ndarray object at 0x0000027ADDE5D990>
1
print(A.sum(axis = (1)))
[[11 12 14]
 [18 22 14]
 [21 12 14]]
1
print(A.sum(axis = 2))
[[ 8 19 10]
 [17 17 20]
 [14 26  7]]

5、切分

与级联类似,三个函数完成切分工作:

  • np.split 将一个数组分割为多个子数组
  • np.hsplit 将一个数组水平分割为多个子数组(按列)
  • np.vsplit 将一个数组垂直分割为多个子数组(按行)
1
import numpy as np
1
2
3
4
5
6
7
nd1 = np.random.randint(0,150,size = (4,5))

nd2 = np.random.randint(0,150,size = (2,5))

nd3 = np.random.randint(0,150,size = (4,8))

display(nd1,nd2,nd3)
array([[ 79,  82,  99,  29, 147],
       [147, 142,  32,   9, 127],
       [ 32,  31, 114,  28,  34],
       [128, 128,  53, 133,  38]])

array([[ 17,  79, 132, 105,  42],
       [ 31, 120,   1,  65,  57]])

array([[ 35, 102, 119,  11,  82,  91, 128, 142],
       [ 99,  53, 140, 121,  84,  68,   6,  47],
       [127, 131, 100,  78, 143, 148,  23, 141],
       [117,  85,  48,  49,  69,  95,  94,   0]])
1
np.concatenate([nd1,nd3],axis = 1)
array([[ 79,  82,  99,  29, 147,  35, 102, 119,  11,  82,  91, 128, 142],
       [147, 142,  32,   9, 127,  99,  53, 140, 121,  84,  68,   6,  47],
       [ 32,  31, 114,  28,  34, 127, 131, 100,  78, 143, 148,  23, 141],
       [128, 128,  53, 133,  38, 117,  85,  48,  49,  69,  95,  94,   0]])
1
2
3
# horizontal 水平的,列数增加
nd5 = np.hstack((nd1,nd3))
nd5
array([[ 79,  82,  99,  29, 147,  35, 102, 119,  11,  82,  91, 128, 142],
       [147, 142,  32,   9, 127,  99,  53, 140, 121,  84,  68,   6,  47],
       [ 32,  31, 114,  28,  34, 127, 131, 100,  78, 143, 148,  23, 141],
       [128, 128,  53, 133,  38, 117,  85,  48,  49,  69,  95,  94,   0]])
1
2
3
# vertical 竖直方向增多,行数增多
nd4 = np.vstack((nd1,nd2))
nd4
array([[ 79,  82,  99,  29, 147],
       [147, 142,  32,   9, 127],
       [ 32,  31, 114,  28,  34],
       [128, 128,  53, 133,  38],
       [ 17,  79, 132, 105,  42],
       [ 31, 120,   1,  65,  57]])
1
nd4.shape
(6, 5)
1
nd4
array([[ 79,  82,  99,  29, 147],
       [147, 142,  32,   9, 127],
       [ 32,  31, 114,  28,  34],
       [128, 128,  53, 133,  38],
       [ 17,  79, 132, 105,  42],
       [ 31, 120,   1,  65,  57]])
1
np.split(nd4,indices_or_sections=3)
[array([[ 79,  82,  99,  29, 147],
        [147, 142,  32,   9, 127]]),
 array([[ 32,  31, 114,  28,  34],
        [128, 128,  53, 133,  38]]),
 array([[ 17,  79, 132, 105,  42],
        [ 31, 120,   1,  65,  57]])]
1
nd4
array([[ 79,  82,  99,  29, 147],
       [147, 142,  32,   9, 127],
       [ 32,  31, 114,  28,  34],
       [128, 128,  53, 133,  38],
       [ 17,  79, 132, 105,  42],
       [ 31, 120,   1,  65,  57]])
1
np.split(nd4,indices_or_sections=[1,3])
[array([[ 79,  82,  99,  29, 147]]),
 array([[147, 142,  32,   9, 127],
        [ 32,  31, 114,  28,  34]]),
 array([[128, 128,  53, 133,  38],
        [ 17,  79, 132, 105,  42],
        [ 31, 120,   1,  65,  57]])]
1
2
# 横向切割,将纵长数组切割为纵短数组
np.vsplit(nd4,indices_or_sections=2)
[array([[ 79,  82,  99,  29, 147],
        [147, 142,  32,   9, 127],
        [ 32,  31, 114,  28,  34]]),
 array([[128, 128,  53, 133,  38],
        [ 17,  79, 132, 105,  42],
        [ 31, 120,   1,  65,  57]])]
1
np.vsplit(nd4,indices_or_sections=(2))
[array([[ 79,  82,  99,  29, 147],
        [147, 142,  32,   9, 127],
        [ 32,  31, 114,  28,  34]]),
 array([[128, 128,  53, 133,  38],
        [ 17,  79, 132, 105,  42],
        [ 31, 120,   1,  65,  57]])]
1
np.vsplit(nd4,indices_or_sections=[2])
[array([[ 79,  82,  99,  29, 147],
        [147, 142,  32,   9, 127]]),
 array([[ 32,  31, 114,  28,  34],
        [128, 128,  53, 133,  38],
        [ 17,  79, 132, 105,  42],
        [ 31, 120,   1,  65,  57]])]
1
np.vsplit(nd4,indices_or_sections=(2,5))
[array([[ 79,  82,  99,  29, 147],
        [147, 142,  32,   9, 127]]),
 array([[ 32,  31, 114,  28,  34],
        [128, 128,  53, 133,  38],
        [ 17,  79, 132, 105,  42]]),
 array([[ 31, 120,   1,  65,  57]])]
1
np.vsplit(nd4,indices_or_sections=[2,5])
[array([[ 79,  82,  99,  29, 147],
        [147, 142,  32,   9, 127]]),
 array([[ 32,  31, 114,  28,  34],
        [128, 128,  53, 133,  38],
        [ 17,  79, 132, 105,  42]]),
 array([[ 31, 120,   1,  65,  57]])]
1
nd5.shape
(4, 13)
1
nd5
array([[ 79,  82,  99,  29, 147,  35, 102, 119,  11,  82,  91, 128, 142],
       [147, 142,  32,   9, 127,  99,  53, 140, 121,  84,  68,   6,  47],
       [ 32,  31, 114,  28,  34, 127, 131, 100,  78, 143, 148,  23, 141],
       [128, 128,  53, 133,  38, 117,  85,  48,  49,  69,  95,  94,   0]])
1
np.hsplit(nd5,indices_or_sections=[5])
[array([[ 79,  82,  99,  29, 147],
        [147, 142,  32,   9, 127],
        [ 32,  31, 114,  28,  34],
        [128, 128,  53, 133,  38]]),
 array([[ 35, 102, 119,  11,  82,  91, 128, 142],
        [ 99,  53, 140, 121,  84,  68,   6,  47],
        [127, 131, 100,  78, 143, 148,  23, 141],
        [117,  85,  48,  49,  69,  95,  94,   0]])]
1
2
# np.hsplit	将一个数组水平分割为多个子数组(按列)
np.hsplit(nd5,indices_or_sections=[4,8,12])
[array([[ 79,  82,  99,  29],
        [147, 142,  32,   9],
        [ 32,  31, 114,  28],
        [128, 128,  53, 133]]),
 array([[147,  35, 102, 119],
        [127,  99,  53, 140],
        [ 34, 127, 131, 100],
        [ 38, 117,  85,  48]]),
 array([[ 11,  82,  91, 128],
        [121,  84,  68,   6],
        [ 78, 143, 148,  23],
        [ 49,  69,  95,  94]]),
 array([[142],
        [ 47],
        [141],
        [  0]])]

6. 副本

所有赋值运算不会为ndarray的任何元素创建副本。对赋值后的对象的操作也对原来的对象生效。

  • 可使用copy()函数创建副本
1
nd5
array([[ 79,  82,  99,  29, 147,  35, 102, 119,  11,  82,  91, 128, 142],
       [147, 142,  32,   9, 127,  99,  53, 140, 121,  84,  68,   6,  47],
       [ 32,  31, 114,  28,  34, 127, 131, 100,  78, 143, 148,  23, 141],
       [128, 128,  53, 133,  38, 117,  85,  48,  49,  69,  95,  94,   0]])
1
2
nd7 = nd5
nd7
array([[ 79,  82,  99,  29, 147,  35, 102, 119,  11,  82,  91, 128, 142],
       [147, 142,  32,   9, 127,  99,  53, 140, 121,  84,  68,   6,  47],
       [ 32,  31, 114,  28,  34, 127, 131, 100,  78, 143, 148,  23, 141],
       [128, 128,  53, 133,  38, 117,  85,  48,  49,  69,  95,  94,   0]])
1
id(nd7)
2726732492032
1
id(nd5)
2726732492032
1
2
nd8 = nd5.copy()
id(nd8)
2726756355328

四、ndarray的聚合操作

1、求和np.sum

1
nd5
array([[ 79,  82,  99,  29, 147,  35, 102, 119,  11,  82,  91, 128, 142],
       [147, 142,  32,   9, 127,  99,  53, 140, 121,  84,  68,   6,  47],
       [ 32,  31, 114,  28,  34, 127, 131, 100,  78, 143, 148,  23, 141],
       [128, 128,  53, 133,  38, 117,  85,  48,  49,  69,  95,  94,   0]])
1
nd5.sum()
4388

2、最大值np.max() 和最小值 np.min()

1
nd5.max()
148
1
nd5.min()
0
1
2
# np.prod() 计算所有元素的乘积
nd5.prod()
0
1
2
# mean()函数功能:求取均值
nd5.mean()
84.38461538461539

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 幂运算
1
np.sin(0)
0.0
1
np.pi
3.141592653589793
1
np.sin(np.pi/2)
1.0
1
np.arcsin(1)
1.5707963267948966

五、ndarray的矩阵操作

1. 基本矩阵操作

  1. 算术运算符:
  • 加减乘除
  1. 矩阵积np.dot()
1
2
X = np.array([[1,1,1],[2,-1,1],[3,1,-1]])
X
array([[ 1,  1,  1],
       [ 2, -1,  1],
       [ 3,  1, -1]])
1
2
Y = np.array([8,8,2])
Y
array([8, 8, 2])
1
2
3
# w是我们要求解未知数
w = np.array(['x','y','z'])
w
array(['x', 'y', 'z'], dtype='<U1')
1
2
# 矩阵运算
Xw = Y
1
2
3
4
# 逆矩阵
# np.linalg.inv():矩阵求逆
X_inv = np.linalg.inv(X)
X_inv
array([[ 0. ,  0.2,  0.2],
       [ 0.5, -0.4,  0.1],
       [ 0.5,  0.2, -0.3]])
1
np.dot(X_inv,Y)
array([2., 1., 5.])
1
# 矩阵  逆矩阵(倒数关系),5 1/5

2. 广播机制

【重要】ndarray广播机制的两条规则

  • 规则一:为缺失的维度补1
  • 规则二:假定缺失元素用已有值填充

例1:
m = np.ones((2, 3))

a = np.arange(3)

求M+a

1
nd5
array([[ 79,  82,  99,  29, 147,  35, 102, 119,  11,  82,  91, 128, 142],
       [147, 142,  32,   9, 127,  99,  53, 140, 121,  84,  68,   6,  47],
       [ 32,  31, 114,  28,  34, 127, 131, 100,  78, 143, 148,  23, 141],
       [128, 128,  53, 133,  38, 117,  85,  48,  49,  69,  95,  94,   0]])
1
nd5 + 10
array([[ 89,  92, 109,  39, 157,  45, 112, 129,  21,  92, 101, 138, 152],
       [157, 152,  42,  19, 137, 109,  63, 150, 131,  94,  78,  16,  57],
       [ 42,  41, 124,  38,  44, 137, 141, 110,  88, 153, 158,  33, 151],
       [138, 138,  63, 143,  48, 127,  95,  58,  59,  79, 105, 104,  10]])
1
nd4
array([[ 79,  82,  99,  29, 147],
       [147, 142,  32,   9, 127],
       [ 32,  31, 114,  28,  34],
       [128, 128,  53, 133,  38],
       [ 17,  79, 132, 105,  42],
       [ 31, 120,   1,  65,  57]])
1
2
nd4_1 = np.random.randint(0,10,size=(6,5))
nd4_1
array([[1, 2, 4, 2, 0],
       [3, 2, 0, 7, 5],
       [9, 0, 2, 7, 2],
       [9, 2, 3, 3, 2],
       [3, 4, 1, 2, 9],
       [1, 4, 6, 8, 2]])
1
nd4 + nd4_1
array([[ 80,  84, 103,  31, 147],
       [150, 144,  32,  16, 132],
       [ 41,  31, 116,  35,  36],
       [137, 130,  56, 136,  40],
       [ 20,  83, 133, 107,  51],
       [ 32, 124,   7,  73,  59]])

六、ndarray的排序

1
2
nd4.sort()
nd4
array([[ 29,  79,  82,  99, 147],
       [  9,  32, 127, 142, 147],
       [ 28,  31,  32,  34, 114],
       [ 38,  53, 128, 128, 133],
       [ 17,  42,  79, 105, 132],
       [  1,  31,  57,  65, 120]])
1
np.sort(nd4)
array([[ 29,  79,  82,  99, 147],
       [  9,  32, 127, 142, 147],
       [ 28,  31,  32,  34, 114],
       [ 38,  53, 128, 128, 133],
       [ 17,  42,  79, 105, 132],
       [  1,  31,  57,  65, 120]])

1. 快速排序

np.sort()与ndarray.sort()都可以,但有区别:

  • np.sort()不改变输入
  • ndarray.sort()本地处理,不占用空间,但改变输入

2. 部分排序

np.partition(a,k)

有的时候我们不是对全部数据感兴趣,我们可能只对最小或最大的一部分感兴趣。

  • 当k为正时,我们想要得到最小的k个数
  • 当k为负时,我们想要得到最大的k个数
1
2
nd = np.random.randint(0,10000,size = 100)
nd
array([ 136, 6273, 9584, 3419,  168, 6004, 2852, 9753, 4419, 8039, 8700,
       3186, 5918, 5149, 1777, 3361, 8338, 5393, 4317, 4605, 2562, 6213,
       9100, 4652, 6235,  423, 6854,  967, 4370, 9052, 6187, 5203,  433,
       6237, 1429, 2546,  329, 3612, 8401, 6761, 3968, 8150, 1040, 6250,
       8356, 8798, 7704, 6772, 5311, 9411, 9523, 9144, 6011, 2798, 8352,
       2195, 4680, 6599, 9303, 3085, 5713, 5240,  732, 5028, 8473, 7594,
       4566, 9500, 7444, 3396, 5347, 7034,  595,  647,  573, 6797, 5637,
       8448, 5259, 9220, 6567, 4444, 2989,  586, 5102, 7601,  739, 4882,
       5410,  437, 3898, 1847, 9724, 1020, 6930,  941, 8641, 5610, 9008,
       2107])
1
np.partition(nd,kth=5)[:5]
array([168, 136, 423, 329, 433])
1
np.partition(nd,kth = -5)[-5:]
array([9500, 9584, 9523, 9753, 9724])
1
2
nd2 = np.random.randint(0,2,size = 100)
nd2
array([0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1,
       0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
       1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
       0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0,
       1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0])
1
np.partition(nd2,kth=-6)[-6:]
array([1, 1, 1, 1, 1, 1])
1
np.partition(nd2,kth=6)[:5]
array([0, 0, 0, 0, 0])
作者

Fahsa

发布于

2021-01-07

更新于

2021-01-07

许可协议

评论