numpy学习

python 复制代码
import numpy as np
python 复制代码
## 创建一个一维数组
np.array([1,2,3])
复制代码
array([1, 2, 3])
python 复制代码
np.array([1,2,3])
复制代码
array([1, 2, 3])
python 复制代码
np.array([[1,2,3]])
复制代码
array([[1, 2, 3]])
python 复制代码
np.array([[1,2,3]]).T
复制代码
array([[1],
       [2],
       [3]])
python 复制代码
np.ones(4)
复制代码
array([1., 1., 1., 1.])
python 复制代码
np.zeros(4)
复制代码
array([0., 0., 0., 0.])
python 复制代码
np.random.random(3)
复制代码
array([0.76998129, 0.65607576, 0.23038928])

常见错误

1.保留中括号

python 复制代码
a = np.array([[1,2,3]])
a.dtype
复制代码
dtype('int32')
python 复制代码
a = np.array([[1.,2,3]])
a.dtype
复制代码
dtype('float64')
python 复制代码
a = np.array([[1,2,'3']])
a.dtype
复制代码
dtype('<U11')

常用的api

python 复制代码
## 创建一个数组1-10
np.arange(1,11)
复制代码
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
python 复制代码
np.arange(-2,2,0.5)# 0.5是步长
复制代码
array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5])
python 复制代码
## 把数据切割几段
np.linspace(0,2,5)
复制代码
array([0. , 0.5, 1. , 1.5, 2. ])
python 复制代码
np.linspace(0,100,10)
复制代码
array([  0.        ,  11.11111111,  22.22222222,  33.33333333,
        44.44444444,  55.55555556,  66.66666667,  77.77777778,
        88.88888889, 100.        ])
python 复制代码
## 创建高维度数组
python 复制代码
np.ones([2,3])
复制代码
array([[1., 1., 1.],
       [1., 1., 1.]])
python 复制代码
np.zeros([3,3])
复制代码
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

创建对角矩阵

python 复制代码
a =np.eye(3)
python 复制代码
b = np.array([1,2,3])
python 复制代码
np.dot(a,b)
复制代码
array([1., 2., 3.])
python 复制代码
c = np.array([4,5,6])
python 复制代码
np.diag(c)
复制代码
array([[4, 0, 0],
       [0, 5, 0],
       [0, 0, 6]])
numpy数组的属性
python 复制代码
c = np.array([1,2,3,4])
c.ndim
复制代码
1
  • 数组的形状
python 复制代码
 a= np.array([
     [2,3,4],
     [1,3,7]
 ])
python 复制代码
a.shape
复制代码
(2, 3)
python 复制代码
a.size #数量
复制代码
6
python 复制代码
a.dtype
复制代码
dtype('int32')

数组的运算

python 复制代码
data = np.array([1,2])
ones = np.ones(2)
python 复制代码
data
复制代码
array([1, 2])
python 复制代码
ones
复制代码
array([1., 1.])
python 复制代码
data+ones
复制代码
array([2., 3.])
python 复制代码
data - ones
复制代码
array([0., 1.])
python 复制代码
data * ones
复制代码
array([1., 2.])
python 复制代码
data/ones
复制代码
array([1., 2.])
python 复制代码
data * 3
复制代码
array([3, 6])
python 复制代码
data ** 2
复制代码
array([1, 4], dtype=int32)
python 复制代码
np.sin(data)
复制代码
array([0.84147098, 0.90929743])
python 复制代码
np.exp(data)
复制代码
array([2.71828183, 7.3890561 ])
python 复制代码
np.sqrt(data)
复制代码
array([1.        , 1.41421356])

布尔过滤

python 复制代码
a = np.array([168,159,165,178,190])
python 复制代码
a >=175
复制代码
array([False, False, False,  True,  True])
python 复制代码
a = [10,20,30,40]
python 复制代码
a * 2
复制代码
[10, 20, 30, 40, 10, 20, 30, 40]

索引入门

python 复制代码
a = np.array([1,2,3])
python 复制代码
a[1]
复制代码
2
python 复制代码
#索引范围
a[0:2]
复制代码
array([1, 2])
python 复制代码
a[1:30]
复制代码
array([2, 3])
python 复制代码
a[1:]
复制代码
array([2, 3])
python 复制代码
a[:5]
复制代码
array([1, 2, 3])
python 复制代码
a[:]
复制代码
array([1, 2, 3])
python 复制代码
a[-1]
复制代码
3
python 复制代码
a[-3:-1]
复制代码
array([1, 2])

