Numpy 100题 —— Neophyte 初学者 10道题

来源:互联网 发布:安卓网络修复大师 编辑:程序博客网 时间:2024/06/07 05:23

1. Import the numpy package under the name np

import numpy as np

2. Print the Julia version

# Versionprint np.__version__

3. Create a null vector of size 10

#Z = zeros(10)Z = np.zeros(10)print Z

4. Create a null vector of size 10 and set the fifth value to 1

#Z = zeros(10)#Z[5] = 1#ZZ = np.zeros(10)Z[5]=1print Z[5],'\n',Z

5. Create a vector with values ranging from 10 to 99

#Z = [10:99]Z = np.arange(10, 100)print Z

6. Create a 3x3 matrix with values ranging from 0 to 8

#Z = reshape(0:8, 3, 3)Z = np.arange(0,9).reshape((3,3))print Z

7. Find indices of non-zero elements from [1,2,0,0,4,0]

#nz = find([1,2,0,0,4,0])Z = [1,2,0,0,4,0]nz = np.nonzero(Z)print nz

8. Create a 3x3 identity matrix (单位阵)

#Z = eye(3)Z = np.eye(3)print Z,'\n'

9. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal

#Z = diagm(1:4, -1)Z = np.diag(np.arange(1,5),k=-1)print(Z)

10. Create a 10x10x10 array with random values

#rand(10, 10, 10)prng = np.random.RandomState(123456789) # 定义局部种子xx = prng.rand(2, 4)print xx
0 0