以下是NumPy中一些常用的操作及其相应的代码示例:
创建NumPy数组:
import numpy as np
# 从Python列表创建一维数组
a = np.array([1, 2, 3, 4, 5])
print(a)
# 从Python列表创建二维数组
b = np.array([[1, 2, 3], [4, 5, 6]])
print(b)
# 用zeros创建一个全为0的数组
c = np.zeros((3, 3))
print(c)
# 用ones创建一个全为1的数组
d = np.ones((2, 2))
print(d)
# 用arange创建一个一维数组
e = np.arange(10)
print(e)
数组的基本属性:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
# 数组的维度
print(a.ndim)
# 数组的形状
print(a.shape)
# 数组中元素的总数
print(a.size)
# 数组中元素的数据类型
print(a.dtype)
数组的索引和切片:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
# 访问单个元素
print(a[0, 0])
# 访问一整行
print(a[0, :])
# 访问一整列
print(a[:, 1])
# 切片操作
print(a[1:, 1:])
数组的运算:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# 数组的加法
print(a + b)
# 数组的减法
print(a - b)
# 数组的乘法
print(a * b)
# 数组的矩阵乘法
print(np.dot(a, b))
数组的统计操作:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
# 数组中所有元素的和
print(a.sum())
# 数组中每一行的和
print(a.sum(axis=1))
# 数组中每一列的和
print(a.sum(axis=0))
# 数组中的最小值
print(a.min())
# 数组中的最大值
print(a.max())
数组的变形和重塑:
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
# 将数组变形成一维数组
b = a.reshape(-1)
print(b)
# 将数组变形成三行两列的二维数组
c = a.reshape(3, 2)
print(c)
# 将数组变形成两行三列的二维数组
d = a.reshape(2, 3)
print(d)
# 将数组转置
e = a.T
print(e)
数组的堆叠和拆分:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# 水平堆叠数组
c = np.hstack((a, b))
print(c)
# 垂直堆叠数组
d = np.vstack((a, b))
print(d)
# 将一维数组分割成多个数组
e = np.array([1, 2, 3, 4, 5, 6])
f = np.split(e, 3)
print(f)
# 将二维数组按行拆分成多个数组
g = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
h = np.split(g, 3, axis=0)
print(h)
# 将二维数组按列拆分成多个数组
i = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
j = np.split(i, 3, axis=1)
print(j)
数组的广播:
a = np.array([[1, 2], [3, 4]])
b = np.array([10, 20])
# 数组的广播
c = a + b
print(c)
# 数组的广播
d = a * 2
print(d)
数组的逻辑运算:
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([2, 4, 6, 8])
# 数组的比较运算
c = a == b
print(c)
# 数组的逻辑运算
d = np.logical_and(a > 1, b < 5)
print(d)
# 数组的逻辑运算
e = np.logical_or(a < 2, b > 7)
print(e)
数组的统计运算:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# 求数组的最大值、最小值、平均值、中位数、标准差、方差
b = np.max(a)
c = np.min(a)
d = np.mean(a)
e = np.median(a)
f = np.std(a)
g = np.var(a)
print(b, c, d, e, f, g)
数组的排序:
import numpy as np
a = np.array([3, 1, 4, 2, 6, 5])
# 对数组进行排序
b = np.sort(a)
print(b)
# 对数组进行逆序排序
c = np.sort(a)[::-1]
print(c)
# 返回数组排序的下标
d = np.argsort(a)
print(d)
矩阵运算:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# 矩阵相乘
c = np.dot(a, b)
print(c)
# 矩阵的迹
d = np.trace(a)
print(d)
# 矩阵的行列式
e = np.linalg.det(a)
print(e)
# 矩阵的逆
f = np.linalg.inv(a)
print(f)
随机数生成:
import numpy as np
# 生成指定范围内的随机整数
a = np.random.randint(0, 10, size=5)
print(a)
# 生成指定范围内的随机浮点数
b = np.random.uniform(0, 1, size=5)
print(b)
# 生成正态分布的随机数
c = np.random.normal(0, 1, size=5)
print(c)
# 生成指定概率分布的随机数
d = np.random.choice([1, 2, 3], size=5, p=[0.1, 0.3, 0.6])
print(d)
数组的复制:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
# 浅复制数组
b = a.view()
print(b)
# 深复制数组
c = a.copy()
print(c)
数组的拼接和分割:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
# 水平拼接数组
c = np.concatenate((a, b), axis=1)
print(c)
# 垂直拼接数组
d = np.concatenate((a, b), axis=0)
print(d)
# 水平分割数组
e = np.hsplit(c, 2)
print(e)
# 垂直分割数组
f = np.vsplit(d, 2)
print(f)
数组的元素操作:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
# 数组的切片操作
b = a[1:4]
print(b)
# 数组的索引操作
c = a[[0, 2, 4]]
print(c)
# 数组的赋值操作
a[2:4] = [6, 7]
print(a)
# 数组的去重操作
d = np.unique([1, 2, 3, 3, 4, 5, 5])
print(d)
数组的线性代数:
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
# 求解线性方程组
c = np.linalg.solve(a, b)
print(c)
# 计算特征值和特征向量
d, e = np.linalg.eig(a)
print(d, e)
# 计算奇异值分解
f, g, h = np.linalg.svd(a)
print(f, g, h)
数组的傅里叶变换:
import numpy as np
import matplotlib.pyplot as plt
# 生成信号
t = np.linspace(0, 1, 1000)
x = np.sin(2 * np.pi * 5 * t) + np.sin(2 * np.pi * 10 * t)
# 对信号进行傅里叶变换
y = np.fft.fft(x)
# 绘制傅里叶变换后的频谱图
freqs = np.fft.fftfreq(len(x), t[1] - t[0])
plt.plot(freqs, np.abs(y))
plt.show()
数组的统计:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
# 数组的和
b = np.sum(a)
print(b)
# 数组的平均值
c = np.mean(a)
print(c)
# 数组的方差和标准差
d = np.var(a)
e = np.std(a)
print(d, e)
# 数组的最小值和最大值
f = np.min(a)
g = np.max(a)
print(f, g)
# 数组的排序
h = np.sort(a)
print(h)
数组的元素比较:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
b = np.array([5, 4, 3, 2, 1])
# 逐个元素比较
c = a == b
print(c)
# 数组比较
d = np.array_equal(a, b)
print(d)
标签:常用,print,数组,import,np,操作,array,numpy
From: https://www.cnblogs.com/bertin/p/17226474.html