numpy数组的元素操作

python 复制代码
data = np.array([1,2,3,4,5,6,7,8,9,8,7,6,5,4])
python 复制代码
data.max()
复制代码
9
python 复制代码
data.min()
复制代码
1
python 复制代码
data.sum()
复制代码
75
python 复制代码
data.mean() #求平均值
复制代码
5.357142857142857

二位数组切片

python 复制代码
data = np.array([
    [1,2],
    [3,4],
    [5,6]
])
python 复制代码
data
复制代码
array([[1, 2],
       [3, 4],
       [5, 6]])
python 复制代码
data[0][1]
复制代码
2
python 复制代码
data[0,1]
复制代码
2
python 复制代码
data[1:3]
复制代码
array([[3, 4],
       [5, 6]])
python 复制代码
data[0:2,0:1]
复制代码
array([[1],
       [3]])

numpy 数据形状的改变

python 复制代码
data= np.array([1,2,3,4,5,6])
python 复制代码
data.shape
复制代码
(6,)
python 复制代码
data.reshape(2,3)
复制代码
array([[1, 2, 3],
       [4, 5, 6]])
python 复制代码
data.reshape(3,2)
## 注意行乘以列等于数据的总量
复制代码
array([[1, 2],
       [3, 4],
       [5, 6]])
python 复制代码
data.reshape(3,-1)
复制代码
array([[1, 2],
       [3, 4],
       [5, 6]])

数据降维

python 复制代码
c = np.array([
    [1,2,3],
    [2,3,4]
])
python 复制代码
c.ravel()
复制代码
array([1, 2, 3, 2, 3, 4])
python 复制代码
a = np.array([
[3000,3500],
[4500,5000],
[10000,11000]]
)
python 复制代码
np.sum(a)
复制代码
37000
python 复制代码
np.sum(a,axis=0)
复制代码
array([17500, 19500])
python 复制代码
np.sum(a,axis=1)
复制代码
array([ 6500,  9500, 21000])
python 复制代码
np.max(a)
复制代码
11000
python 复制代码
np.min(a)
复制代码
3000
python 复制代码
np.mean(a)
复制代码
6166.666666666667
python 复制代码
np.median(a)
复制代码
4750.0

方差

python 复制代码
np.var(a)
复制代码
9888888.888888888

标准差

python 复制代码
np.std(a)
复制代码
3144.660377352201

随机数

python 复制代码
np.random.rand(2,3)
复制代码
array([[0.14406139, 0.58255917, 0.56687269],
       [0.45931544, 0.27684285, 0.53764072]])
python 复制代码
np.random.randint(2,6,5)
复制代码
array([2, 3, 2, 4, 5])
python 复制代码
np.random.randn(10)
复制代码
array([ 0.13193888,  0.09997268, -0.071677  , -0.08255967, -0.4487725 ,
       -1.75318345, -0.46725208,  0.34519292, -0.30273565, -0.58354462])

创建正太分布规律的数据

python 复制代码
np.random.normal(175,15,size=(4,4))
复制代码
array([[190.6869503 , 162.3662239 , 155.35991852, 185.87871385],
       [179.38113865, 171.48428447, 179.59261062, 169.09543353],
       [166.4724072 , 185.78899473, 168.35652325, 163.75069691],
       [164.24513277, 186.98536138, 164.74146814, 176.38007905]])
python 复制代码
np.random.randn(4,4)
复制代码
array([[-3.08839751e-01,  7.66951959e-01, -4.02554299e-01,
         6.24276206e-01],
       [-6.56442394e-01, -5.90405577e-01, -2.12262531e+00,
        -2.38134835e-01],
       [-2.17110459e-01, -6.00174700e-01,  1.06536890e-01,
         1.70518271e+00],
       [ 2.33097080e-01, -4.03758442e-01,  2.64799870e-01,
         6.67566195e-04]])
python 复制代码
np.average(np.random.randn(400,400))
复制代码
-0.005990795466101744
python 复制代码
np.std(np.random.randn(400,400))
复制代码
1.0005126387054453

