Numpy

numpy 基础

N 维数组

创建

import numpy as np

print("一维数组")
x = np.array([1, 2, 3, 4, 5])
print(x)
print(x.dtype)

print("二维数组")
x = np.array([[1, 2], [3, 4], [5, 6]])
print(x)
print(x.ndim) # 维度
print(x.shape) # 各维度的长度

print("创建数组 (zeros, ones, empty)")
x = np.zeros(6)
print(x)
x = np.zeros((2, 3))
print(x)
x = np.ones((2, 3))
print(x)
x = np.empty((3, 3))
print(x)

print("使用arrange生成连续元素")
print(np.arange(6))
print(np.arange(0, 6, 2))

数据类型

import numpy as np
x = np.array([1, 2.6, 3], dtype = np.int64)
print(x)
print(x.dtype)

x = np.array([1, 2, 3], dtype = np.float64)
print(x)
print(x.dtype)

x = np.array([1, 2.6, 3], dtype = np.float64)
y = x.astype(np.int32)
print(y)
print(x)
z = y.astype(np.float64)
print(z)

print("将字符串元素转化为数值元素")
x = np.array(['1', '2', '3'], dtype = np.str_)
y = x.astype(np.int32)
print(x)
print(y)

print("使用其他数组的数据类型作为参数")
x = np.array([1., 2.6, 3.], dtype = np.float32)
y = np.arange(3, dtype = np.int32)
print(y.astype(x.dtype))

Ndarray 的矢量化计算

import numpy as np
print("ndarray数组与标量/数组的运算")
x = np.array([1, 2, 3])
print(x * 2)
print(x > 2)

y = np.array([3, 4, 5])
print(x + y)
print(x > y)

Ndarray 的基本索引和切片

import numpy as np
print("Ndarray 的基本索引")
x = np.array([[1, 2], [3, 4], [5, 6]])
print(x[0])
print(x[0][1])
print(x[0, 1]) # 同 x[0][1]
x = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(x[0])
y = x[0].copy()
z = x[0] # 注意 z 与 x[0] 指向同一个内存值
print(y)
print(y[0, 0])
y[0, 0] = 0
z[0, 0] = -1
print(y)
print(x[0])
print(z)
import numpy as np

print("Ndarray 的切片")
x = np.array([1, 2, 3, 4, 5])
print(x[1:3])
print(x[:3])
print(x[1:])
print(x[0:4:2]) # [1, 3] 下标递增 2

x = np.array([[1, 2], [3, 4], [5, 6]])
print(x[:2])
print(x[:2, :1]) # [[1], [3]]
x[:2, :1] = 0
print(x)
x[:2, :1] = [[8], [6]]

数组的转置和轴对换

import numpy as np
print("ndarray 的数组转置和轴对换")
k = np.arange(9)
m = k.reshape(3, 3)
print(k)
print(m)

print(m.T) # 转置(矩阵)数组:T 属性 mT[x][y] = m[y][x]
print(np.dot(m, m.T)) # 点乘
k = np.arange(8).reshape((2, 2, 2))
print(k)

m = k.transpose(1, 0, 2) # m[y][x][z] = k[x][y][z]
print(m)

#轴交换
m = k.swapaxes(0, 1) # m[y][x][z] = k[x][y][z]

# 利用轴交换进行数组矩阵转置

m = np.arange(9).reshape(3, 3)
print(m)
print(m.swapaxes(0, 1))

Ndarray 的通用函数

通用函数(ufunc)是可以对数组中每个元素进行操作的函数。

abs, sqrt, square, exp, log, log10, log2, log1p, sign, ceil, floor, rint(四舍五入保留 dtype), modf(将数组的小数部分和整数部分以两个独立数组的形式返回), isnan, isfinite, isinf, cos...

add, subtract, mutiply, divide, floor_divide, power, maximum, minimum, mod, copysign, greater(比较), logical_and...

Ndarray 中的常用统计方法

import numpy as np
print("Ndarray 中的基本统计方法")
x = np.array([[1, 2], [3, 3], [1, 2]])
print(x)
print(x.mean())
print(x.mean(axis = 1))
print(x.mean(axis = 0))
# sum, max 同理

print(x.cumsum()) # 所有元素的累计和
print(x.cumprod()) # 所有元素的累计积

Numpy 中的线性代数

import numpy as np
import numpy.linalg as nla

print("矩阵点乘")
x = np.array([[1, 2], [3, 4]])
y = np.array([[1, 2], [3, 4]])
print(x.dot(y))
print(np.dot(x, y))

print("矩阵求逆")
x = np.array([[1, 1], [1, 2]])
y = nla.inv(x)
print(x.dot(y)) # 单位矩阵
print(nla.det(x)) # 求行列式
posted @ 2024-09-22 23:17  mklzc  阅读(2)  评论(0编辑  收藏  举报