numpy基本操作(二)

此博客只有代码以及标题,适合使用时查找,不适合用来学习 代码参考 NumPy中文文档https://www.numpy.org.cn/index.html

import numpy as np

创建一个数组

a = np.array([0,1,2,3,4])
b = np.array([0,1,2,3,4])
c = np.arange(0,5,1)
d = np.linspace(0,2*np.pi,5)

print(a)
print(b)
print(c)
print(d)
print(a[3])
[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]
[0.         1.57079633 3.14159265 4.71238898 6.28318531]
3
e = np.array([[11,12,13,14,15],
              [16,17,18,19,20],
              [21,22,23,24,25],
              [26,27,28,29,30],
              [31,32,33,34,35]])
print(e);
print(e[1][2]);
[[11 12 13 14 15]
 [16 17 18 19 20]
 [21 22 23 24 25]
 [26 27 28 29 30]
 [31 32 33 34 35]]
18

数组切片

print(e[0,1:4])
print(e[1:4,0])
print(e[::2,::2])
print(e[:,1])
[12 13 14]
[16 21 26]
[[11 13 15]
 [21 23 25]
 [31 33 35]]
[12 17 22 27 32]

数组性质

print(type(e))
print(e.dtype)
print(e.size)
print(e.shape)
print(e.itemsize)#每项占字节数
print(e.ndim)#数组维度
print(e.nbytes)#总占空间字节数

int32
25
(5, 5)
4
2
100

数组的使用

a = np.arange(25)
a = a.reshape((5,5))
b = np.array([12,32,123,14,12,4,35,32,2,5,23,53,1,3,124,325,15,34,1,23,21,23,34,64,34])
b = b.reshape((5,5))

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a ** 2)
print(a < b)
print(a > b)
print(a.dot(b))
[[ 12  33 125  17  16]
 [  9  41  39  10  14]
 [ 33  64  13  16 138]
 [340  31  51  19  42]
 [ 41  44  56  87  58]]
[[ -12  -31 -121  -11   -8]
 [   1  -29  -25    6    4]
 [ -13  -42   11   10 -110]
 [-310    1  -17   17   -4]
 [  -1   -2  -12  -41  -10]]
[[   0   32  246   42   48]
 [  20  210  224   16   45]
 [ 230  583   12   39 1736]
 [4875  240  578   18  437]
 [ 420  483  748 1472  816]]
[[0.00000000e+00 3.12500000e-02 1.62601626e-02 2.14285714e-01
  3.33333333e-01]
 [1.25000000e+00 1.71428571e-01 2.18750000e-01 4.00000000e+00
  1.80000000e+00]
 [4.34782609e-01 2.07547170e-01 1.20000000e+01 4.33333333e+00
  1.12903226e-01]
 [4.61538462e-02 1.06666667e+00 5.00000000e-01 1.80000000e+01
  8.26086957e-01]
 [9.52380952e-01 9.13043478e-01 6.47058824e-01 3.59375000e-01
  7.05882353e-01]]
[[  0   1   4   9  16]
 [ 25  36  49  64  81]
 [100 121 144 169 196]
 [225 256 289 324 361]
 [400 441 484 529 576]]
[[ True  True  True  True  True]
 [False  True  True False False]
 [ True  True False False  True]
 [ True False  True False  True]
 [ True  True  True  True  True]]
[[False False False False False]
 [ True False False  True  True]
 [False False  True  True False]
 [False  True False  True False]
 [False False False False False]]
[[1109  278  272  267  458]
 [3034 1068 1392  687 1448]
 [4959 1858 2512 1107 2438]
 [6884 2648 3632 1527 3428]
 [8809 3438 4752 1947 4418]]

特殊运算符

a = np.arange(10)

print(a)
print(a.sum())
print(a.min())
print(a.max())
print(a.cumsum())
[0 1 2 3 4 5 6 7 8 9]
45
0
9
[ 0  1  3  6 10 15 21 28 36 45]

花式索引

a = np.arange(0,100,10)
indices = [1,5,6,-2]
b = a[indices]
print(a)
print(b)
[ 0 10 20 30 40 50 60 70 80 90]
[10 50 60 80]

布尔屏蔽

import matplotlib.pyplot as plt

a = np.linspace(0,2 * np.pi,50)
b = np.sin(a)
c = plt.plot(a,b)
mask = b >=0
plt.plot(a[mask],b[mask],'bo')
mask = (b >= 0)&(a <= np.pi/2)
plt.plot(a[mask],b[mask],'go')
plt.show(c)

numpy基本操作(二)_第1张图片

缺省索引

a = np.arange(50,150,10)
b = a[:5]
c = a[a >=50]
print(b)
print(c)
[50 60 70 80 90]
[ 50  60  70  80  90 100 110 120 130 140]

Where函数

a = np.arange(0,100,10)
b = np.where(a < 50)
c = np.where(a >= 50)[0]
print(b)
print(c)
(array([0, 1, 2, 3, 4], dtype=int64),)
[5 6 7 8 9]

你可能感兴趣的:(python)