numpy数据打散

python 复制代码
a = np.arange(10)
a
复制代码
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
python 复制代码
np.random.permutation(a)
复制代码
array([6, 1, 7, 5, 9, 4, 2, 3, 8, 0])
python 复制代码
a
复制代码
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

直接修改原始数组

python 复制代码
np.random.shuffle(a)
python 复制代码
a
复制代码
array([2, 0, 9, 1, 7, 6, 4, 3, 8, 5])

numpy指定随机数种子

python 复制代码
np.random.seed(3)
np.random.randint(24)
复制代码
10

numpy逻辑操作

python 复制代码
a = np.array([True,False,True,True,False])
python 复制代码
np.all(a)
复制代码
False
python 复制代码
np.any(a)
复制代码
True
python 复制代码
a = np.array([50,55,65,57,58,66,100])
a >19
复制代码
array([ True,  True,  True,  True,  True,  True,  True])

numpy数据排序

python 复制代码
b = np.random.randn(10)
b
复制代码
array([ 0.87226552, -0.01554175,  1.16668206, -2.15861048,  0.75620445,
       -1.69049879, -0.34879409, -1.72951471,  0.69631161,  0.56496171])
python 复制代码
b.sort()
python 复制代码
b
复制代码
array([-2.15861048, -1.72951471, -1.69049879, -0.34879409, -0.01554175,
        0.56496171,  0.69631161,  0.75620445,  0.87226552,  1.16668206])

不修改元素族排序

python 复制代码
a = np.random.randn(10)
python 复制代码
a
复制代码
array([ 0.47986141, -0.29594453, -0.42030926, -0.85961379, -0.40791451,
        1.24757841, -1.82938368, -1.59651767,  0.69031204,  0.92812246])
python 复制代码
a.argsort()
复制代码
array([6, 7, 3, 2, 4, 1, 0, 8, 9, 5], dtype=int64)
python 复制代码
a
复制代码
array([ 0.47986141, -0.29594453, -0.42030926, -0.85961379, -0.40791451,
        1.24757841, -1.82938368, -1.59651767,  0.69031204,  0.92812246])
python 复制代码
a[a.argsort()]
复制代码
array([-1.82938368, -1.59651767, -0.85961379, -0.42030926, -0.40791451,
       -0.29594453,  0.47986141,  0.69031204,  0.92812246,  1.24757841])

二维数组

python 复制代码
b = np.random.randn(3,3)
python 复制代码
b
复制代码
array([[ 1.58653631,  0.40130586, -1.26968864],
       [ 0.46920629,  1.30385884,  1.23128365],
       [ 0.22948181,  0.27360762, -0.666784  ]])
python 复制代码
b.sort(axis=1)
python 复制代码
b
复制代码
array([[-1.26968864,  0.40130586,  1.58653631],
       [ 0.46920629,  1.23128365,  1.30385884],
       [-0.666784  ,  0.22948181,  0.27360762]])
python 复制代码
b.sort(axis=0)
python 复制代码
b
复制代码
array([[-1.26968864,  0.22948181,  0.27360762],
       [-0.666784  ,  0.40130586,  1.30385884],
       [ 0.46920629,  1.23128365,  1.58653631]])
python 复制代码
b.argsort(axis=1)
复制代码
array([[1, 0],
       [1, 0],
       [1, 0],
       ...,
       [1, 0],
       [0, 1],
       [1, 0]], dtype=int64)
python 复制代码
np.set_printoptions(suppress=True)
totalpoint = 1000
b = np.random.rand(totalpoint,2)
b
复制代码
array([[0.03393228, 0.16615602],
       [0.07044839, 0.25010443],
       [0.37316487, 0.33790676],
       ...,
       [0.2200427 , 0.60226645],
       [0.04297848, 0.38231248],
       [0.45174169, 0.16641803]])
python 复制代码
c = np.sqrt(np.sum(b**2,axis=1))
c
复制代码
array([0.73849201, 0.78324931, 0.2300633 , ..., 0.77652285, 0.78353274,
       0.62629663])
python 复制代码
c < 1
复制代码
array([ True,  True,  True, ..., False,  True,  True])
python 复制代码
np.sum(c<=1)
复制代码
785401387

