Numpy练习100题--难度★☆☆

时间:2021-10-16 18:33:17

1.Import the numpy package under the name np (★☆☆)

#导入numpy模块
import numpy as np

2.Print the numpy version and the configuration (★☆☆)

#打印numpy的版本信息
print np.version.version
print np.__version__
np.show_config()

3.Create a null vector of size 10 (★☆☆)

#新建全为0的数组
vector=np.zeros(10)#一维
vector_2=np.zeros((10,10))#二维

4.How to find the memory size of any array (★☆☆)

#数组所占内存大小=元素个数*每个元素的大小
print vector.size*vector.itemsize

5.How to get the documentation of the numpy add function from the command line? (★☆☆)

#命令行获取numpy的add函数的文档信息
python -c "import numpy;numpy.info(numpy.add)"

6.Create a null vector of size 10 but the fifth value which is 1 (★☆☆)

#创建非空的大小为10的数组,第5个元素为1
vector=np.zeros(10)
vector[4]=1

7.Create a vector with values ranging from 10 to 49 (★☆☆)

#创建一个数组,元素值从10-49
vector=np.arange(10,50)

8.Reverse a vector (first element becomes last) (★☆☆)

#翻转一个数组
vector[::-1]

9.Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)

#创建一个3*3的矩阵,元素值为0-8
vector=np.arange(0,9).reshape(3,3)

10.Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)

#查找数组的所有非0元素的下标
indices=np.nonzero([1,2,0,0,4,0])

11.Create a 3x3 identity matrix (★☆☆)

#创建3*3单位矩阵
vector=np.eye(3)

12.Create a 3x3x3 array with random values (★☆☆)

#创建3*3*3的随机数组
vector=np.random.random([3,3,3])

13.Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)

#创建10*10的随机数组,并找出最大值和最小值
vector=np.random.random([10,10])
v_max=vector.max()
v_min=vector.min()

14.Create a random vector of size 30 and find the mean value (★☆☆)

#创建大小为30的随机数组,求数组的平均值
vector=np.random.random(30)
v_mean=vector.mean()

15.Create a 2d array with 1 on the border and 0 inside (★☆☆)

#创建二维数组,边界元素为1,内部元素为0
vector_2d=np.zeros((5,5))
vector_2d[:,[0,-1]]=1#左右为 1
vector_2d[[0,-1],:]=1#上下为 1
#或者
vector_2d=np.ones((5,5))
vector_2d[1:-1,1:-1]=0

16.How to add a border (filled with 0’s) around an existing array? (★☆☆)

#给一个数组的外围填充0
vector=np.pad(vector,pad_width=1,mode='constant',constant_values=0)

17.What is the result of the following expression? (★☆☆)

0 * np.nan#np.nan
np.nan == np.nan#False
np.inf > np.nan#False
np.nan - np.nan#np.nan
0.3 == 3 * 0.1#False

18.Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)

#生成5*5矩阵,值1,2,3,4刚好在对角线下面
#k=0对角线,k>0对角线上面,k<0对角线下面
vector=np.diag(np.arange(1,5),k=-1)

19.Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)

#生成8*8棋盘模式
vector=np.zeros((8,8))
vector[::2,1::2]=1
vector[1::2,::2]=1

20.Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?

#一个6*7*8的三维数组,第100个元素的下标是多少
z=100%8
y=(100/8)%7
x=100/(8*7)
#unravel_index()将一维数组下标转化为
print np.unravel_index(100,(6,7,8))

21.Create a checkerboard 8x8 matrix using the tile function (★☆☆)

#使用tile函数创建8*8棋盘,tile函数表示将某一模式在行、列重复多少次
vector=np.tile([[1,0],[0,1]],(4,4))#[[1,0],[0,1]]在行列均重复4

22.Normalize a 5x5 random matrix (★☆☆)

#5*5随机矩阵归一化
vector=np.random.random((5,5))
#极值归一化
vector=(vector-vector.min())/(vector.max()-vector.min())
#标准化
vector=(vector-vector.mean())/vector.std()

23.Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)

#自定义类型,用4个无符号字节来表示RGBA
color=np.dtype([('R',np.ubyte),('G',np.ubyte),('B',np.ubyte),('A',np.ubyte)])

24.Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)

#实数矩阵5*3,3*2的乘法
A=np.ones((5,3))
B=np.ones((3,2))
vector=np.dot(A,B)#numpy中*是逐元素计算,dot是矩阵乘法

25.Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)

#把一维数组中3-8之间的元素取反
vector=np.arange(10)
vector[vector>3&vector<8]*=-1

26.What is the output of the following script? (★☆☆)

# Author: Jake VanderPlas
sum(range(5),-1)#9,sum是python内建函数,sum(sequence[,start]),相当于-1+1+2+3+4=9
from numpy import *
#numpy.sum(a, axis=None),axis表示沿着哪个轴求和,因为数组是一维的,所以axis的大小没关系
sum(range(5),-1)

27.Consider an integer vector Z, which of these expressions are legal? (★☆☆)

#legal
Z**Z#**是乘方运算,element-wise
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z

28.What are the result of the following expressions?

np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
29.How to round away from zero a float array ? (★☆☆)

30.How to find common values between two arrays? (★☆☆)

31.How to ignore all numpy warnings (not recommended)? (★☆☆)

32.Is the following expressions true? (★☆☆)

np.sqrt(-1) == np.emath.sqrt(-1)
33.How to get the dates of yesterday, today and tomorrow? (★☆☆)