Abstract:Numpy是Python做数据分析所必须要掌握的基础库之一。这篇练习通过89道题目带你快速玩转Numpy。
介绍
Numpy是Python做数据分析所必须要掌握的基础库之一。这篇练习通过89道题目带你快速玩转Numpy。
练习
# coding:utf-8 import numpy as np import pandas as pd # 1.Print the numpy version and the configuration print (np.__version__) print np.show_config() # 2. Create a null vector of size 10 Z = np.zeros(10) print Z # 3.Create a null vector of size 10 but the fifth value which is 1 A = np.zeros(10) A[4] = 1 print A # 4.Create a vector with values ranging from 10 to 49 A = np.arange(50) print A # 5.Reverse a vector (first element becomes last) A = np.arange(50) A = A[::-1] print A # 6.Create a 3x3 matrix with values ranging from 0 to 8 Z = np.arange(9).reshape(3,3) print Z # 7.Find indices of non-zero elements from [1,2,0,0,4,0] nz = np.nonzero([1,2,0,0,4,0]) print (nz) # 8.Create a 3x3 identity matrix A = np.eye(3) print A # 9.create a 3*3*3 array with random values A = np.random.random((3,3,3)) print (Z) # 10.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) # 11.Create a random vector of size 30 and find the mean value A = np.random.random(30) m = A.mean() print (m) # 12.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) # 13.How to add a border (filled with 0's) around an existing array? A = np.ones((5,5)) # ones(): 返回给定形状和类型的新数组,用数字填充 A = np.pad(A,pad_width=1, mode='constant', constant_values=0) # pad():filling the array print A # 14.What is the result of the following expression? print (0 * np.nan) print (np.nan == np.nan) print (np.nan - np.nan) print (0.3 == 3 * 0.1) # 15.Create a 5x5 matrix with values 1,2,3,4 just below the diagonal A = np.diag(1+np.arange(4),k=-1) print A # 16.Create a 8x8 matrix and fill it with a checkerboard pattern A = np.zeros((8,8),dtype=int) A[1::2,::2] = 1 A[::2,1::2] = 1 print A # 17.Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element print (np.unravel_index(100,(6,7,8))) # unravel_index(): 将平面索引的平面索引或数组转换为坐标数组的元组 # 18.Create a checkerboard 8x8 matrix using the tile function Z = np.tile( np.array([[0,1],[1,0]]), (4,4)) print(Z) # 19.Normalize a 5x5 random matrix Z = np.random.random((5,5)) Zmax, Zmin = Z.max(), Z.min() Z = (Z - Zmin)/(Zmax - Zmin) print(Z) # 20. 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)]) # 21.Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) A = np.dot(np.ones((5,3)),np.ones((3,2))) print A B = np.dot(np.ones((1,2)),np.ones((2,2))) print B # 22.Given a 1D array, negate all elements which are between 3 and 8, in place. A = np.arange(11) A[(3 < A) & (A <= 8)] *= -1 print A # 23.What is the output of the following script? print (sum(range(5),-1)) # 24.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)) # 25.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)) # 26.How to find common values between two arrays? A1 = np.random.randint(0,10,10) A2 = np.random.randint(0,10,10) print A1,A2 print (np.intersect1d(A1,A2)) # 27.How to ignore all numpy warnings (not recommended)? defaults = np.seterr(all="ignore") Z = np.ones(1) / 0 _ = np.seterr(**defaults) with np.errstate(divide='ignore'): Z = np.ones(1) / 0 # 28. Is the following expressions true? # numpy.sqrt():按元素方式返回数组的正平方根 print np.sqrt(-1) == np.emath.sqrt(-1) # 29. 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')