行向量和列向量

python 复制代码
a = np.array([1,2,3])
python 复制代码
a
复制代码
array([1, 2, 3])
python 复制代码
a.transpose()
复制代码
array([1, 2, 3])

ID数组(一维数组)

python 复制代码
b = np.arange(10)
python 复制代码
b
复制代码
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
python 复制代码
b[::2]
复制代码
array([0, 2, 4, 6, 8])

数组倒序排列

python 复制代码
b
复制代码
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

b[::-1]

2D(二位数组)

python 复制代码
a = np.array([
    [1,2],
    [3,4],
    [5,6]
])
a
复制代码
array([[1, 2],
       [3, 4],
       [5, 6]])
python 复制代码
a[1,1]
复制代码
4
python 复制代码
data = np.arange(25).reshape(5,5)
data
复制代码
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, 24]])
python 复制代码
data[1,2:4]
复制代码
array([7, 8])
python 复制代码
data[3:,3:]
复制代码
array([[18, 19],
       [23, 24]])
python 复制代码
data[::2,2::2]
复制代码
array([[ 2,  4],
       [12, 14],
       [22, 24]])
python 复制代码
data[:,1:2]
复制代码
array([[ 1],
       [ 6],
       [11],
       [16],
       [21]])
python 复制代码
data[[0,2,4],[0,2,4]]
复制代码
array([ 0, 12, 24])
python 复制代码
data[[1,3,4],3:]
复制代码
array([[ 8,  9],
       [18, 19],
       [23, 24]])

数组拆分和合并

python 复制代码
data = np.arange(9).reshape(3,3)
data
复制代码
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
python 复制代码
np.vsplit(data,3)
复制代码
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
python 复制代码
np.hsplit(data,3)
复制代码
[array([[0],
        [3],
        [6]]),
 array([[1],
        [4],
        [7]]),
 array([[2],
        [5],
        [8]])]

数组的合并

python 复制代码
a = np.array([
    [1,2],
    [3,4]
])
b = np.array([
    [7,8],
    [9,10]
])
python 复制代码
np.vstack((a,b))
复制代码
array([[ 1,  2],
       [ 3,  4],
       [ 7,  8],
       [ 9, 10]])
python 复制代码
np.hstack((a,b))
复制代码
array([[ 1,  2,  7,  8],
       [ 3,  4,  9, 10]])

numpy 高级操作,索引数组

python 复制代码
b = np.array([1,3,5,2,4,6])
python 复制代码
b[np.argsort(b)]
复制代码
array([1, 2, 3, 4, 5, 6])
python 复制代码
## mnumpy广播机制
python 复制代码
a = np.arange(3)
python 复制代码
a + 1
复制代码
array([1, 2, 3])

其他常用的api

python 复制代码
b = np.array([1,2,3,4,5,6,7,8,9])
python 复制代码
np.argmax(b)
复制代码
8
python 复制代码
np.argmin(b)
复制代码
0
python 复制代码
np.nonzero(b)
复制代码
(array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int64),)
python 复制代码
np.count_nonzero(b)
复制代码
9
python 复制代码
相关推荐
猪在黑魔纹里15 小时前
解决VSCode无法高亮、解析numpy中的部分接口(如pi、deg2rad)
ide·vscode·python·numpy
九死九歌18 小时前
【Sympydantic】使用sympydantic,利用pydantic告别numpy与pytorch编程中,tensor形状带来的烦人痛点!
开发语言·pytorch·python·机器学习·numpy·pydantic
qq19226382 天前
探索图像滤波去噪:MATLAB GUI的奇妙之旅
numpy
Python大数据分析@2 天前
Numpy基础20问
numpy
Cat God 0072 天前
CentOS 搭建 SFTP 服务器(二)
服务器·centos·numpy
fresh hacker3 天前
【Python数据分析】速通NumPy
开发语言·python·数据挖掘·数据分析·numpy
maycho1233 天前
探索 Buck DCDC:自适应恒定导通时间控制的降压变换器之旅
numpy
裤裤兔3 天前
python2与python3的兼容
开发语言·python·numpy
工具人55555 天前
numpy如何学
numpy
与代码不die不休6 天前
Numpy学习——创建ndarray的方法
学习·numpy