1. Import the numpy package under the name np
導入numpy包,命名爲np
import numpy as np
2. Print the numpy version and the configuration
打印numpy版本和配置
3. Create a null vector of size 10
創建一個10*10的0數組
4. How to find the memory size of any array
如何查看數組佔內存大小
5. How to get the documentation of the numpy add function?
如何查看numpy add函數文檔
6. Create a null vector of size 10 but the fifth value which is 1
創建一個長度爲10的0數組,第5個值爲1
7. Create a vector with values ranging from 10 to 49
創建一個值從10到49的數組
8. Reverse a vector (first element becomes last)
反轉數組(第一個元素變成最後一個)
9. Create a 3x3 matrix with values ranging from 0 to 8
創建一個從0~8的3*3矩陣
10. Find indices of non-zero elements from [1,2,0,0,4,0]
從[1,2,0,0,4,0]中找到非0元素的索引
11. Create a 3x3 identity matrix
生成一個3*3的對角矩陣
12. Create a 3x3x3 array with random values
創建一個333的隨機值數組
13. Create a 10x10 array with random values and find the minimum and maximum values
創建一個10*10的隨機值數組,並找到最大最小值
14. Create a random vector of size 30 and find the mean value
創建一個長度爲30的隨機值數組,並找到平均值
15. Create a 2d array with 1 on the border and 0 inside
創建一個四邊爲1,中間爲0的二維數組
16. How to add a border (filled with 0’s) around an existing array
如何給一個已經存在的數組添加邊(填充0)
numpy.pad使用’constant’方法二維數組填充
np.pad(Matrix,((1,1),(1,2)),‘constant’,constant_values = (0,0)) 表示在二維數組Matrix的邊緣填充constant_values指定的數值
(1,1)表示在Matrix的第[0]軸填充(二維數組中,0軸表示行),即在0軸前面填充1個寬度的0,比如數組Matrix中的1,2,3兩個元素前面各填充了一行0,在4,5,6下面填充了一行0。
(1,2)表示在Matrix的第[1]軸填充(二維數組中,1軸表示列),即在1軸前面填充1個寬度的0,後面填充2個寬度的constant_values表示填充值,且(axis0,axis1)的填充值等於(0,0)。
17. What is the result of the following expression
看看下面表達式的結果是什麼
inf和nan
Python中的正無窮或負無窮,使用float(“inf”)或float("-inf")來表示。
這裏有點特殊,寫成:float(“inf”),float(“INF”)或者float(‘Inf’)都是可以的。
當涉及 > 和 < 比較時,所有數都比無窮小float("-inf")大,所有數都比無窮大float(“inf”)小。
相等比較時,float("+inf")與float("+inf")、float(“inf”)三者相等。簡單的加,減,乘操作,還是會返回無窮。
nan代表Not A Number(不是一個數),它並不等於0。因爲nan不是一個數,所以相關計算都無法得到數字。所有涉及nan的操作,返回的都是nan。比較操作時,返回的都是False。
18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal
創建一個5*5矩陣,對角線下方值爲1,2,3,4
numpy.diag(v,k=0)
以一維數組的形式返回方陣的對角線(或非對角線)元素,或將一維數組轉換成方陣(非對角線元素爲0).兩種功能角色轉變取決於輸入的v。
v : array_like
如果v是2D數組,返回k位置的對角線。
如果v是1D數組,返回一個v作爲k位置對角線的2維數組。
k : int, optional
對角線的位置。大於零位於對角線上面,小於零則在下面。
19.Create a 8x8 matrix and fill it with a checkerboard pattern
創建一個8*8矩陣,並用棋盤圖案填充
20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element
給定一個678的三維矩陣,求100個元素的索引是什麼
numpy.unravel_index
numpy.unravel_index(indices, dims)
獲取一個/組(int)類型的索引值在一個多維數組中的位置。