玩转 Numpy 的精选习题 (一)
1,打印 numpy 版本
import numpy as np
print(np.__version__)
>1.16.5
2、创建 10 个元素空向量
Z = np.zeros(10)
print(Z)
>[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
3, 返回数组的元素的内存大小
Z = np.zeros((10,10))
print(Z.itemsize)
print("%d bytes"%(Z.size*Z.itemsize))
> 8
> 800 bytes
4,查看 numpy.add 的用法
np.info(np.add)
>add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])
Add arguments element-wise.
5,创建一个大小为10的空向量,其中第五个元素赋值为1
Z = np.zeros(10)
Z[4] = 1
print(Z)
>[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
6, 创建向量,元素为10-49
Z = np.arange(10,50)
print(Z)
>[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]
7,逆置向量
Z = np.arange(50)
print(Z)
Z = Z[::-1]
print(Z)
>[ 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49]
>[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2
1 0]
8,创建3*3矩阵,值分别为 0-8
Z = np.arange(9).reshape(3,3)
print(Z)
>[[0 1 2]
[3 4 5]
[6 7 8]]
9,找到非 0 元素索引
nz = np.nonzero([1,2,0,0,4,0])
print(nz)
> (array([0, 1, 4], dtype=int64),)
10,创建 3*3 的向量矩阵;
Z = np.eye(3)
print(Z)
> [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
11,创建 333 数组,元素值随机;
Z = np.random.random((3,3,3))
print(Z)
>[[[0.33020335 0.85888276 0.72230212]
[0.01956836 0.53253257 0.73982722]
[0.03397414 0.44528818 0.42641914]]
[[0.19122995 0.56924379 0.84207502]
[0.76823495 0.5557601 0.60467123]
[0.33447872 0.70850795 0.35257091]]
[[0.84615772 0.93949644 0.20724402]
[0.9927998 0.88442679 0.39655603]
[0.88817575 0.34702258 0.22757922]]]
12,创建一个10*10数组,找最大值,最小值,平均值
Z = np.random.random((10,10))
print(Z)
Zmin,Zmax,Zmean = Z.min(),Z.max(),Z.mean()
print(Zmin,Zmax,Zmean)
> [[0.12973065 0.31677156 0.54994539 0.91349228 0.36075614 0.08692027
0.45248993 0.26464321 0.30248679 0.26602034]
[0.02364034 0.53374142 0.30359262 0.62535471 0.73467954 0.9846915
0.74688887 0.48055324 0.38289746 0.24502886]
[0.5322309 0.38022224 0.24114753 0.50580718 0.45854571 0.55290577
0.74052612 0.09313212 0.93412896 0.49288767]
[0.93782643 0.76282996 0.7010213 0.28775456 0.78197272 0.70660015
0.84372202 0.78868034 0.78336608 0.51177132]
[0.77439896 0.07053705 0.69320814 0.52254626 0.7504721 0.74303412
0.91345364 0.02576358 0.60881113 0.59070141]
[0.66012363 0.6985128 0.66980978 0.56780945 0.12893516 0.82853888
0.71200874 0.25859772 0.17366786 0.54844797]
[0.0383674 0.63298341 0.55101785 0.90127532 0.72015945 0.45123806
0.32016112 0.47413686 0.26122929 0.83610203]
[0.95844267 0.70325507 0.41800104 0.33859346 0.01799442 0.25119684
0.02960764 0.95912232 0.34042016 0.58667242]
[0.57093639 0.22364083 0.04937112 0.22619717 0.19154811 0.86348401
0.45599839 0.31423009 0.54150058 0.12802574]
[0.06339162 0.60665877 0.72231736 0.52420897 0.49326727 0.37083385
0.53290497 0.37937229 0.23433393 0.60931142]]
0.01799442455882616 0.9846915015819662 0.4957229230688264
13,创建一个2d数组,1在外面,0在里面
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
> [[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
14,在现有的数组周围填补 0 padding
Z = np.ones((5,5))
Z = np.pad(Z,pad_width = 1,mode = "constant",constant_values = 0)
print(Z)
# 内置补一圈0
Z = np.ones((5,5))
Z[:,[0,-1]] = 0
Z[[0,-1],:] = 0
print(Z)
> [[0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0. 0.]]
> [[0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 1. 1. 1. 0.]
[0. 0. 0. 0. 0.]]
15,np的一些特殊表达式用法
print(0*np.nan) #nan
print(np.nan == np.nan)# False
print(np.inf > np.nan) # False
print(np.nan - np.nan) #nan
print(np.nan in set([np.nan])) # True
print(0.3==3*0.1) # False
16,创建一个 5*5 矩阵值为1,2,3,4在对角线的下方
Z = np.diag(1+np.arange(4),k = -1)
print(Z)
> [[0 0 0 0 0]
[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]]
17,创建一个 8*8 矩阵,并用棋盘模式填充
Z = np.zeros((8,8),dtype =int)
Z[1::2,::2] = 1 # 从第一行每次增加2行;
Z[::2,1::2] = 1 # 从第一列,每次增加两列;
print(Z)
> [[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
18,返回 (6,7,8)数组 第100元素的索引:
print(np.unravel_index(99,(6,7,8)))
> (1, 5, 3)
19, 利用 tile 功能创建 8*8 矩阵;
Z = np.tile(np.array([[0,1],[1,0]]),(4,4))
print(Z)
> [[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
20,对5*5 随机元素矩阵
Z = np.random.random((5,5))
Z = (Z-np.mean(Z))/(np.std(Z))
print(np.std(Z))
print(Z)
> 0.9999999999999999
[[ 1.59898483 0.0897438 -1.04817826 -0.15821328 0.84998669]
[-0.1827481 -0.30145101 1.24307433 0.57339069 -0.40676173]
[-1.03534466 -0.02810903 2.15479285 0.48527072 0.42268743]
[-0.34578218 1.62264222 -1.21917396 -0.01898178 -1.27560744]
[-1.23050258 -1.08268862 1.1647007 -1.49899855 -0.37273306]]
玩转 Numpy 的精选习题 (二)
以下部分是 21- 40题,对于陌生的用法大家记得跟着敲一下,加深以下理解!
21,创建一个自定以类型来表述一个 RGBA 颜色代码(unsigned)
color = np.dtype([('r',np.ubyte,1),
("g",np.ubyte,1),
('b',np.ubyte,1),
('a',np.ubyte,1)])
print(color)
> [('r', 'u1'), ('g', 'u1'), ('b', 'u1'), ('a', 'u1')]
22,53 矩阵乘以 32 矩阵;
Z = np.dot(np.ones((5,3)),np.ones((3,2)))
print(Z)
> [[3. 3.]
[3. 3.]
[3. 3.]
[3. 3.]
[3. 3.]]
23, 给定一维数组,在3-8元素全部取反
Z = np.arange(11)
Z[(3<Z)&(Z<8)] *=-1
print(Z)
> [ 0 1 2 3 -4 -5 -6 -7 8 9 10]
24 ,下面脚本输出结果为?
print(sum(range(5),-1)) # 9
from numpy import *
print(sum(range(5),-1)) # 10
25 ,下面表达式结果为:
print(np.array(0)/np.array(0))
print(np.array(0)//np.array(0))
print(np.array([np.nan]).astype(int).astype(float))
>nan
0
[-2.14748365e+09]
16,创建一个 5*5 矩阵值为1,2,3,4在对角线的下方
Z = np.diag(1+np.arange(4),k = -1)
print(Z)
> [[0 0 0 0 0]
[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]]
26, 将一个 浮点数组从0舍入
Z = np.random.uniform(-10,+10,10)
print(Z)
print(np.copysign(np.ceil(np.abs(Z)),Z))
> [-8.9433086 -1.94558017 5.09831429 0.75843992 -2.57004218 -3.68920573
-2.23238395 -6.75331705 3.62935401 -2.37852371]
[-9. -2. 6. 1. -3. -4. -3. -7. 4. -3.]
27,找到两个数组的共有元素;
Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print(np.intersect1d(Z1,Z2))
> [0 1 4 6 7 8]
28,怎样返回昨天、今天、明天;
yes = np.datetime64("today") - np.timedelta64(1)
tod = np.datetime64("today")
tom = np.datetime64("today") + np.timedelta64(1)
print(yes)
print(tod)
print(tom)
> 2020-06-30
2020-07-01
2020-07-02
29,返回,19年2月份的所有日期
Z = np.arange('2019-02','2019-03',dtype = 'datetime64[D]')
print(Z)
> ['2019-02-01' '2019-02-02' '2019-02-03' '2019-02-04' '2019-02-05'
'2019-02-06' '2019-02-07' '2019-02-08' '2019-02-09' '2019-02-10'
'2019-02-11' '2019-02-12' '2019-02-13' '2019-02-14' '2019-02-15'
'2019-02-16' '2019-02-17' '2019-02-18' '2019-02-19' '2019-02-20'
'2019-02-21' '2019-02-22' '2019-02-23' '2019-02-24' '2019-02-25'
'2019-02-26' '2019-02-27' '2019-02-28']
30 ,怎样计算 ((A+B)*(-A/2))
A = np.ones(3)*1
B = np.ones(3)*2
C = np.ones(3) *3
np.add(A,B,out=A)
np.divide(A,2,out = -A)
np.negative(A,out =A)
np.multiply(A,B,out = A)
> array([-6., -6., -6.])
31,取出随机数组的正数部分的整数部分
Z = np.random.uniform(0,10,10)
print(np.floor(Z))
> [5. 8. 7. 7. 4. 4. 7. 7. 7. 1.]
32, 创见5*5 矩阵,行值为0-4;
Z = np.zeros((5,5))
Z += np.arange(5)
print(Z)
> [[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
33,构建一个生成函数生成10个整数,并转换为数组
def generate():
for x in range(10):
yield x
Z = np.fromiter(generate(),dtype =float,count = -1)
print(Z)Z = np.zeros((5,5))
Z += np.arange(5)
print(Z)
> [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
34 ,创建一个10个元素大小的数组,值为0-1
Z = np.linspace(0,1,11,endpoint = False)[1:]
Z
> array([0.09090909, 0.18181818, 0.27272727, 0.36363636, 0.45454545,
0.54545455, 0.63636364, 0.72727273, 0.81818182, 0.90909091])
35,判断两个随机数组,是否相等;
A = np.random.randint(0,2,5)
B = np.random.randint(0,2,5)
equal = np.array_equal(A,B)
print(equal)
> False
37,10*2 的随机矩阵,由笛卡尔坐标转化为极坐标
Z = np.random.random((10,2))
X,Y = Z[:,1],Z[:,1]
R = np.sqrt(X**2+Y**2)
T = np.arctan2(Y,X)
print(R)
print(T)
> [0.12396673 1.09640358 0.60171085 0.93054782 0.05433122 0.88863367
0.10389402 0.45757275 0.53011318 0.17896545]
[0.78539816 0.78539816 0.78539816 0.78539816 0.78539816 0.78539816
0.78539816 0.78539816 0.78539816 0.78539816]
38, 创建一个10大小的随机向量,把最大值取为0
Z = np.random.random(10)
Z[Z.argmax()] = 0
Z
> array([0.49363055, 0.03622898, 0.19958785, 0.1625108 , 0. ,
0.88062849, 0.27145883, 0.42484903, 0.62000329, 0.41577207])
39,给两个数据组,构造为 柯西矩阵(CIJ = 1/(xi-yj))
X = np.arange(8)
Y = X+0.5
C = 1.0/np.subtract.outer(X,Y)
print(np.linalg.det(C))
>3638.1636371179666
40,在一个一维数组中找到与给定值最近的值
Z = np.arange(100)
v = np.random.uniform(0,100)
print(v)
index = (np.abs(Z-v)).argmin()
print(Z[index])
> 14.763688219125658
15
玩转 Numpy 的精选习题 (三)
推送的 20 道题目是比较难的,里面会用到一些不常用的函数,例如 np.bincount()、np.atleast_2d()、np.genfromtxt(),但仔细琢磨的话问题都不大
这 20 道解题步骤中虽然大部分都是一些常见函数,但用的是都是一些进阶语法,有时解一道题需要多个函数混合在一起使用,需要仔细思考它的用法
41,打印一个数组中所有值
np.set_printoptions(threshold =float('inf'))
Z = np.zeros((16,16))
print(Z)
//
42,给定一个标量,在一个向量中找到值最近的元素值
# 42,在向量元素中找离指定元素最近的值:
Z = np.arange(100)
v = np.random.uniform(0,100)
index = (np.abs(Z-v)).argmin()
print(Z[index])
#
15
43,建立一个结构数组,来表示一个坐标和 RGB 编码
Z =np.zeros(10,
[('position',[('x',float,1),
('y',float,1)]),
('color',[('r',float,1),
('g',float,1),
('b',float,1)])])
print(Z)
#
[((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.))]
44,指定一个100*2的向量矩阵,代表点的坐标,计算矩阵中点到点之间的距离
Z = np.random.random((10,2))
X,Y = np.atleast_2d(Z[:,0],Z[:,1])
D = np.sqrt((X-X.T)**2+(Y-Y.T)**2)
D
#
array([[0. , 0.98633439, 0.72167275, 0.90188932, 0.13230666,
0.74692975, 0.06223381, 0.60174661, 0.95391954, 0.92008742],
[0.98633439, 0. , 0.77985879, 0.1631969 , 0.90583532,
0.30879931, 0.92419607, 0.39486349, 1.00587334, 1.0537349 ],
[0.72167275, 0.77985879, 0. , 0.61879559, 0.7580086 ,
0.48464973, 0.68199994, 0.65207576, 0.28058449, 0.2957362 ],
[0.90188932, 0.1631969 , 0.61879559, 0. , 0.83909907,
0.1681398 , 0.83995416, 0.35751067, 0.84286462, 0.89058005],
[0.13230666, 0.90583532, 0.7580086 , 0.83909907, 0. ,
0.69570875, 0.10653016, 0.51251781, 1.01061853, 0.98608667],
[0.74692975, 0.30879931, 0.48464973, 0.1681398 , 0.69570875,
0. , 0.68572148, 0.27153623, 0.73491355, 0.77182869],
[0.06223381, 0.92419607, 0.68199994, 0.83995416, 0.10653016,
0.68572148, 0. , 0.54051085, 0.9237499 , 0.89448777],
[0.60174661, 0.39486349, 0.65207576, 0.35751067, 0.51251781,
0.27153623, 0.54051085, 0. , 0.9285785 , 0.94720901],
[0.95391954, 1.00587334, 0.28058449, 0.84286462, 1.01061853,
0.73491355, 0.9237499 , 0.9285785 , 0. , 0.08212287],
[0.92008742, 1.0537349 , 0.2957362 , 0.89058005, 0.98608667,
0.77182869, 0.89448777, 0.94720901, 0.08212287, 0. ]])
45,将 float32 转化为 int 32
Z = (np.random.rand(10)*100).astype(np.float32)
Y = Z.view(np.int32)
Y[:] = Z
Y
#
[ 3 71 47 23 57 54 67 1 76 6]
46,读取下面文本并转化为二维 array
from io import StringIO
s = StringIO('''1, 2, 3, 4, 5
6, , , 7, 8
, , 9,10,11''')
Z =np.genfromtxt(s,delimiter = ",",dtype = np.int)
print(Z)
#
[[ 1 2 3 4 5]
[ 6 -1 -1 7 8]
[-1 -1 9 10 11]]
47,Numpy 中数组枚举的表示方法
Z = np.arange(9).reshape(3,3)
for index,value in np.ndenumerate(Z):
print(index,value)
for index in np.ndindex(Z.shape):
print(index,Z[index])
48,建立一个二维高斯数组
X,Y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
D = np.sqrt(X*X+Y*Y)
sigma,mu = 1.0,0.0
G = np.exp(-((D-mu)**2/(2.0*sigma**2)))
print(G)
#
[[0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]
[0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]]
49,在二维数组中随机放置 p 个元素
n = 10
p = 3
Z = np.zeros((n,n))
np.put(Z,np.random.choice(range(n*n),p,replace = False),1)
print(Z)
#
[[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. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
50,矩阵减去每一行的平均值
X = np.random.rand(5,10)
Y = X - X.mean(axis =1,keepdims = True)
#Y = X -X.mean(axis=1).reshape(-1,1)
print(Y)
#
[[-0.2854325 0.20882031 0.35568196 0.18316993 -0.40776416 -0.30312722
0.07405133 0.35347259 -0.09991147 -0.07896078]
[ 0.04400539 -0.12949299 0.43078155 0.10710735 0.35645354 -0.43201896
0.1102518 -0.11709654 0.01395583 -0.38394697]
[-0.07116609 -0.09382485 0.02956884 -0.29241189 -0.05337575 -0.24354029
0.28228751 0.28369777 -0.22918485 0.3879496 ]
[ 0.34436033 0.29003034 -0.21067108 -0.09945774 0.0428871 -0.17125693
-0.28001421 0.40165161 -0.03741505 -0.28011437]
[ 0.32963488 -0.03061654 -0.36657671 -0.05771164 -0.35016734 0.27941279
0.19521001 0.44803322 -0.31404906 -0.13316961]]
51,以某一列对整个数组的行进行排序
Z = np.random.randint(0,10,(3,3))
print(Z[Z[:,1].argsort()])
#
[[3 4 8]
[1 5 2]
[2 8 2]]
52,判断一个2D数组,是否含有空列
Z = np.random.randint(0,3,(3,10))
print((~Z.any(axis = 0)).any())
#
True
53,在数组中找到给定值的最近值
Z = np.random.uniform(0,1,10)
z = 0.5
m = Z.flat[np.abs(Z-z).argmin()]
print(m)
#
0.507763391604458
54,给定两个二维数组维度分别为 13,31;利用迭代器计算他们的和
A = np.arange(3).reshape(3,1)
B = np.arange(3).reshape(1,3)
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]]
55,基于索引列表 I ,对应权重 X,进行加权计算得到F
需要注意一下,这里用到 np.bincount() 函数,只有一个参数时,返回一个数组表示每个元素出现的次数;如果设置两个参数则会涉及到加权运算,理解方面比较困难,建议查阅一下官方文档
X = [1,2,3,4,5,6]
I = [1,3,9,3,4,1]
F = np.bincount(I,X)
print(F)
#
[0. 7. 0. 6. 5. 0. 0. 0. 0. 3.]
56,给定一个 图片像素数组(w,h,3),计算其中唯一颜色数量
w,h = 16,16
I = np.random.randint(0,2,(h,w,3)).astype(np.ubyte)
F = I[...,0]*256*256 + I[...,1]*256 + I[...,2] # color RGB 进行编码
n = len(np.unique(F))
print(n)
#
4
57,给定一个四维数组,同时计算最后轴元素之和
A = np.random.randint(0,10,(3,4,3,4))
sum = A.sum(axis = (-2,-1))# 计算最后两列;
print(sum)
# second solutions
sum = A.reshape(A.shape[:-2]+(-1,)).sum(axis = -1)
#
[[62 59 48 61]
[53 59 45 48]
[50 56 71 60]]
58,给定一维数组D,通过相同维度向量 S ,通过自己索引来计算子集D 的平均值
D = np.random.uniform(0,1,100)
S = np.random.randint(0,10,100)
D_sums = np.bincount(S,weights =D)
D_counts = np.bincount(S)
D_means = D_sums/D_counts
print(D_means)
#
[0.50247673 0.43057174 0.54440853 0.60862306 0.61939138 0.6184843
0.57271125 0.49704534 0.52671729 0.4849897 ]
59,计算一个点积的对角线
A = np.random.uniform(0,1,(5,5))
B = np.random.uniform(0,1,(5,5))
np.diag(np.dot(A,B))
# Second solutions
np.sum(A*B.T,axis = 1)
#
array([0.49721784, 1.06865483, 0.65669748, 0.78147516, 1.11704931])
60,给定一维向量例如[1,2,3,4,5],在相邻两个值之间加入3个0
Z = np.array([1,2,3,4,5])
nz = 3
Z0 = np.zeros(len(Z)+(len(Z)-1)*(nz))
Z0[::nz+1] = Z
print(Z0)
#
[1. 0. 0. 0. 2. 0. 0. 0. 3. 0. 0. 0. 4. 0. 0. 0. 5.]
参考链接 :
玩转 Numpy 的精选习题 (一) :https://mp.weixin.qq.com/s/oRyOL4JAC80F4_-V_3laQg
玩转 Numpy 的精选习题 (二) :
玩转 Numpy 的精选习题 (三) :https://mp.weixin.qq.com/s/AIjWBZDjOeorv18Xl4Iz5g