1. 程式人生 > >Numpy練習100題--難度★☆☆

Numpy練習100題--難度★☆☆

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? (★☆☆)