1.

Q:将 numpy 导入为 np 并查看版本号。

参考答案:

import numpy as np
print(np.__version__)

输出:1.20.3

2.

Q:创建一个从0到9的一维数组。

三种方法都可以。arange用法和range用法相同。

参考答案:

res = np.array([0,1,2,3,4,5,6,7,8,9])
res = np.array(range(10))
res = np.arange(10)

输出都是:[0 1 2 3 4 5 6 7 8 9]

3.

Q:创建一个3×3的所有真值的数组。

参考答案:

res = np.full((3,3),True,dtype=bool)

输出:
[[ True  True  True]
 [ True  True  True]
 [ True  True  True]]

解释一下 full函数.

full(形状,填充数值,数值类型)
numpy.full(shape, fill_value, dtype=None, order='C')
order: : {‘C’, ‘F’}, 可选参数
是否以C或Fortran-contiguous(行或列)顺序存储多维数据。

4.

Q:从 arr 中提取所有奇数.

参考答案:

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
res = arr[arr%2==1]
输出:[1 3 5 7 9]

5.

Q:用-1替换 arr 中的所有奇数

参考答案:

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr[arr%2==1] = -1
输出:[ 0 -1  2 -1  4 -1  6 -1  8 -1]

6.

Q:在不改变 arr 的情况下,用1代替 arr 中的所有奇数.

参考答案:

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
res = np.where(arr % 2 == 1,-1,arr)
输出res:[ 0 -1  2 -1  4 -1  6 -1  8 -1]
输出arr:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

where函数的两种用法:
1.np.where(condition, x, y)
满足条件(condition),输出x,不满足输出y。
2.np.where(condition)
只有条件 (condition),没有x和y,则输出满足条件 (即非0) 元素的坐标。

7.

Q:将一维数组转换成2行的2D数组.

参考答案:

arr = np.arange(10)
res = np.reshape(arr,(2,5))
输出:
arr:[0 1 2 3 4 5 6 7 8 9]
res:[[0 1 2 3 4]
 [5 6 7 8 9]]

8.

Q:垂直排列数组A和B.

参考答案:

a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)

res = np.vstack([a,b])
输出:

a:[[0 1 2 3 4]
 [5 6 7 8 9]]
 
b:[[1 1 1 1 1]
 [1 1 1 1 1]]
 
res:
[[0 1 2 3 4]
 [5 6 7 8 9]
 [1 1 1 1 1]
 [1 1 1 1 1]]

9.

Q:水平排列两数组A和B.

参考答案:

a = np.arange(10).reshape(2,-1)
b = np.repeat(1, 10).reshape(2,-1)
res = np.hstack([a,b])
输出:
[[0 1 2 3 4 1 1 1 1 1]
 [5 6 7 8 9 1 1 1 1 1]]

10.

Q:获取A和B之间的公共项

参考答案:

a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
res = np.intersect1d(a,b)
print(res)
输出:
[2 4]

11.

Q:在没有硬编码的情况下创建下面的数组。array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
只使用numpy函数和下面的输入数组a。

a = np.array([1,2,3])
np.r_[np.repeat(a, 3), np.tile(a, 3)]
输出:
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])

r_ : 按行拼接

12.

Q:从数组A移除数组B中存在的所有项.

参考答案:

a = np.array([1,2,3,4,5])
b = np.array([5,6,7,8,9])
res = np.setdiff1d(a,b)
print(res)
输出:
[1 2 3 4]

13.

Q:获取A和B元素匹配的位置

a = np.array([1,2,3,2,3,4,3,4,5,6])
b = np.array([7,2,10,2,7,4,9,4,9,8])
res = np.where(a == b)
print(res)
输出:
(array([1, 3, 5, 7]),)

14.

Q:从A中获取5到10之间的所有项。


a = np.array([2, 6, 1, 9, 10, 3, 27])

法一:
res = np.where((a >=5) & (a <= 10))
print(res)
输出:(array([1, 3, 4]),)

法二:

res = a[(a >=5) & (a <= 10)]
print(res)
输出:[ 6  9 10]

这里要注意,where返回的是符合条件的值的索引
而直接使用 arr[条件],返回符合条件的值。

15.

Q:转换适用于两个标量的函数maxx,以处理两个数组。

待解决。。。。。

16.

Q:在数组arr中交换列1和2。

参考答案:


arr = np.arange(9).reshape(3,3)
res = arr[:,[1,0,2]]
print(res)

切片和python的切片语法大致一样。arr[:,[1,0,2]] 逗号前面的分号前后都没有值,表示取所有行。逗号后面为[1,0,2] 表示取第1,0,2行(书写与返回顺序一致)

17.

Q:在数组arr中交换两行1和2

参考答案:

arr = np.arange(9).reshape(3,3)
res = arr[[1,0,2],:]
print(res)

18.

Q:反转二维数组arr的行

参考答案:

arr = np.arange(9).reshape(3,3)
res = arr[::-1,:]
print(res)

看到网上很多答案是 res = arr[::-1] 这样不够精确也不便于理解。
arr[::-1,:] 显然逗号前面是操作行,后面操作列。表示取全部行,-1就是步长,与python里的切片一样表示翻转。

19.

Q:反转二维数组 arr 的列

参考答案:

arr = np.arange(9).reshape(3,3)
res = arr[:,::-1]
print(res)

20.

Q:创建一个形状为5x3的二维数组,包含5到10之间的随机十进制浮点数。

参考答案:

arr = np.random.uniform(5,10,(5,3))

21,

Q:只打印或显示numpy数组rand_arr的小数点后三位。

参考答案:

