numpy基础用法

这篇博客详细介绍了numpy的基础用法,包括定义数组及其属性、数组值的设置、各种运算、数组的拼接与分割、数组赋值等内容。涉及的运算包括加减法、平方运算、三角函数、条件判断、矩阵乘法、求和、求最小值和最大值、索引、排序、转置以及数值处理等,同时讲解了如何进行数组的拼接和分割以及赋值操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy中定义数组和数组的属性

  1. 定义数组
  • 代码
import numpy as np
array = np.array([[1,2,3],
                  [2,3,4])
print(array)
  • 输出结果
[[1 2 3]
 [2 3 4]]
//(输出结果中无逗号)
  1. 显示数组维度
  • 代码
print('number of dim:'array.ndim)
  • 输出结果
number of dim: 2
  1. 显示数组形状
  • 代码
print('shape:', array.shape)
  • 输出结果
shape: (2,3)
  1. 显示数组大小
  • 代码
print('size:', array.size)
  • 输出结果
size: 6

numpy中数组值的设置

  1. 设置和显示数据类型
  • 代码(不设置输出数据类型的长度时)
import numpy as np
a = np.array([2,23,4], dtype=np.int)
print(a.dtype)
  • 输出结果
int64
//(不输入数据长度时默认为64)
  • 代码(设置输出数据类型的长度时)
import numpy as np
a = np.array([2,23,4], dtype=np.int32)
print(a.dtype)
  • 输出结果
int32
  • 若需要存储空间小,则设置小的数据长度(e.g.16位);
  • 若需要存储空间大,则设置大的数据长度(e.g. 64位)
  1. 创建全0矩阵、全1矩阵和空矩阵
    (1)全0矩阵:
  • 代码
a = np.zero( (3,4) )
print(a)
  • 输出结果
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

(2)全1矩阵:

  • 代码
a = np.ones( (3,4), dtype=np.int16 )
print(a)
  • 输出结果
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]

(3)空矩阵:

  • 代码
a = np.empty( (3,4) )
print(a)
  • 输出结果
[[6.23042070e-307 1.89146896e-307 1.37961302e-306 1.05699242e-307]
 [8.01097889e-307 1.78020169e-306 7.56601165e-307 1.02359984e-306]
 [1.29060531e-306 1.24611741e-306 1.11261027e-306 3.01468662e+161]]
//(是十分接近0的数)
  1. 生成有序序列或矩阵(相当于python中的range功能)
  • 代码
a = np.arange(10,20,2)
print(a)
b = np.arange(12).reshape((3,4))
print(b)
  • 输出结果
[10 12 14 16 18]
[[0 1 2 3]
 [4 5 6 7]
 [8 9 10 11]]
  1. 创建linspace()
  • 代码
a = np.linspace(1,10,5)
print(a)
b = np.linspace(1,10,6).reshape((2,3))
print(b)
  • 输出结果
[1. 3.25 5.5 7.75 10.]
[[1. 2.8 4.6]
 [6.4 8.2 10.]]

numpy的运算

  1. 加减法运算
  • 代码
import namoy as np
a = np.array([10,20,30,40])
b = np.arange(4)
print(a,b)
c = a+b
print(c)
  • 输出结果
[10 20 30 40] [0 1 2 3]
[10 21 32 43]
  1. 平方运算:
  • 代码
c = b**2
print(c)
  • 输出结果:
[0 1 4 9]
  1. sin/cos:
c = 10*np.cos(a)
print(c)
  • 输出结果:
[-8.39071529  4.08082062  1.5425145  -6.66938062]
  1. 判断序列中数值大小是否符合条件:
  • 代码
print(b)
print(b==3)
  • 输出结果:
[0 1 2 3]
[False False False  True]
  1. 矩阵乘法:
    (1)逐个相乘:
  • 代码
a = np.array([[1,1],
              [0,1]])
b = np.arrange(4).reshape(2,2)
c = a*b
print(c)
  • 输出结果:
[[0 1]
 [0 3]]

(2)矩阵相乘:

  • 代码
a = np.array([[1,1],
                      [0,1]])
b = np.arrange(4).reshape(2,2)
c_dot = np.dot(a,b)
//或:c_dot = a.dot(b)
print(c_dot)
  • 输出结果:
