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 小时前
websockets库使用(基于Python)
开发语言·python·numpy
A_lvvx2 天前
07_矩形圆形绘制
开发语言·python·numpy
星霜旅人3 天前
Python数据分析篇--NumPy--进阶
python·数据分析·numpy
敲代码不忘补水5 天前
Python NumPy 数据分析:处理复杂数据的高效方法
python·数据分析·numpy
Thetoicxdude5 天前
[Day 80] 區塊鏈與人工智能的聯動應用:理論、技術與實踐
人工智能·web3·numpy
Thetoicxdude6 天前
[Day 82] 區塊鏈與人工智能的聯動應用:理論、技術與實踐
人工智能·web3·numpy
凭栏落花侧6 天前
数据预处理:数据挖掘的第一步
人工智能·python·数据挖掘·conda·numpy·pandas·pip
cndes7 天前
Numpy 和 Pandas的区别与高效协同
numpy·pandas
跟德姆(dom)一起学AI7 天前
0基础跟德姆(dom)一起学AI 数据处理和统计分析08-日期类型处理,Matplotlib介绍
开发语言·人工智能·python·信息可视化·numpy·matplotlib
deephub8 天前
8种数值变量的特征工程技术:利用Sklearn、Numpy和Python将数值转化为预测模型的有效特征
人工智能·python·机器学习·numpy·sklearn·特征工程