Github-关于numpy的100道题-★☆☆
- numpy 100 ★☆☆
- 1. Import the numpy package under the name np (★☆☆)
- 2. Print the numpy version and the configuration (★☆☆)
- 3. Create a null vector of size 10 (★☆☆)
- 4. How to find the memory size of any array (★☆☆)
- 5. How to get the documentation of the numpy add function from the command line? (★☆☆)
- 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
- 7. Create a vector with values ranging from 10 to 49 (★☆☆)
- 8、Reverse a vector (first element becomes last) (★☆☆)
- 9、Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
- 10、 Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
- 11、Create a 3x3 identity matrix (★☆☆)
- 12、Create a 3x3x3 array with random values (★☆☆)
- 13、Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
- 14、Create a random vector of size 30 and find the mean value (★☆☆)
- 15、 Create a 2d array with 1 on the border and 0 inside (★☆☆)
- 16、 How to add a border (filled with 0's) around an existing array? (★☆☆)
- 17、What is the result of the following expression? (★☆☆) (★☆☆)
- 18、Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
- 19、 Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
- 20、Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
- 21、Create a checkerboard 8x8 matrix using the tile function (★☆☆)
- 22、Normalize a 5x5 random matrix (★☆☆)
- 23、Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
- 24、Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
- 25、Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
- 26、What is the output of the following script? (★☆☆)
- 27、Consider an integer vector Z, which of these expressions are legal? (★☆☆)
- 28、What are the result of the following expressions?
- 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? (★☆☆)
- 33、How to get the dates of yesterday, today and tomorrow? (★☆☆)
numpy 100 ★☆☆
小白初学,多多交流
原链接链接:https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises.md;
答案链接为:https://www.cnblogs.com/wyb6231266/p/11273486.html
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__)
np.show_config()
3. Create a null vector of size 10 (★☆☆)
#创建一个大小为10的零向量。
#创建指定大小的数组,数组元素以 0 来填充:
A = np.zeros(10)
print (A)
4. How to find the memory size of any array (★☆☆)
#查找数组的内存大小
#size元素个数,itemsize每个元素的大小,下面是10 8bytes
A = np.zeros(10)
print (A.size,A.itemsize,"bytes")
5. How to get the documentation of the numpy add function from the command line? (★☆☆)
#如何在命令行获取add函数的官方文档
np.info(np.add)
x1 = np.arange(9.0).reshape((3,3)) #首先生成的是从0.0开始的矩阵【0,1,2,3,4,5,6,7,8】,然后修改形状
x2 = np.arange(3.0)#arrange后面只写一个数字的画,默认为步长为1,否则自己设定,如arange(1,100,10)
print (np.add(x1,x2))
值得注意的是,若两个矩阵维度不相同也可以利用add相加,结果如下所示:
[[ 0. 2. 4.]
[ 3. 5. 7.]
[ 6. 8. 10.]]
6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
#创建一个大小为10的零向量,第五个值为1
A = np.zeros(10)
A[4] = 1
print (A)
7. Create a vector with values ranging from 10 to 49 (★☆☆)
A = np.arange(10,50) #arange(start,end)表示从start到end-1
8、Reverse a vector (first element becomes last) (★☆☆)
**A = np.arange(0,5)**
A=A[::-1] #这个就是将数组所有元素逆置 ;索引;-1为步长,所以就是
9、Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
#创建一个0-8 3*3的矩阵
A= np.arange(9).reshape(3,3)
10、 Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
#从上面列表中找到非0元素的索引
**A = [1,2,0,0,4,0]
index = np.nonzero(A)**
#结果是(array([0, 1, 4], dtype=int64),)
11、Create a 3x3 identity matrix (★☆☆)
#创建3*3的单位矩阵
A = np.eye(3)
12、Create a 3x3x3 array with random values (★☆☆)
#创建3个3行3列的矩阵,数值为随机数
**A = np.random.random((3,3,3))** #第一个3为创建的矩阵的个数,若为2,就是2个3*3的矩阵
13、Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
#创建10*10矩阵,数值为随机数
A = np.random.random((10,10))
mina = A.min()
maxa = np.max(A)
14、Create a random vector of size 30 and find the mean value (★☆☆)
#创建大小为30的随机向量,求平均值
A = np.random.random(30)
B = A.mean()
15、 Create a 2d array with 1 on the border and 0 inside (★☆☆)
#创建一个二维数组,就是矩阵咯,边上都是1,里面都是0
**A=np.ones((5,5))
A[1:-1,1:-1]=0 #索引是左闭右开的,所以,逗号前面代表行,表示包括第2行到倒数第2行;逗号后面代表列,表示包括第2列到倒数第2列,里面的数都是0
print(A)**
16、 How to add a border (filled with 0’s) around an existing array? (★☆☆)
#在一个已有矩阵周围增加一周0
A = np.random.random((5,5))
padimg= np.pad(A, ((1, 1), (1, 1)), 'constant', constant_values=(0, 0))
17、What is the result of the following expression? (★☆☆) (★☆☆)
0 * np.nan--》nan表示not a number--》*Out[37]: nan*
np.nan == np.nan--》*Out[38]: False*
np.inf > np.nan--》inf为无穷大--》Out[39]: False
np.nan - np.nan--》Out[40]: nan
np.nan in set([np.nan])--》Out[41]: True
0.3 == 3 * 0.1--》Out[42]: False--》因为计算机中对浮点数的处理,并不是简单的我们看上去的这么短,其实后面还有很长
18、Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
#创建一个5*5的矩阵,把对角线线下一行改为1、2、3、4
#自己的傻办法
A = np.random.random((5,5))
A[1,0] = 1
A[2,1] = 2
A[3,2] = 3
A[4,3] = 4
print(A)
#大神的好办法,diag函数
Z = np.diag(1+np.arange(4),k=-1)
print(Z)
19、 Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
#创建一个8*8的矩阵,并用棋盘模式填充
#这里也是利用到了索引的方法,记住索引【左闭右开】
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?
#unravel_index就是查找在一个矩阵中 ,索引的位置
print(np.unravel_index(99,(6,7,8)))
#运行结果为(1,3,5)意思就是在第一个矩阵中的,第三行第五列
21、Create a checkerboard 8x8 matrix using the tile function (★☆☆)
#创建一个8*8的棋盘,使用tile函数
C = np.identity(2) #定义一个2*2的对角阵
B = np.tile(C,(4,4)) #行列分别复制4遍
22、Normalize a 5x5 random matrix (★☆☆)
#标准化、一个随机的5*5矩阵,标准差标准化
A = np.random.random((5,5))
A = (A-A.mean())/np.std(A)
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.random.random((5,3)) /A=np.ones((5,3))
B = np.arange(9).reshape(3,2)/B=np.ones((3,2))
C = A.dot(B)
或者C = A@B
25、Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
Z = np.arange(11)
Z[(Z<8)&(Z>3)]=Z[(Z<8)&(Z>3)]*(-1)
#Z[(Z<8)&(Z>3)]*= -1
26、What is the output of the following script? (★☆☆)
#这里学习的是sum函数的用法,sum(iterable[, start]),iterable可迭代对象,比如说列表(list)、元组(tuple)、集合(set)、字典(dictionary)。start – 指定相加的参数,如果没有设置这个值,默认为0。也就是说sum()最后求得的值 = 可迭代对象里面的数加起来的总和(字典:key值相加) + start的值(如果没写start的值,则默认为0)
from numpy import *
print(sum(range(5),-1))
#属于这里值为0+1+2+3+4-1 =10
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?
np.array(0) / np.array(0)-----Out[76]: nan
np.array(0) // np.array(0)------Out[77]: 0
np.array([np.nan]).astype(int).astype(float)---rray([-2.14748365e+09])
29、How to round away from zero a float array ? (★☆☆)
#向上取整,向下取整
Z = np.random.uniform(-10,+10,10)
print(np.copysign(np.ceil(np.abs(Z)), Z))
# More readable but less efficient
print(np.where(Z>0, np.ceil(Z), np.floor(Z)))
30、How to find common values between two arrays? (★☆☆)
#找到两组数组里面的相同值
Z1 = np.random.randint(0,10,10)
Z2 = np.random.randint(0,10,10)
print(np.intersect1d(Z1,Z2))
31、How to ignore all numpy warnings (not recommended)? (★☆☆)
defaults = np.seterr(all="ignore")
Z = np.ones(1) / 0
# Back to sanity
_ = np.seterr(**defaults)
# Equivalently with a context manager
with np.errstate(all="ignore"):
np.arange(3) / 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') - np.timedelta64(1)
today = np.datetime64('today')
tomorrow = np.datetime64('today') + np.timedelta64(1)