[[2 4]
 [2 3]
  1. 求和
  • 代码
import numpy as np
a = np.random.random((2,4))
print(a)
print(np.sum(a))
  • 输出结果
[[0.54856051 0.75547687 0.97634788 0.03362161]
 [0.03198332 0.70799137 0.07809093 0.76455108]]
3.8966235679462295
  1. 求最小值
  • 代码
print(np.min(a))
  • 输出结果
0.03198332442659946
//可以在后面加axis限制计算范围(axis=0表示在列数中的结果,axis=1表示在行数中的结果)
  1. 求最大值
  • 代码
print(np.max(a))
  • 输出结果:
0.9763478806201845
  1. 索引
    (1)输出最小值的索引
  • 代码
A = np.arange(2,14).reshape((3,4))
print(np.argmin(A))
  • 输出结果
0

(2)输出最大值的索引

  • 代码
print(np.argmax(A))
  • 输出结果
11

(3)输出非零的数的位置(输出的 第一个array代表行数,第二个array代表列数)

  • 代码
print(np.nonzero(A))
  • 输出结果
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))

(4)一维序列索引

  • 代码
import numpy as np
A = np.arrange(3,15)

*输出结果:

6

(5)二维数组索引

  • 代码
A = np.arange(3,15).reshape((3,4))
print(A[2])
print(A[1][1])
print(A[2,1])

  • 输出结果:
[11 12 13 14]
8
12

(6)用冒号输出所有数

  • 代码
print(A[:,1])
print(A[1, 1:3])
  • 输出结果:
[4 8 12]
[8 9]

(7)用for迭代每一行的数

  • 代码
for row in A:
    print (row)
  • 输出结果:
[2 3 4 5]
[6 7 8 9]
[10 11 12 13]

(8)用for循环迭代每一列的数

  • 代码
for column in A.T:
    print (column)
  • 输出结果:
[ 2  6 10]
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]

(9)迭代每一个项目

  • 代码
for item in A.flat:
    print(item)
  • 输出结果:
2
3
4
5
6
7
8
9
10
11
12
13
  1. 计算平均值
  • 代码
//方法1
print(np.mean(A))
//或:print(A.mean())
//方法2print(np.average(A))
  • 输出结果
7.5
  1. 计算中位数
  • 代码
print(np.median(A))
  1. 累加
  • 代码
print(np.cumsum(A))
  • 输出结果:
[ 2  5  9 14 20 27 35 44 54 65 77 90]
  1. 累差
  • 代码
print(np.diff(A))
  • 输出结果:
[[1 1 1]
 [1 1 1]
 [1 1 1]]
  1. 逐行从小到大排序
  • 代码
print(np.sort(A))
  • 输出结果:
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
  1. 矩阵的转置
  • 代码
print(np.transpose(A))
//或:print(A.T)
//矩阵转置和矩阵相乘
print((A.T).dot(A))
  • 输出结果:
[[ 2  6 10]
 [ 3  7 11]
 [ 4  8 12]
 [ 5  9 13]]
 [[140 158 176 194]
 [158 179 200 221]
 [176 200 224 248]
 [194 221 248 275]]

  1. 设置一个范围,矩阵中大于该范围的值都用范围的最大值表示,小于该范围的值都用范围的最小值表示
  • 代码
print(np.clip(A,5,9))
  • 输出结果:
[[5 5 5 5]
 [6 7 8 9]
 [9 9 9 9]]

数组的拼接

  1. 序列的垂直拼接
  • 代码
import numpy as np
A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A,B))
print(C)
print(A.shape, C.shape)
  • 输出结果:
[[1 1 1]
 [2 2 2]]
(3,) (2, 3)
  1. 序列的水平拼接
  • 代码
A = np.array([1,1,1])
B = np.array([2,2,2])
D = np.hstack((A,B))
print(D)
print(A.shape, D.shape)
  • 输出结果:
[1 1 1 2 2 2]
(3,) (6,)

(3)将横向序列转变为竖向序列:通过np.newaxis改变维度

  • 代码
A = np.array([1,1,1])
A = A[:, np.newaxis]
//或:A = np.array([1,1,1])[:, np.newaxis]
print(A)
  • 输出结果:
[[1]
 [1]
 [1]]

(4)合并时定义合并的维度

  • 代码
A = np.array([1,1,1])[:, np.newaxis]
B = np.array([2,2,2])[:, np.newaxis]
C = np.concatenate((A,B,B,A),axis =1)
print(C)
  • 输出:
[[1 2 2 1]
 [1 2 2 1]
 [1 2 2 1]]

数组的分割

  1. 等量分割
    (1)横向分割
  • 代码
import numpy as np
A = np.arange(12).reshape((3,4))
print(np.split(A,3,axis=0))
//或:print(np.vsplit(A,3))
  • 输出结果:
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]

(2)纵向分割

  • 代码
print(np.split(A,2,axis=1))
//或:print(np.hsplit(A,2))
  • 输出结果:
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
  1. 不等量分割
  • 代码
print(np.array_split(A,3,axis=0))
  • 输出结果:
[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]

数组的赋值

(1)使用“=”赋值:当一个数组的值改变时,被赋值的任一数组的值相应改变

(2)若不想要这种关联性,则采用.copy()方法赋值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值