numpy 练习题(附难度、答案、解析)

NumPy数据分析库需要掌握的知识点:

  • NumPy基本数据结构
  • NumPy数组操作
  • NumPy矩阵操作
  • NumPy随机数的生成
  • NumPy常用函数(数学类、统计学类等)
  • NumPy数据处理
  • NumPy文件操作

100 numpy exercises

Github资源链接:https://github.com/rougier/numpy-100
版本:Python 3.0

1. Import the numpy package under the name np (★☆☆)
import numpy as np
2. Print the numpy version and the configuration (★☆☆)
print(np.__version__)
np.show_config()
3. Create a null vector of size 10 (★☆☆)
Z = np.zeros(10)
print(Z)
4. How to find the memory size of any array (★☆☆)
Z = np.zeros((10,10))
print("%d bytes" % (Z.size * Z.itemsize))
5. How to get the documentation of the numpy add function from the command line? (★☆☆)
%run `python -c "import numpy; numpy.info(numpy.add)"`
6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
Z = np.zeros(10)
Z[4] = 1
print(Z)
7. Create a vector with values ranging from 10 to 49 (★☆☆)
Z = np.arange(10,50)
print(Z)
8. Reverse a vector (first element becomes last) (★☆☆)
Z = np.arange(50)
Z = Z[::-1]  # python切片
print(Z)
9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
Z = np.arange(9).reshape(3,3)
print(Z)
10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
nz = np.nonzero([1,2,0,0,4,0])
print(nz)
11. Create a 3x3 identity matrix (★☆☆)
Z = np.eye(3)  #单位矩阵
print(Z)
12. Create a 3x3x3 array with random values (★☆☆)
Z = np.random.random((3,3,3))
print(Z)
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
Z = np.random.random((10,10))
Zmin, Zmax = Z.min(), Z.max()
print(Zmin, Zmax)
14. Create a random vector of size 30 and find the mean value (★☆☆)
Z = np.random.random(30)
m = Z.mean()
print(m)
15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
Z = np.ones((10,10))
Z[1:-1,1:-1] = 0
print(Z)
16. How to add a border (filled with 0’s) around an existing array? (★☆☆)
Z = np.ones((5,5))
Z = np.pad(Z, pad_width=1, mode='constant', constant_values=0)
print(Z)
17. What is the result of the following expression? (★☆☆)
print(0 * np.nan)
print(np.nan == np.nan)
print(np.inf > np.nan)
print(np.nan - np.nan)
print(np.nan in set([np.nan]))
print(0.3 == 3 * 0.1)
18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
Z = np.diag(1+np.arange(4),k=-1)
print(Z)
19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
Z = np.zeros((8,8),dtype=int)
Z[1::2,::2] = 1
Z[::2,1::2] = 1
print(Z)
20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
print(np.unravel_index(99,(6,7,8)))
21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)
Z = np.tile( np.array([[0,1],[1,0]]), (4,4))
print(Z)
22. Normalize a 5x5 random matrix (★☆☆)
# 矩阵标准化
Z = np.random.random((5,5))
Z1 = (Z-np.mean(Z))/(np.std(Z)) # mean取平均值,std计算矩阵标准差
# Z2 = (Z-Z.mean())/(Z.std()) 结果相同
print(Z1)
23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
# 自定义数据类型
color = np.dtype([("r", np.ubyte, 1),
                  ("g", np.ubyte, 1),
                  ("b", np.ubyte, 1),
                  ("a", np.ubyte, 1)])
24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
A = np.ones((5,3))
B = np.ones((3,2))
C = np.dot(A,B) 
# 等同于 C = A @ B
print (C)
25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
# Author: Evgeni Burovski

Z = np.arange(11)
Z[(3 < Z) & (Z <= 8)] *= -1
print(Z)
26. What is the output of the following script? (★☆☆)
# Author: Jake VanderPlas

print(sum(range(5),-1)) # python内置sum,求和后再加(-1)
from numpy import *
print(sum(range(5),-1)) # np内sum,-1是维度
27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z
28. What are the result of the following expressions?
print(np.array(0) / np.array(0))
print(np.array(0) // np.array(0))
print(np.array([np.nan]).astype(int).astype(float))
29. How to round away from zero a float array ? (★☆☆)
# 远离0取整
Z = np.random.uniform(-10,+10,10) # uniform() 随机实数
print (np.copysign(np.ceil(np.abs(Z)), Z)) # copysign() 复制符号
30. How to find common values between two arrays? (★☆☆)
Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print("Z1: " , Z1)
print("Z2: " , Z2)
print(np.intersect1d(Z1, Z2)) # 取交集
31. How to ignore all numpy warnings (not recommended)? (★☆☆)
# Suicide mode on
defaults = np.seterr(all="ignore")
Z = np.ones(1) / 0

# Back to sanity
_ = np.seterr(**defaults)

An equivalent way, with a context manager:

with np.errstate(divide='ignore'):
    Z = np.ones(1) / 0
32. Is the following expressions true? (★☆☆)
np.sqrt(-1) == np.emath.sqrt(-1)
33. How to get the dates of yesterday, today and tomorrow? (★☆☆)
yesterday = np.datetime64('today', 'D') - np.timedelta64(1, 'D')
today     = np.datetime64('today', 'D')
tomorrow  = np.datetime64('today', 'D') + np.timedelta64(1, 'D')
34. How to get all the dates corresponding to the month of July 2016? (★★☆)
Z = np.arange('2016-07', '2016-08', dtype='datetime64[D]')
print(Z)
35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
A = np.ones(3)*1
B = np.ones(3)*2
C = np.ones(3)*3
np.add
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值