np.set_printoptions(precision=3) # 没有任何接收值什么的。直接在需要控制的语句上面写就行了
rand_arr = np.random.random([5,3])

22.

Q:通过e式科学记数法来打印rand_arr(如1e10)

参考答案:

np.set_printoptions(suppress=False)
rand_arr = np.random.random([3,3])/1e3
print(rand_arr)
np.set_printoptions(suppress=True)
print(rand_arr)

前后输出对比:
[[8.86114528e-04 2.35134440e-04 8.55421233e-04]
 [5.52686672e-04 3.89532450e-04 4.37608757e-04]
 [1.91159998e-05 6.21107903e-04 8.97569376e-04]]
[[0.00088611 0.00023513 0.00085542]
 [0.00055269 0.00038953 0.00043761]
 [0.00001912 0.00062111 0.00089757]]

np.set_printoptions(precision=None, threshold=None, suppress=None)

  • precision:控制输出的小数点个数,默认是8
  • threshold:控制输出的值的个数,其余以…代替;
    当设置打印显示方式threshold=np.nan,意思是输出数组的时候完全输出,不需要省略号将中间数据省略
  • suppress: 当suppress=True,表示小数不需要以科学计数法的形式输出

23.

Q:将numpy数组a中打印的项数限制为最多6个元素。

参考答案:

np.set_printoptions(threshold=6)
a = np.arange(15)
print(a)
输出[ 0  1  2 ... 12 13 14]

24.

Q:打印完整的numpy数组a而不截断。

上一题中 使用了函数将中间一写值用省略号来表示了。
如果想再次全部显示,只需要设置 threshold = np.nan

25.

Q:从数组a中,将大于30的替换为30,小于10的替换为10

参考答案:

np.random.seed(100)
a = np.random.uniform(1,50, 20)
res = np.clip(a,10,30)
print(res)

clip(数组,小于N的变成N,大于X的变成X)

26.

Q:获取给定数组a中前5个最大值的位置。

参考答案:

np.random.seed(100)
a = np.random.uniform(1,50, 20)
print(a)
res = np.max(a[0:6],axis=0)
res = np.where(res)
print(res)

输出:
[27.62684215 14.64009987 21.80136195 42.39403048  1.23122395  6.95688692
 33.86670515 41.466785    7.69862289 29.17957314 44.67477576 11.25090398
 10.08108276  6.31046763 11.76517714 48.95256545 40.77247431  9.42510962
 40.99501269 14.42961361]
(array([0]),)

27.

Q:将res_9转换为扁平线性1d数组。

参考答案:

res_9 = np.arange(24).reshape((4,6))
res_10 = res_9.flatten()


res_9:
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
res_10:
 [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

27.

Q:按行计算唯一值的计数。

参考答案:


np.random.seed(100)
arr = np.random.randint(1,11,size=(6, 10))
res = []

for i in arr:
    temp = []
    for j in range(1,11):
        i = str(i)
        temp.append(i.count(str(j)))
    # print(temp)
    res.append(temp)
print(res)

28,

Q:计算给定数组中每行的最大值。

参考答案:

np.random.seed(100)
a = np.random.randint(1,10, [5,3])
print(a)


res = np.max(a,axis = 1)
print(res)

输出:
[[9 9 4]
 [8 8 1]
 [5 3 6]
 [3 3 3]
 [2 1 9]]
[9 8 6 3 9]

29.

Q:在给定的numpy数组中找到重复的条目(第二次出现以后),并将它们标记为True。第一次出现应该是False的。

参考答案:

a = np.arange(10)
res = np.full(a.shape, True)

Array=[0,0,3 ,0, 2, 4, 2, 2, 2, 2]
b = np.unique(Array,return_index=True)[1]
res[b] = False
print(res)

full函数就是填充数组内的值,使用后面的那个参数。

unique函数 去重的作用,return_index=True,返回去重之后的元素下标。

30.

Q:从一维numpy数组中删除所有NaN值

参考答案:

a = np.array([1,2,3,np.nan,5,6,7,np.nan])
res = np.isnan(a)
print(res)
res = np.delete(a,res)
print(res)

输出:
[False False False  True False False False  True]
[1. 2. 3. 5. 6. 7.]

isnan()将为nan的位置设置为True其他为False
delete() 删除

31.

Q:从2d数组a_2d中减去一维数组b_1D.

参考答案:

a_2d = np.array([[3,3,3],[4,4,4],[5,5,5]])
b_1d = np.array([1,1,1])
res = a_2d - b_1d
print(res)

输出:
[[2 2 2]
 [3 3 3]
 [4 4 4]]

32.

Q:找出x中数字1的第5次重复的索引。

参考答案:

x = np.array([1, 2, 1, 1, 3, 4, 3, 1, 1, 2, 1, 1, 2])

temp = 0
for index,val in enumerate(x):
    if val == 1:
        temp +=1
    if temp == 6:
        print(index)
        break
输出:10

这道题要找到数字1的第五次重复位置的下标,
网上的答案给的5显然是不对的。
注意这里是第五次重复。也就是第六次1出现的位置。

33.

Q:将numpy的datetime64对象转换为datetime的datetime对象

参考答案:

import numpy as np

from  datetime import datetime
dt64 = np.datetime64('2018-02-25 22:10:10')

print(dt64)
res = dt64.astype(datetime)
print(res)

34.

Q:创建长度为10的numpy数组,从5开始,在连续的数字之间的步长为3。

参考答案:

length = 10
start = 5
step = 3

leng = 5 + 3*10

res = np.arange(5,leng,3)
print(res)

这道题计算一下结尾数字就行了.

35.

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