0
点赞
收藏
分享

微信扫一扫

Numpy的创建、属性、聚合函数、线性代数函数、随机函数


numpy数组与list的区别

numpy.ndarray

list

是否动态可变

否,改变长度的话,会新建一个数组并删除原数组。


类型是否不一样

必须一样

可以是多样的

运行速度

优化过,更快,占用内存更多


新建

import numpy as np

# 深拷贝
# np.array()

# 浅拷贝
array_1_d = np.asarray([1]) # 一维数组
array_2_d = np.asarray([[1, 2], [3, 4]]) # 二维数组

# 全1
np.ones(shape = (2, 3), dtype='int64') * 0.5
# array([[0.5, 0.5, 0.5],
# [0.5, 0.5, 0.5]])

# 全0
np.zeros(shape = (2, 3), dtype='int64')
# array([[0, 0, 0],
# [0, 0, 0]], dtype=int64)

# 等差数列
np.linspace(start=2, stop=10, num=3) # 元素个数:num

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-50, 51, 2)
y = x ** 2

plt.plot(x, y, color="blue")
plt.legend()
plt.show()

Numpy的创建、属性、聚合函数、线性代数函数、随机函数_数组元素

获取属性

# ndim 维度/轴个数
print(array_1_d.ndim) # 1
print(array_2_d.ndim) # 2

# 数组形状
print(array_1_d.shape) # (1,) 向量
print(array_2_d.shape) # (2, 2) 矩阵

# 改变形状
array_2_d.reshape((4,1))
# array([[1],
# [2],
# [3],
# [4]])

# 元素总数,shape元素中乘积
array_2_d.size # 4

# 元素类型
array_2_d.dtype # 元素类型 dtype('int32')
array_2_d_float = array_2_d.astype('float64') # 改变元素类型 dtype('float64')

# 整个数组所占的存储空间
array_2_d.nbytes # 32

# 数组的当前值
array_2_d.flags
# C_CONTIGUOUS : True
# F_CONTIGUOUS : False
# OWNDATA : True
# WRITEABLE : True
# ALIGNED : True
# WRITEBACKIFCOPY : False
# UPDATEIFCOPY : False

读取方式

array_arange = np.arange(6).reshape(2, 3)
# array([[0, 1, 2],
# [3, 4, 5]])

# 行优先
np.reshape(array_arange, (1, 6), 'C')
# array([[0, 3, 1, 4, 2, 5]])

# 列优先
np.reshape(array_arange, (1, 6), 'F')
# array([[0, 3, 1, 4, 2, 5]])

切片

import numpy as np
a = np.arange(10)
s = slice(1,8,2)
print(a[s])
# [1 3 5 7]

聚合函数

import numpy as np
import matplotlib.pyplot as plt

rand_int = np.random.randint(10, size=(4, 3, 2))
print(rand_int, rand_int.shape)
array_max = np.max(rand_int, axis=0)
print(array_max, array_max.shape)

可以发现,形状为(a, b, c)的三维数组,
沿着0轴聚合后,形状变为(b, c);
沿着1轴聚合后,形状变为(a, c);
沿着2轴聚合后,形状变为(a, b)。
更高维以此类推。

聚合函数

函数功能

np.prod()

返回指定轴上的数组元素的乘积

sum()

返回指定轴上的数组元素的总和

cumprod()

返回沿给定轴上的数组元素的累积乘积

cumsum()

返回沿给定轴上的数组元素的累积总和

diff()

返回沿给定轴上的数组差分

gradient()

数组的梯度

cross()

返回两个数组或向量的叉积

mean()

算数平均数

argmin()

求最小值下标

argmax()

求最大值下标

线性代数模块

函数名称

函数功能

dot()

计算两个数组的点积

vdot()

计算两个向量的点积

inner()

计算两个数组的内积

matmul()

计算两个数组的矩阵积

determinant()

计算两个数组的行列式

solve()

计算线性矩阵方程

inv()

计算矩阵的乘法逆矩阵

三角函数模块

函数名称

函数功能

sin(x [, out])

正弦值

cos(x [, out])

余弦值

tan(x [, out])

正切值

arcsin(x [, out])

反正弦值

arccos(x [, out])

反余弦值

arctan(x [, out])

反正切值

随机函数

函数名称

函数功能

seed()

确定随机数生成器

permutation()

返回一个序列的随机排序或返回一个随机排列的范围

normal()

产生正态分布的样本值

binomial()

产生二项式分布的样本值

rand()

返回一组随机值,根据给定维度生成[0, 1)的数据

randn()

返回一个样本,具有标准正态分布

randint(low [, high, size])

返回随机的整数,位于半开区间[low, high)

random_integers(low [, high, size])

返回随机的整数,位于半开区间[low, high]

random()

返回随机的浮点数,位于半开区间[0.0, 1.0)

bytes()

返回随机字节

uniform()

返回均匀分布

poisson()

返回泊松分布

import numpy as np
import matplotlib.pyplot as plt

rand1 = np.random.randint(5, size=(4, 4))
print(rand1, rand1.shape)
rand2 = np.random.randint(5, size=(4, 4))
print(rand2, rand2.shape)

# 点积
result = np.dot(rand1, rand2)
print(result, result.shape)

数据清洗

import numpy as np

arr = np.array([2,3,6,6])
# 去除重复数据
print(np.unique(arr))

list1 = [1,2,3,3]
print(np.unique(list1))

tuple1 = (1,2,3,3)
# 求最大值
print(np.amax(list1))
# 求最小值
print(np.amin(list1))


举报

相关推荐

0 条评论