参考Numpy50-100题目,有一两个没有调试出来,有的理解也不透彻。 有理解错误的地方望评论指正。
ps:编译器使用的Jupyter,有时候编译跟pycharm不同,大家可以参考一下,自己进行编译,有的random的数组肯定不同,结果也不同。
- 对一个表示坐标形状为(10, 2)的随机向量,找到点与点的距离
import numpy as np
z = np.random.rand(10, 2)
z
array([[0.04532245, 0.03630593],
[0.48843139, 0.05627583],
[0.95156374, 0.76969794],
[0.31074912, 0.345851 ],
[0.7717821 , 0.0263965 ],
[0.8524816 , 0.46817301],
[0.79829407, 0.99267914],
[0.72832911, 0.31756134],
[0.18366014, 0.62556464],
[0.61180644, 0.13956454]])
#atleast_2d(*arys):将一个或多个类似数组的序列,
#非数组将转化为数组,保留已经具有二维或更多维的数组
#返回值为一个数组或数组列表
x = np.atleast_2d(z[:, 0])
x
array([[0.04532245, 0.48843139, 0.95156374, 0.31074912, 0.7717821 ,
0.8524816 , 0.79829407, 0.72832911, 0.18366014, 0.61180644]])
#z[:, 1]:前面为所有行,第一列
y = np.atleast_2d(z[:, 1])
y
array([[0.03630593, 0.05627583, 0.76969794, 0.345851 , 0.0263965 ,
0.46817301, 0.99267914, 0.31756134, 0.62556464, 0.13956454]])
x_y = np.sqrt((x - x.T)**2 + (y - y.T)**2)
x_y
array([[0. , 0.44355872, 1.16582036, 0.40776154, 0.72652724,
0.91543163, 1.21721648, 0.73864924, 0.60527939, 0.57581807],
[0.44355872, 0. , 0.85056609, 0.33974221, 0.28492174,
0.54971978, 0.98633971, 0.35471261, 0.64573622, 0.14885701],
[1.16582036, 0.85056609, 0. , 0.76830307, 0.76473425,
0.31738707, 0.27057754, 0.5042432 , 0.78131322, 0.71589324],
[0.40776154, 0.33974221, 0.76830307, 0. , 0.56089445,
0.55537082, 0.8099918 , 0.41853715, 0.30723172, 0.36495152],
[0.72652724, 0.28492174, 0.76473425, 0.56089445, 0. ,
0.44908673, 0.96664627, 0.29438942, 0.83957721, 0.19595719],
[0.91543163, 0.54971978, 0.31738707, 0.55537082, 0.44908673,
0. , 0.5272978 , 0.19518636, 0.68709116, 0.40731813],
[1.21721648, 0.98633971, 0.27057754, 0.8099918 , 0.96664627,
0.5272978 , 0. , 0.67873347, 0.71592452, 0.8732595 ],
[0.73864924, 0.35471261, 0.5042432 , 0.41853715, 0.29438942,
0.19518636, 0.67873347, 0. , 0.62572383, 0.21274491],
[0.60527939, 0.64573622, 0.78131322, 0.30723172, 0.83957721,
0.68709116, 0.71592452, 0.62572383, 0. , 0.64769232],
[0.57581807, 0.14885701, 0.71589324, 0.36495152, 0.19595719,
0.40731813, 0.8732595 , 0.21274491, 0.64769232, 0. ]])
- 如何将32位的浮点数(float)转换为对应的整数(integer)
arr = np.arange(5, dtype = np.float32)
arr = arr.astype(np.int32, copy = False)
arr
array([0, 1, 2, 3, 4])
arr.dtype
dtype(‘int32’)
- 如何读取以下文件
from io import StringIO
s = StringIO("""1, 2, 3, 4, 5\n
6, , , 7, 8\n
, , 9, 10, 11\n""")
arr = np.genfromtxt(s, delimiter=",", dtype=np.int)
arr
array([[ 1, 2, 3, 4, 5],
[ 6, -1, -1, 7, 8],
[-1, -1, 9, 10, 11]])
- 对于numpy数组,enumerate的等价操作是什么
#enumerate(sequence, [start = 0])
#sequence: 序列、迭代器或者其他支持迭代的对象
#start: 下标起始位置
#np.ndenumerate(arr)
#np.ndenumerate()效果等同与enumerate
arr = np.arange(9).reshape(3, 3)
for index, value in np.ndenumerate(arr):
print(index, value)
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
#np.nindex(*shape)
#用于求数列中元素的下标
arr = np.arange(9).reshape(3, 3)
for index in np.ndindex(arr.shape):
print(index, arr[index])
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
- 生成一个通用的二维Gaussian-like数组
#10*10
x, y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
d = np.sqrt(x*x + y*y)
a, b = 1.0, 0.0
G = np.exp(-((d-a)**2 / (2.0 * b**2)))
G
array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
减去一个矩阵中的每一行的平均值
arr = np.random.rand(3, 3)
arr
array([[0.33241387, 0.35774673, 0.51174137],
[0.54694019, 0.17902232, 0.47671356],
[0.72893273, 0.04262465, 0.84865213]])
# axis=1 以竖轴为基准 ,同行相加
# keepdims主要用于保持矩阵的二维特性
y = arr - arr.mean(axis=1, keepdims=True)
y
array([[-0.06822012, -0.04288726, 0.11110738],
[ 0.14604817, -0.2218697 , 0.07582154],
[ 0.18886289, -0.49744519, 0.30858229]])
- 如何通过第n列对一个数组进行排序
arr = np.random.randint(0, 10, (3, 3))
arr
array([[9, 7, 8],
[3, 8, 3],
[8, 2, 6]])
# argsort()返回的是索引
arr[arr[:1].argsort()]
array([[[3, 8, 3],
[8, 2, 6],
[9, 7, 8]]])
- 如何检查一个二维数组是否有空列
arr = np.random.randint(0, 10, (3, 10))
arr
array([[8, 0, 3, 2, 3, 6, 2, 1, 4, 7],
[9, 5, 8, 6, 7, 7, 3, 8, 5, 1],
[3, 2, 6, 3, 8, 2, 1, 5, 0, 2]])
(~arr.any()).any()
False
(~arr.any(axis=0)).any()
False
- 从数组中的给定值中查找最近的值
arr_1 = np.random.uniform(0, 1, 10)
arr_2 = 0.5
m = arr.flat[np.abs(arr_1 - arr_2).argmin()]
m
4
- 考虑形状为(1,3)和(3,1)的两个数组,如何使用迭代器计算它们的和
A = np.arange(3).reshape(1, 3)
B = np.arange(3).reshape(3, 1)
it = np.nditer([A, B, None])
for x,y,z in it: z[ ...] = x+y
print(it.operands[2])
[[0 1 2]
[1 2 3]
[2 3 4]]
- 创建具有名称属性的数组类
class NamedArrary(np.ndarray):
def __new__(cls, array, name="no name"):
obj = np.asarray(array).view(cls)
obj.name = name
return obj
def __array_finalize__(self, obj):
if obj is None: return
self.info = getattr(obj, "name", "no name")
Arr = NamedArrary(np.arange(10), "range_10")
Arr.name
‘range_10’
- 考虑给定的向量,如何对第二向量索引的每个元素加1(小心重复索引)
arr_1 = np.ones(10)
arr_1
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
arr_2 = np.random.randint(0, len(arr_1), 20)
arr_2
array([8, 6, 7, 2, 0, 6, 3, 1, 8, 2, 2, 2, 0, 0, 6, 8, 8, 0, 3, 1])
# np.bincount(x)
# x:数组;
# 加上bincount是将x数组中最大值+1设为数组的长度,索引为0~最大值
# 输出:索引值对应x数组中对应值的个数
# np.bincount(x, minlength=num)
# 加上minlength是给bincount中的数组设定一个长度值,如果数组中最大值+1大于minlength的值,则minlength失去作用,如果minlength大于最大值+1,新的长度就为minlength
# 输出:索引值对应x数组中对应值的个数
x = np.bincount(arr_2, minlength = len(arr_1))
x
array([4, 2, 4, 2, 0, 0, 3, 1, 4, 0], dtype=int64)
arr_1 += np.bincount(arr_2, minlength = len(arr_1))
arr_1
array([5., 3., 5., 3., 1., 1., 4., 2., 5., 1.])
# np.add.at(x,y,1)
# x被加数组
# y提供索引值
# 1:加的数值
arr_4 = np.ones(10)
arr_3 = np.arange(10)
np.add.at(arr_4, arr_3, 1)
print(arr_4)
[2. 2. 2. 2. 2. 2. 2. 2. 2. 2.]
- 如何基于索引列表(I)将向量(X)的元素累积到数组(F)
X = [1, 2, 3, 4, 5, 6]
I = [1, 3, 9, 3, 4, 1]
# 数字0在X中不存在,所以为0
# 数字1在I中的index=0,X[0] = 1
F = np.bincount(I, weights = X)
F
array([0., 7., 0., 6., 5., 0., 0., 0., 0., 3.])
- 考虑(dtype=ubyte)的(w,h,3)图像,计算唯一颜色的数量
w, h = 256, 256
I = np.random.randint(0, 4, (h, w, 3)).astype(np.ubyte)
# np.unique([数组]):将数组中的重复数字去除,并进行排序后输出
# 将I输出位几行三列的矩阵,
colors = np.unique(I.reshape(-1, 3), axis=0)
colors
array([[0, 0, 0],
[0, 0, 1],
[0, 0, 2],
[0, 0, 3],
[0, 1, 0],
[0, 1, 1],
[0, 1, 2],
[0, 1, 3],
[0, 2, 0],
[0, 2, 1],
[0, 2, 2],
[0, 2, 3],
[0, 3, 0],
[0, 3, 1],
[0, 3, 2],
[0, 3, 3],
[1, 0, 0],
[1, 0, 1],
[1, 0, 2],
[1, 0, 3],
[1, 1, 0],
[1, 1, 1],
[1, 1, 2],
[1, 1, 3],
[1, 2, 0],
[1, 2, 1],
[1, 2, 2],
[1, 2, 3],
[1, 3, 0],
[1, 3, 1],
[1, 3, 2],
[1, 3, 3],
[2, 0, 0],
[2, 0, 1],
[2, 0, 2],
[2, 0, 3],
[2, 1, 0],
[2, 1, 1],
[2, 1, 2],
[2, 1, 3],
[2, 2, 0],
[2, 2, 1],
[2, 2, 2],
[2, 2, 3],
[2, 3, 0],
[2, 3, 1],
[2, 3, 2],
[2, 3, 3],
[3, 0, 0],
[3, 0, 1],
[3, 0, 2],
[3, 0, 3],
[3, 1, 0],
[3, 1, 1],
[3, 1, 2],
[3, 1, 3],
[3, 2, 0],
[3, 2, 1],
[3, 2, 2],
[3, 2, 3],
[3, 3, 0],
[3, 3, 1],
[3, 3, 2],
[3, 3, 3]], dtype=uint8)
- 考虑到一个四维数组,如何一次得到最后两个轴的和?
# 每个数组是3行4列,
# 是一个三行四列的四维数组
arr = np.random.randint(0, 10, (3, 4, 3, 4))
arr
array([[[[2, 0, 6, 8],
[6, 2, 0, 4],
[8, 2, 2, 4]],[[0, 7, 3, 4], [9, 4, 3, 1], [6, 4, 4, 0]], [[6, 4, 4, 9], [7, 6, 5, 0], [2, 2, 8, 9]], [[3, 4, 1, 6], [1, 8, 6, 4], [7, 1, 2, 2]]], [[[9, 5, 3, 7], [3, 8, 5, 6], [5, 0, 2, 9]], [[6, 1, 8, 9], [2, 6, 2, 2], [1, 3, 8, 1]], [[5, 9, 5, 8], [4, 8, 2, 1], [5, 7, 2, 5]], [[6, 0, 7, 7], [9, 5, 9, 6], [2, 4, 6, 3]]], [[[7, 3, 8, 0], [9, 5, 7, 3], [4, 8, 1, 5]], [[6, 0, 6, 0], [1, 4, 4, 9], [8, 2, 0, 1]], [[4, 7, 8, 3], [6, 0, 4, 6], [8, 0, 8, 5]], [[7, 8, 2, 9], [3, 5, 1, 7], [4, 4, 1, 8]]]])
sum = arr.sum(axis = (-2, -1))
sum
array([[44, 45, 62, 45],
[62, 49, 61, 64],
[60, 41, 59, 59]])
- 考虑一维向量D,如何使用描述子集指数的相同大小的向量S计算D子集的平均值?
import numpy as np
D = np.random.uniform(0, 10, 100).astype(int)
D
array([4, 3, 6, 1, 9, 4, 7, 3, 7, 1, 8, 1, 3, 3, 3, 0, 7, 7, 7, 8, 2, 9, 6, 7, 5, 7, 3, 6, 0, 1, 5, 1, 9, 6, 0, 3, 8, 3, 0, 1, 1, 2, 5, 5, 6, 5, 2, 3, 9, 9, 1, 4, 1, 4, 1, 8, 2, 7, 2, 1, 5, 1, 7, 0, 8, 1, 5, 1, 9, 0, 8, 9, 0, 9, 9, 1, 9, 1, 1, 0, 2, 4, 9, 0, 4, 6, 1, 4, 8, 6, 7, 6, 7, 3, 9, 8, 9, 4, 3, 9])
S = np.random.uniform(0, 10, 100).astype(int)
S
array([6, 7, 5, 9, 0, 8, 2, 2, 2, 5, 1, 9, 5, 8, 3, 3, 9, 0, 3, 4, 6, 3, 2, 2, 3, 3, 5, 1, 0, 5, 8, 9, 0, 7, 0, 3, 6, 9, 2, 8, 9, 3, 7, 5, 8, 4, 7, 5, 6, 9, 5, 8, 9, 5, 3, 1, 7, 3, 4, 0, 9, 2, 0, 2, 8, 6, 6, 7, 5, 3, 0, 5, 7, 1, 6, 4, 4, 8, 5, 1, 5, 4, 7, 1, 6, 9, 2, 2, 9, 3, 6, 5, 2, 1, 8, 5, 9, 0, 0, 1])
- 长度为10。0,1,2,3…对应的个数
D_counts = np.bincount(S)
D_counts
array([10, 8, 11, 12, 6, 15, 9, 8, 9, 12], dtype=int64)
D_sums = np.bincount(S,weights=D)
D_sums
array([48., 43., 43., 50., 29., 62., 49., 28., 41., 52.])
D_means = D_sums / D_counts
D_means
array([4.8 , 5.375 , 3.90909091, 4.16666667, 4.83333333, 4.13333333, 5.44444444, 3.5 , 4.55555556, 4.33333333])
- 如何得到点积的对角线?
# 0-1的5*5矩阵
A = np.random.uniform(0, 1, (5, 5))
B = np.random.uniform(0, 1, (5, 5))
A
array([[0.87346676, 0.3848573 , 0.55148247, 0.38211038, 0.30442977],
[0.55811857, 0.92485284, 0.07490107, 0.58891012, 0.29474163],
[0.59893861, 0.35281021, 0.33163623, 0.60095118, 0.18287727],
[0.14060577, 0.02790482, 0.15178205, 0.64045809, 0.01638634],
[0.50444417, 0.68255241, 0.92915858, 0.8889811 , 0.67612398]])
B
array([[0.75807715, 0.71772088, 0.26447386, 0.07014307, 0.50757933],
[0.22668815, 0.57049467, 0.66611432, 0.46188566, 0.86932795],
[0.06744027, 0.44699658, 0.58535483, 0.21537955, 0.42344384],
[0.60086082, 0.32688327, 0.7076823 , 0.16731335, 0.9703401 ],
[0.26999782, 0.87086296, 0.72556407, 0.06444064, 0.7733776 ]])
# A * B
np.dot(A, B)
array([[1.09838044, 1.48299725, 1.30147707, 0.44135557, 1.61765892],
[1.07123408, 1.41086192, 1.4381244 , 0.59998244, 1.91839594],
[0.96684969, 1.13508928, 1.1455121 , 0.38872892, 1.47570617],
[0.51240239, 0.4083066 , 0.6097509 , 0.16365524, 0.79403319],
[1.31650277, 2.0461777 , 2.25164515, 0.74307446, 2.62836694]])
# 对角线
np.diag(np.dot(A, B))
array([1.09838044, 1.41086192, 1.1455121 , 0.16365524, 2.62836694])
- 考虑向量[1, 2, 3,4, 5 ],如何构建一个新的向量,在每个值之间交错3个连续零点?
arr = np.array([1,2,3,4,5])
arr
array([1, 2, 3, 4, 5])
a = 3
# 5个元素,4个空,每个空3个0
arr_1 = np.zeros(len(arr) + (len(arr) - 1) * a)
arr_1
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
# 隔4个元素对应一个arr的元素
arr_1[::a+1] = arr
arr_1
array([1., 0., 0., 0., 2., 0., 0., 0., 3., 0., 0., 0., 4., 0., 0., 0., 5.])
- 考虑一个维数数组(5,5,3),如何通过一个维数为(5,5)的数组对其进行多重化?
# 5个5*3的矩阵
arr =np.ones((5, 5, 3))
arr
array([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],[[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]])
B = 2 * np.ones((5, 5))
B
array([[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.],
[2., 2., 2., 2., 2.]])
C = A * B[:, :, None]
C
array([[[1.74693352, 0.7697146 , 1.10296495, 0.76422076, 0.60885954],
[1.11623713, 1.84970567, 0.14980213, 1.17782024, 0.58948327],
[1.19787723, 0.70562042, 0.66327247, 1.20190235, 0.36575455],
[0.28121155, 0.05580963, 0.3035641 , 1.28091617, 0.03277268],
[1.00888834, 1.36510481, 1.85831715, 1.77796221, 1.35224795]],
[[1.74693352, 0.7697146 , 1.10296495, 0.76422076, 0.60885954],
[1.11623713, 1.84970567, 0.14980213, 1.17782024, 0.58948327],
[1.19787723, 0.70562042, 0.66327247, 1.20190235, 0.36575455],
[0.28121155, 0.05580963, 0.3035641 , 1.28091617, 0.03277268],
[1.00888834, 1.36510481, 1.85831715, 1.77796221, 1.35224795]],
[[1.74693352, 0.7697146 , 1.10296495, 0.76422076, 0.60885954],
[1.11623713, 1.84970567, 0.14980213, 1.17782024, 0.58948327],
[1.19787723, 0.70562042, 0.66327247, 1.20190235, 0.36575455],
[0.28121155, 0.05580963, 0.3035641 , 1.28091617, 0.03277268],
[1.00888834, 1.36510481, 1.85831715, 1.77796221, 1.35224795]],
[[1.74693352, 0.7697146 , 1.10296495, 0.76422076, 0.60885954],
[1.11623713, 1.84970567, 0.14980213, 1.17782024, 0.58948327],
[1.19787723, 0.70562042, 0.66327247, 1.20190235, 0.36575455],
[0.28121155, 0.05580963, 0.3035641 , 1.28091617, 0.03277268],
[1.00888834, 1.36510481, 1.85831715, 1.77796221, 1.35224795]],
[[1.74693352, 0.7697146 , 1.10296495, 0.76422076, 0.60885954],
[1.11623713, 1.84970567, 0.14980213, 1.17782024, 0.58948327],
[1.19787723, 0.70562042, 0.66327247, 1.20190235, 0.36575455],
[0.28121155, 0.05580963, 0.3035641 , 1.28091617, 0.03277268],
[1.00888834, 1.36510481, 1.85831715, 1.77796221, 1.35224795]]])
- 如何交换数组的两行?
A = np.arange(25).reshape(5, 5)
A
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]])
A[[0, 1]] = A[[1, 0]]
A
array([[ 5, 6, 7, 8, 9],
[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
- 考虑一组10个三元组,描述10个三角形(具有共用顶点),找到组成所有三角形的唯一线段集
arr = np.random.randint(0, 100, (10, 3))
arr
array([[35, 35, 24],
[ 1, 35, 84],
[62, 1, 27],
[ 0, 88, 2],
[48, 56, 61],
[61, 91, 57],
[33, 93, 29],
[81, 96, 48],
[38, 88, 43],
[95, 70, 64]])
# repeat(x,axis=i)
# 复制行/列x次
# axis=0,沿着Y轴复制,行
# axis=1,沿着X轴复制,列
arr = arr.repeat(2, axis=1)
arr
array([[35, 35, 35, 35, 24, 24],
[ 1, 1, 35, 35, 84, 84],
[62, 62, 1, 1, 27, 27],
[ 0, 0, 88, 88, 2, 2],
[48, 48, 56, 56, 61, 61],
[61, 61, 91, 91, 57, 57],
[33, 33, 93, 93, 29, 29],
[81, 81, 96, 96, 48, 48],
[38, 38, 88, 88, 43, 43],
[95, 95, 70, 70, 64, 64]])
# arr在竖直方向翻动,意思就是列的变换
# -1是将第一个移动到最后一个
A = np.roll(arr, -1, axis=1)
A
array([[35, 35, 35, 24, 24, 35],
[ 1, 35, 35, 84, 84, 1],
[62, 1, 1, 27, 27, 62],
[ 0, 88, 88, 2, 2, 0],
[48, 56, 56, 61, 61, 48],
[61, 91, 91, 57, 57, 61],
[33, 93, 93, 29, 29, 33],
[81, 96, 96, 48, 48, 81],
[38, 88, 88, 43, 43, 38],
[95, 70, 70, 64, 64, 95]])
B = A.reshape(len(A)*3, 2)
B
array([[35, 35],
[35, 24],
[24, 35],
[ 1, 35],
[35, 84],
[84, 1],
[62, 1],
[ 1, 27],
[27, 62],
[ 0, 88],
[88, 2],
[ 2, 0],
[48, 56],
[56, 61],
[61, 48],
[61, 91],
[91, 57],
[57, 61],
[33, 93],
[93, 29],
[29, 33],
[81, 96],
[96, 48],
[48, 81],
[38, 88],
[88, 43],
[43, 38],
[95, 70],
[70, 64],
[64, 95]])
D = np.sort(B, axis=1)
D
array([[35, 35],
[24, 35],
[24, 35],
[ 1, 35],
[35, 84],
[ 1, 84],
[ 1, 62],
[ 1, 27],
[27, 62],
[ 0, 88],
[ 2, 88],
[ 0, 2],
[48, 56],
[56, 61],
[48, 61],
[61, 91],
[57, 91],
[57, 61],
[33, 93],
[29, 93],
[29, 33],
[81, 96],
[48, 96],
[48, 81],
[38, 88],
[43, 88],
[38, 43],
[70, 95],
[64, 70],
[64, 95]])
G = D.view(dtype=[('p0', D.dtype), ('p1', D.dtype)])
G
array([[(35, 35)],
[(24, 35)],
[(24, 35)],
[( 1, 35)],
[(35, 84)],
[( 1, 84)],
[( 1, 62)],
[( 1, 27)],
[(27, 62)],
[( 0, 88)],
[( 2, 88)],
[( 0, 2)],
[(48, 56)],
[(56, 61)],
[(48, 61)],
[(61, 91)],
[(57, 91)],
[(57, 61)],
[(33, 93)],
[(29, 93)],
[(29, 33)],
[(81, 96)],
[(48, 96)],
[(48, 81)],
[(38, 88)],
[(43, 88)],
[(38, 43)],
[(70, 95)],
[(64, 70)],
[(64, 95)]], dtype=[(‘p0’, ‘<i4’), (‘p1’, ‘<i4’)])
# 将数组中重复的去掉,并进行排序后,再进行输出
G = np.unique(G)
G
array([( 0, 2), ( 0, 88), ( 1, 27), ( 1, 35),
( 1, 62), ( 1, 84), ( 2, 88), (24, 35), (27, 62), (29, 33), (29, 93), (33, 93),(35, 35), (35, 84), (38, 43), (38, 88), (43, 88), (48, 56), (48, 61), (48, 81), (48, 96), (56, 61), (57, 61), (57, 91), (61, 91), (64, 70), (64, 95), (70, 95), (81, 96)],
dtype=[(‘p0’, ‘<i4’), (‘p1’, ‘<i4’)])
- 给定一个作为二进制计数的数组C,如何生成一个数组a,使得np.bincount(a)=C
arr = np.bincount([1, 1, 2, 3, 4, 4, 6])
arr
array([0, 2, 1, 1, 2, 0, 1], dtype=int64)
arr_arange = np.arange(len(arr))
arr_arange
array([0, 1, 2, 3, 4, 5, 6])
A = np.repeat(arr_arange, arr)
A
array([1, 1, 2, 3, 4, 4, 6])
- 如何在数组上使用滑动窗口计算平均值?
def moving_average(a, n=3):
ret = np.cumsum(a, dtype=float)
ret[n:] = ret[n:] - ret[:-n]
return ret[n-1:] / n
arr = np.arange(20)
moving_average(arr, n=3)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16., 17., 18.])
- 考虑一维数组Z,建立一个二维数组,其第一行是(z〔0〕,z〔1〕,z〔2〕),且每一后续行移位1(最后一行应该是(Z[-3),Z[-2
],Z[-1 ])。
from numpy.lib import stride_tricks
# 传数组,列数
def rolling(a, window):
# a中数据的个数-window+1,window
shape = (a.size - window + 1, window)
#strides是numpy数组对象的一个属性,
# 官方手册给出的解释是跨越数组各个维度所需要经过的字节数(bytes)
strides = (a.itemsize, a.itemsize)
return stride_tricks.as_strided(a, shape = shape, strides = strides)
arr = rolling(np.arange(10), 3)
arr
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7],
[6, 7, 8],
[7, 8, 9]])
- 如何对布尔值求反,或在原地更改浮点的符号
arr = np.random.randint(0, 2, 5)
arr
array([0, 0, 0, 1, 0])
# logical_not(x)
# 输出x的逻辑非布尔值
np.logical_not(arr)
array([ True, True, True, False, True])
arr = np.random.uniform(-1.0, 1.0, 10)
arr
array([ 0.95804794, 0.21277938, -0.67977694, 0.97118321, -0.98667341,
0.0118689 , -0.89735121, -0.20822032, 0.18428843, -0.19640487])
# 改变数组中对应值的正负
np.negative(arr, out=arr)
array([-0.95804794, -0.21277938, 0.67977694, -0.97118321,
0.98667341, -0.0118689 , 0.89735121, 0.20822032, -0.18428843, 0.19640487])
- 考虑2组点P0,P1描述线(2D)和点P,如何计算从P到每个线I(P0[i],P1[i])的距离?
def distance(arr_1, arr_2, arr):
T = arr_2 - arr_1
L = (T**2).sum(axis=1)
U = -((arr_1[:,0] - arr[...,0])*T[:,0] + (arr_1[:,1] - arr[..., 1])*T[:,1])/L
U = U.reshape(len(U), 1)
D = arr_1 + U*T - arr
return np.sqrt((D**2).sum(axis=1))
# 生成10*2的数组,数值为-10-10之间
arr_1 = np.random.uniform(-10, 10, (10, 2))
arr_2 = np.random.uniform(-10, 10, (10, 2))
arr = np.random.uniform(-10, 10, (10, 2))
distance(arr_1, arr_2, arr)
array([9.39016212, 1.14053264, 7.5961429 , 6.74741895, 4.93722282, 6.56887718, 8.16274959, 6.61383145, 4.53540982, 2.10121491])
- 考虑2组点P0,P1描述线(2D)和一组点P,如何计算从每个点j(P[j])到每一行i(P0[i],P1[i])的距离?
# 生成10*2的数组,数值为-10-10之间
arr_1 = np.random.uniform(-10, 10, (10, 2))
arr_2 = np.random.uniform(-10, 10, (10, 2))
arr = np.random.uniform(-10, 10, (10, 2))
np.array([distance(arr_1, arr_2, arr_i) for arr_i in arr])
array([[ 2.07818074, 4.03885423, 8.20413339, 1.26483874,
1.70893955,
4.00599365, 4.05108341, 0.62630085, 6.19021344, 2.09179793],
[ 0.42956013, 5.04155977, 2.02808439, 2.49587957, 5.17999845,
1.393135 , 9.69949243, 4.19925211, 2.88930507, 3.72150365],
[ 8.72545346, 5.65578084, 0.08247098, 8.22997956, 12.33141937,
5.29396596, 0.40022573, 9.24288648, 4.48271827, 5.3496779 ],
[ 2.45570215, 3.16535479, 10.08695769, 3.02540921, 0.96308946,
5.53854164, 1.75792531, 1.91575287, 6.87005429, 4.42614368],
[ 5.64347623, 2.10159593, 1.35920044, 4.93945208, 9.54483424,
3.42646428, 3.01638929, 7.04192815, 1.63765511, 2.82776035],
[ 7.1773408 , 5.42899807, 2.77953308, 9.15345574, 10.45001371,
2.80341192, 1.39780682, 6.92293671, 2.65906827, 7.23034879],
[ 0.24139109, 2.01190769, 6.38847515, 2.67014974, 4.00646673,
1.95495646, 3.37610245, 1.53512374, 3.88490206, 2.68117066],
[ 9.89843465, 9.90195879, 15.60571749, 1.86861307, 6.33695821,
11.88772963, 4.38860077, 8.66964224, 14.20809414, 2.06488383],
[ 5.54476962, 5.0873008 , 5.488479 , 9.95780382, 8.49765776,
0.2791286 , 3.08916973, 4.55631981, 0.76188624, 9.00653607],
[ 1.0782535 , 0.26042858, 7.07908178, 4.86360587, 4.52007948,
2.23121485, 1.09539896, 1.52000948, 3.31144679, 4.95409344]])
- 考虑任意数组,编写一个函数,该函数提取具有固定形状的子部分,并集中在给定元素(必要时填充值为PAD)。
# 生成10*10的数组,值为0-10
Z = np.random.randint(0, 10, (10, 10))
array([[3, 0, 7, 3, 9, 1, 7, 6, 2, 9],
[7, 3, 9, 0, 5, 5, 2, 6, 7, 5],
[1, 3, 1, 4, 6, 0, 6, 3, 9, 9],
[3, 2, 5, 0, 0, 6, 3, 9, 5, 1],
[1, 2, 3, 5, 4, 2, 8, 9, 6, 9],
[3, 1, 9, 7, 5, 7, 7, 7, 5, 3],
[7, 6, 1, 6, 3, 7, 5, 2, 8, 0],
[4, 1, 6, 2, 4, 4, 1, 0, 1, 5],
[2, 1, 9, 5, 0, 1, 2, 8, 6, 9],
[0, 7, 5, 4, 9, 2, 0, 4, 1, 6]])
shape = (5, 5)
fill = 0
R = np.ones(shape,dtype = Z.dtype) * fill
R
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
position = (1, 1)
P = np.array(list(position)).astype(int)
P
array([1, 1])
Rs = np.array(list(R.shape)).astype(int)
Rs
array([5, 5])
Zs = np.array(list(R.shape)).astype(int)
Zs
array([5, 5])
R_Start = np.zeros((len(shape), )).astype(int)
R_Start
array([0, 0])
R_Stop = np.zeros(len(shape)).astype(int)
R_Stop
array([0, 0])
Z_Start = P - Rs//2
Z_Start
array([-1, -1])
Z_Stop = (P + Rs//2) +Rs%2
Z_Stop
array([4, 4])
R_Start = (R_Start - np.minimum(Z_Start, 0)).tolist()
R_Start
[1, 1]
Z_Start = (np.maximum(Z_Start, 0)).tolist()
Z_Start
[0, 0]
R_Stop = np.maximum(R_Start, (R_Stop - np.maximum(Z_Stop-Zs, 0))).tolist()
R_Stop
[1, 1]
Z_Stop = (np.minimum(Z_Stop, Zs)).tolist()
Z_Stop
[4, 4]
r = [slice(start, stop) for start, stop in zip(R_Start, R_Stop)]
r
[slice(1, 1, None), slice(1, 1, None)]
z = [slice(start, stop) for start, stop in zip(Z_Start, Z_Stop)]
z
[slice(0, 4, None), slice(0, 4, None)]
R
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
Z
array([[3, 0, 7, 3, 9, 1, 7, 6, 2, 9],
[7, 3, 9, 0, 5, 5, 2, 6, 7, 5],
[1, 3, 1, 4, 6, 0, 6, 3, 9, 9],
[3, 2, 5, 0, 0, 6, 3, 9, 5, 1],
[1, 2, 3, 5, 4, 2, 8, 9, 6, 9],
[3, 1, 9, 7, 5, 7, 7, 7, 5, 3],
[7, 6, 1, 6, 3, 7, 5, 2, 8, 0],
[4, 1, 6, 2, 4, 4, 1, 0, 1, 5],
[2, 1, 9, 5, 0, 1, 2, 8, 6, 9],
[0, 7, 5, 4, 9, 2, 0, 4, 1, 6]])
- 考虑数组 Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], 如何生成数组 R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], …, [11,12,13,14]]
Z = np.arange(1, 15, dtype = np.uint32)
Z
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
dtype=uint32)
stride_tricks.as_strided(Z, (11, 4), (4,4))
R = stride_tricks.as_strided(Z, (11, 4), (4,4))
R
array([[ 1, 2, 3, 4],
[ 2, 3, 4, 5],
[ 3, 4, 5, 6],
[ 4, 5, 6, 7],
[ 5, 6, 7, 8],
[ 6, 7, 8, 9],
[ 7, 8, 9, 10],
[ 8, 9, 10, 11],
[ 9, 10, 11, 12],
[10, 11, 12, 13],
[11, 12, 13, 14]], dtype=uint32)
- 计算矩阵秩
arr = np.random.randint(0, 1, (2 ,2))
arr
array([[0, 0],
[0, 0]])
U, S, V = np.linalg.svd(arr)
print(U)
print(S)
print(V)
[[1. 0.] [0. 1.]]
[0. 0.]
[[1. 0.] [0. 1.]]
rank = np.sum(S > 1e-10)
rank
0
如何在数组中查找最频繁的值
arr = np.random.randint(0, 5, 10)
arr
array([3, 0, 0, 3, 4, 2, 0, 0, 3, 3])
np.bincount(arr)
array([4, 0, 1, 4, 1], dtype=int64)
np.bincount(arr).argmax()
0
- 从随机10x10矩阵中提取所有连续3x3块
arr = np.random.randint(0, 5, (10, 10))
arr
array([[3, 0, 3, 4, 1, 0, 3, 3, 0, 4],
[3, 0, 4, 3, 3, 2, 4, 2, 3, 2],
[1, 3, 1, 4, 0, 0, 3, 2, 1, 0],
[4, 4, 4, 3, 3, 2, 2, 2, 2, 2],
[0, 4, 4, 4, 4, 0, 2, 0, 3, 1],
[4, 3, 3, 3, 2, 2, 3, 2, 2, 4],
[4, 3, 3, 1, 0, 1, 3, 1, 3, 4],
[1, 2, 4, 0, 2, 3, 2, 0, 2, 1],
[3, 3, 4, 1, 1, 0, 3, 1, 0, 3],
[3, 1, 0, 1, 0, 4, 3, 3, 2, 1]])
n = 3
i = 1 + (arr.shape[0] - 3)
j = 1 + (arr.shape[1] - 3)
arr_1 = stride_tricks.as_strided(arr, shape=(i, j, n, n), strides = arr.strides + arr.strides)
arr_1
array([[[[3, 0, 3],
[3, 0, 4],
[1, 3, 1]],[[0, 3, 4], [0, 4, 3], [3, 1, 4]], [[3, 4, 1], [4, 3, 3], [1, 4, 0]], [[4, 1, 0], [3, 3, 2], [4, 0, 0]], [[1, 0, 3], [3, 2, 4], [0, 0, 3]], [[0, 3, 3], [2, 4, 2], [0, 3, 2]], [[3, 3, 0], [4, 2, 3], [3, 2, 1]], [[3, 0, 4], [2, 3, 2], [2, 1, 0]]], [[[3, 0, 4], [1, 3, 1], [4, 4, 4]], [[0, 4, 3], [3, 1, 4], [4, 4, 3]], [[4, 3, 3], [1, 4, 0], [4, 3, 3]], [[3, 3, 2], [4, 0, 0], [3, 3, 2]], [[3, 2, 4], [0, 0, 3], [3, 2, 2]], [[2, 4, 2], [0, 3, 2], [2, 2, 2]], [[4, 2, 3], [3, 2, 1], [2, 2, 2]], [[2, 3, 2], [2, 1, 0], [2, 2, 2]]], [[[1, 3, 1], [4, 4, 4], [0, 4, 4]], [[3, 1, 4], [4, 4, 3], [4, 4, 4]], [[1, 4, 0], [4, 3, 3], [4, 4, 4]], [[4, 0, 0], [3, 3, 2], [4, 4, 0]], [[0, 0, 3], [3, 2, 2], [4, 0, 2]], [[0, 3, 2], [2, 2, 2], [0, 2, 0]], [[3, 2, 1], [2, 2, 2], [2, 0, 3]], [[2, 1, 0], [2, 2, 2], [0, 3, 1]]], [[[4, 4, 4], [0, 4, 4], [4, 3, 3]], [[4, 4, 3], [4, 4, 4], [3, 3, 3]], [[4, 3, 3], [4, 4, 4], [3, 3, 2]], [[3, 3, 2], [4, 4, 0], [3, 2, 2]], [[3, 2, 2], [4, 0, 2], [2, 2, 3]], [[2, 2, 2], [0, 2, 0], [2, 3, 2]], [[2, 2, 2], [2, 0, 3], [3, 2, 2]], [[2, 2, 2], [0, 3, 1], [2, 2, 4]]], [[[0, 4, 4], [4, 3, 3], [4, 3, 3]], [[4, 4, 4], [3, 3, 3], [3, 3, 1]], [[4, 4, 4], [3, 3, 2], [3, 1, 0]], [[4, 4, 0], [3, 2, 2], [1, 0, 1]], [[4, 0, 2], [2, 2, 3], [0, 1, 3]], [[0, 2, 0], [2, 3, 2], [1, 3, 1]], [[2, 0, 3], [3, 2, 2], [3, 1, 3]], [[0, 3, 1], [2, 2, 4], [1, 3, 4]]], [[[4, 3, 3], [4, 3, 3], [1, 2, 4]], [[3, 3, 3], [3, 3, 1], [2, 4, 0]], [[3, 3, 2], [3, 1, 0], [4, 0, 2]], [[3, 2, 2], [1, 0, 1], [0, 2, 3]], [[2, 2, 3], [0, 1, 3], [2, 3, 2]], [[2, 3, 2], [1, 3, 1], [3, 2, 0]], [[3, 2, 2], [3, 1, 3], [2, 0, 2]], [[2, 2, 4], [1, 3, 4], [0, 2, 1]]], [[[4, 3, 3], [1, 2, 4], [3, 3, 4]], [[3, 3, 1], [2, 4, 0], [3, 4, 1]], [[3, 1, 0], [4, 0, 2], [4, 1, 1]], [[1, 0, 1], [0, 2, 3], [1, 1, 0]], [[0, 1, 3], [2, 3, 2], [1, 0, 3]], [[1, 3, 1], [3, 2, 0], [0, 3, 1]], [[3, 1, 3], [2, 0, 2], [3, 1, 0]], [[1, 3, 4], [0, 2, 1], [1, 0, 3]]], [[[1, 2, 4], [3, 3, 4], [3, 1, 0]], [[2, 4, 0], [3, 4, 1], [1, 0, 1]], [[4, 0, 2], [4, 1, 1], [0, 1, 0]], [[0, 2, 3], [1, 1, 0], [1, 0, 4]], [[2, 3, 2], [1, 0, 3], [0, 4, 3]], [[3, 2, 0], [0, 3, 1], [4, 3, 3]], [[2, 0, 2], [3, 1, 0], [3, 3, 2]], [[0, 2, 1], [1, 0, 3], [3, 2, 1]]]])
# 创建一个二维数组子类,使Z[i,j]==Z[j,i]
# 创建一个类继承自ndarray
class Symetric(np.ndarray):
# 对__setitem进行重写
def __setitem__(self, index, value):
# i, j 为行,列
i,j = index
# 交换z[i, j] = z[j, i]
super(Symetric, self).__setitem__((i,j), value)
super(Symetric, self).__setitem__((j,i), value)
def symetric(Z):
# np.diag(Z.diagonal())
# 先取主对角线元素,再生成主对角方阵
return np.asarray(Z + Z.T - np.diag(Z.diagonal())).view(Symetric)
# S = symetric(np.random.randint(0,10,(5,5)))
# S[2,3] = 42
# print(S)
a = np.random.randint(0,10,(5,5))
a
array([[0, 7, 4, 4, 9],
[5, 1, 5, 9, 6],
[2, 6, 7, 4, 3],
[6, 5, 1, 5, 1],
[2, 3, 3, 1, 5]])
x = symetric(a)
x[2,3]= 42
x
Symetric([[ 0, 12, 6, 10, 11],
[12, 1, 11, 14, 9],
[ 6, 11, 7, 42, 6],
[10, 14, 42, 5, 2],
[11, 9, 6, 2, 5]])
- 考虑一组具有形状(n,n)的p矩阵和一组具有形状(n,1)的p向量。如何一次计算p矩阵乘积之和?(结果具有形状(n,1))
p, n = 3, 5
# 3维,5*5
arr = np.ones((p,n,n))
arr
array([[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]], [[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]]])
arr_1 = np.ones((p, n, 1))
arr_1
array([[[1.],
[1.],
[1.],
[1.],
[1.]],[[1.], [1.], [1.], [1.], [1.]], [[1.], [1.], [1.], [1.], [1.]]])
# tensordot:进行多维数组的运算
# axes=[[0, 2], [0, 1]]
# [0, 2]去掉arr的轴0,轴2,arr一共三个轴:纬度,行数,列数,还剩轴1,为5行
# [0, 1]去掉arr的轴0,轴1,arr一共三个轴:纬度,行数,列数,还剩轴2,为1列
# 所以最后数组的形式为:[5, 1]
sum = np.tensordot(arr, arr_1, axes=[[0, 2], [0, 1]])
sum
array([[15.],
[15.],
[15.],
[15.],
[15.]])
print(sum.shape)
(5, 1)
- 考虑一个16x16数组,如何得到块和(块大小是4x4)?
k = 4
k = 4
arr = np.ones((16, 16))
arr
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])
# 生成分割行的数组
# 获取arr的行数
arr_x = np.arange(0, arr.shape[0], k)
arr_x
array([ 0, 4, 8, 12])
将arr按照:[0:4][4:8][8:12][12:]来进行分割,注意axis=0,代表行
ps:如分割数组出现[4:0],代表是索引4单个位置
x = np.add.reduceat(arr, arr_x, axis=0)
x
array([[4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.],
[4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4., 4.]])
列的
- 生成分割列的数组
# 获取x的列数
arr_y = np.arange(0, x.shape[1], k)
arr_y
array([ 0, 4, 8, 12])
y = np.add.reduceat(x, arr_y, axis=1)
y
array([[16., 16., 16., 16.],
[16., 16., 16., 16.],
[16., 16., 16., 16.],
[16., 16., 16., 16.]])
np.add.reduceat(x, np.arange(0, x.shape[1], k), axis=1)
y = np.add.reduceat(x, np.arange(0, x.shape[1], k), axis=1)
y
array([[16., 16., 16., 16.],
[16., 16., 16., 16.],
[16., 16., 16., 16.],
[16., 16., 16., 16.]])
- 如何使用numpy阵列实现生命游戏?(看不明白)
def iterate(Z):
# Count neighbours
N = (Z[0:-2,0:-2] + Z[0:-2,1:-1] + Z[0:-2,2:] +
Z[1:-1,0:-2] + Z[1:-1,2:] +
Z[2: ,0:-2] + Z[2: ,1:-1] + Z[2: ,2:])
# Apply rules
birth = (N==3) & (Z[1:-1,1:-1]==0)
survive = ((N==2) | (N==3)) & (Z[1:-1,1:-1]==1)
Z[...] = 0
Z[1:-1,1:-1][birth | survive] = 1
return Z
Z = np.random.randint(0,2,(50,50))
for i in range(100): Z = iterate(Z)
print(Z)
- 如何获取数组的n个最大值
arr = np.random.randint(1, 100, 10)
arr
array([52, 48, 95, 37, 69, 45, 35, 26, 20, 52])
# 此函数仅沿多维数组的第一个索引打乱数组
np.random.shuffle(arr)
n=5
#np.argsort(x)[num]
#将x中的元素按从小到大排序
#num>=0时,正向输出num大小的数组
#num<0时,反向输出
print(arr[np.argsort(arr)[-n:]])
[48 52 52 69 95]
print(arr[np.argpartition(-arr, n)[:n]])
[52 95 69 48 52]
- 给定任意数量的向量,构建笛卡尔积(每个项的每个组合)
enumerate
def cartesian(arrays):
arrays = [np.asarray(a) for a in arrays]
shape = (len(x) for x in arrays)
print(shape)
ix = np.indices(shape, dtype=int)
ix = ix.reshape(len(arrays), -1).T
for n, arr in enumerate(arrays):
ix[:, n] = arrays[n][ix[:, n]]
return ix
print(cartesian(([1, 2, 3], [4, 5], [6])))
<generator object cartesian.. at 0x000002027D13B4F8>
[[1 4 6]
[1 5 6]
[2 4 6]
[2 5 6]
[3 4 6]
[3 5 6]]
- 如何从常规数组创建记录数组?
arr = np.array([("Hello", 2.5, 3),
("World", 3.6, 2)])
arr
array([[‘Hello’, ‘2.5’, ‘3’],
[‘World’, ‘3.6’, ‘2’]], dtype=’<U5’)
arr_1 = np.core.records.fromarrays(arr.T,
names = 'col1, col2, col3',
formats = 'S8, f8, i8')
arr_1
rec.array([(b’Hello’, 2.5, 3), (b’World’, 3.6, 2)],
dtype=[(‘col1’, ‘S8’), (‘col2’, ‘<f8’), (‘col3’, ‘<i8’)])
- 考虑一个大的向量z,用2种不同的方法计算Z到3的幂
x = np.random.randint(1, 10)
x
8
np.power(x,3)
512
x*x*x
512
- 考虑两个数组A和B的形状(8,3)和(2,2)。如何查找包含B每行元素的A行,而不考虑B中元素的顺序?
A = np.random.randint(1, 5, (8, 3))
A
array([[3, 4, 1],
[4, 3, 4],
[2, 3, 4],
[2, 2, 4],
[2, 1, 1],
[4, 1, 1],
[4, 2, 2],
[1, 2, 2]])
B = np.random.randint(1, 5, (2, 2))
B
array([[3, 1],
[4, 1]])
C = (A[..., np.newaxis, np.newaxis] == B)
C
#这块不理解
array([[[[ True, False],
[False, False]],[[False, False], [ True, False]], [[False, True], [False, True]]], [[[False, False], [ True, False]], [[ True, False], [False, False]], [[False, False], [ True, False]]], [[[False, False], [False, False]], [[ True, False], [False, False]], [[False, False], [ True, False]]], [[[False, False], [False, False]], [[False, False], [False, False]], [[False, False], [ True, False]]], [[[False, False], [False, False]], [[False, True], [False, True]], [[False, True], [False, True]]], [[[False, False], [ True, False]], [[False, True], [False, True]], [[False, True], [False, True]]], [[[False, False], [ True, False]], [[False, False], [False, False]], [[False, False], [False, False]]], [[[False, True], [False, True]], [[False, False], [False, False]], [[False, False], [False, False]]]])
#np.where(arr)
#返回坐标,
rows = np.where(C.any(3, 1).all(1))[0]
rows
- 考虑10x3矩阵,提取具有不等值的行(例如[2,2,3])
Z = np.random.randint(0,5,(10,3))
Z
array([[4, 2, 0],
[3, 1, 0],
[4, 3, 0],
[3, 4, 3],
[3, 4, 4],
[1, 0, 2],
[0, 2, 2],
[1, 2, 3],
[1, 3, 3],
[4, 0, 1]])
E = np.all(Z[:,1:] == Z[:,:-1], axis=1)
U = Z[~E]
U = Z[Z.max(axis=1) != Z.min(axis=1),:]
U
array([[4, 2, 0],
[3, 1, 0],
[4, 3, 0],
[3, 4, 3],
[3, 4, 4],
[1, 0, 2],
[0, 2, 2],
[1, 2, 3],
[1, 3, 3],
[4, 0, 1]])
U = Z[~E]
U
array([[4, 2, 0],
[3, 1, 0],
[4, 3, 0],
[3, 4, 3],
[3, 4, 4],
[1, 0, 2],
[0, 2, 2],
[1, 2, 3],
[1, 3, 3],
[4, 0, 1]])
U = Z[Z.max(axis=1) != Z.min(axis=1),:]
U
array([[4, 2, 0],
[3, 1, 0],
[4, 3, 0],
[3, 4, 3],
[3, 4, 4],
[1, 0, 2],
[0, 2, 2],
[1, 2, 3],
[1, 3, 3],
[4, 0, 1]])
- 将整数向量转换为矩阵二进制表示
I = np.array([0, 1, 2, 3, 15, 16, 32, 64, 128])
I
array([ 0, 1, 2, 3, 15, 16, 32, 64, 128])
I.reshape(-1, 1)
array([ 0, 1, 2, 3, 15, 16, 32, 64, 128])
B = ((I.reshape(-1,1) & (2**np.arange(8))) != 0).astype(int)
B
array([[0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]])
# 列变换
print(B[:,::-1])
[[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 1 1]
[0 0 0 0 1 1 1 1]
[0 0 0 1 0 0 0 0]
[0 0 1 0 0 0 0 0]
[0 1 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0]]
np.array([0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=np.uint8)
array([ 0, 1, 2, 3, 15, 16, 32, 64, 128], dtype=uint8)
print(np.unpackbits(I[:, np.newaxis], axis=1))
[[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1]
[0 0 0 0 0 0 1 0]
[0 0 0 0 0 0 1 1]
[0 0 0 0 1 1 1 1]
[0 0 0 1 0 0 0 0]
[0 0 1 0 0 0 0 0]
[0 1 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0]]
- 给定二维数组,如何提取唯一行?
Z = np.random.randint(0,2,(6,3))
Z
array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0],
[0, 0, 0],
[1, 0, 0],
[0, 1, 0]])
T = np.ascontiguousarray(Z).view(np.dtype((np.void, Z.dtype.itemsize * Z.shape[1])))
T
array([[b’\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00’],
[b’\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00’],
[b’\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00’],
[b’\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00’],
[b’\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00’],
[b’\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00’]],
dtype=’|V12’)
uZ = np.unique(Z, axis=0)
uZ
array([[0, 0, 0],
[0, 1, 0],
[1, 0, 0]])
- 考虑2个向量A和B,写出内部、外部、总和和多重函数的einsum等价物
A = np.random.uniform(0,1,10)
A
array([0.99118253, 0.34471879, 0.23766156, 0.0319939 , 0.48299773,
0.79210602, 0.19735459, 0.78088031, 0.55373643, 0.90943304])
B = np.random.uniform(0,1,10)
B
array([0.92237584, 0.01358852, 0.25228292, 0.67539596, 0.46242253,
0.52668123, 0.58934982, 0.99384323, 0.64528841, 0.64938654])
#np.sum(A)
np.einsum('i->', A)
5.32206490470038
# A * B
np.einsum('i,i->i', A, B)
array([0.91424282, 0.00468422, 0.05995795, 0.02160855, 0.22334903,
0.41718737, 0.11631089, 0.77607261, 0.3573197 , 0.59057357])
# np.inner(A, B)
np.einsum('i,i', A, B)
3.4813067109129374
# np.outer(A, B) np.einsum('i,j->ij', A, B)
- 考虑由两个向量(X,Y)描述的路径,如何使用等距采样对其进行采样
np.pi
3.141592653589793
phi = np.arange(0, 10*np.pi, 0.1) phi
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,
…
29.7, 29.8, 29.9, 30. , 30.1, 30.2, 30.3, 30.4, 30.5, 30.6, 30.7,
30.8, 30.9, 31. , 31.1, 31.2, 31.3, 31.4]) x
a = 1
x = a*phi*np.cos(phi)
y = a*phi*np.sin(phi)
# 段长度
dr = (np.diff(x)**2 + np.diff(y)**2)**.5
r = np.zeros_like(x)
# 积分路径
r[1:] = np.cumsum(dr)
# 规则间隔路径
r_int = np.linspace(0, r.max(), 200)
#积分路径
x_int = np.interp(r_int, r, x)
y_int = np.interp(r_int, r, y)
print(x_int)
print(y_int)
- 给定一个整数n和一个二维数组X,从X中选择可以解释为从具有n度的多项式分布中提取的行,即仅包含整数且总和为n的行
X = np.asarray([[1.0, 0.0, 3.0, 8.0],
[2.0, 0.0, 1.0, 1.0],
[1.5, 2.5, 1.0, 0.0]])
X
array([[1. , 0. , 3. , 8. ],
[2. , 0. , 1. , 1. ],
[1.5, 2.5, 1. , 0. ]])
n = 4
M = np.logical_and.reduce(np.mod(X, 1) == 0, axis=-1)
M
array([ True, True, False])
M &= (X.sum(axis=-1) == n)
print(X[M])
[[2. 0. 1. 1.]]
- 计算1D数组X的平均值的自举95%置信区间(即,用替换N次对数组元素重新采样,计算每个样本的平均值,然后计算平均值的百分位数)。
arr = np.random.randn(100)
arr
#省略一维数组
array([ 1~100 ])
N = 1000
idx = np.random.randint(0, arr.size, (N, arr.size))
means = arr[idx].mean(axis=1)
confint = np.percentile(means, [2.5, 97.5])
print(confint)
[-0.11933296 0.28769495]