Anaconda 中對於該庫已經集成好了,不需要再進行繁瑣操作。如果用的不是 Anaconda 軟件,則需要自己通過 pip 命令來安裝Numpy庫。
import numpy
當不清楚Numpy中某個函數的功能及用法時,我們可以使用
print (help(numpy.所需查詢函數))
即可查詢該函數的幫助文檔。!。各參數解釋/函數舉例。
可以考慮編譯時同時開啓兩個編譯器,一個用於編碼,一個用於 print help 文檔。hiahia~~
Numpy中最核心的一個方法:.array 。構造一二三···維數組
#The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:
vector = numpy.array([5, 10, 15, 20])
#When we input a list of lists, we get a matrix as a result:
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print (vector)
print (matrix)
#We can use the ndarray.shape property to figure out how many elements are in the array
vector = numpy.array([1, 2, 3, 4])
print(vector.shape)
#For matrices, the shape property contains a tuple with 2 elements.
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)
matrix.shape 如下:
print (**.shape) 可以打印出數組的相關信息。我們再編碼過程中,可以通過對.shape 的實時查看,以保證所得即所求。
Numpy 的基本結構。
Numpy 中元素一定會是相同的類型。通過 .dtype 可以查看相應類型。
numbers = numpy.array([1, 2, 3, 4])
numbers.dtype
結果是: dtype('int32')
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1)
print(world_alcohol)
可通過數組相關操作來對以上數據進行相關操作。
vector = numpy.array([5, 10, 15, 20])
print(vector[0:3])
以上相當於取第一/第二列的值。結果爲:[5,10,15]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,1])
以上相當於取 所有行的第二列的值 結果是:[10 25 40]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,0:2])
取得是所有行的 第一/第二 列。
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[1:3,0:2])
取 第二/第三行的第一/第二列 結果爲 :[[20 25] [35 40]]
#it will compare the second value to each element in the vector
# If the values are equal, the Python interpreter returns True; otherwise, it returns False
vector = numpy.array([5, 10, 15, 20])
vector == 10
array([False, True, False, False])
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix == 25
對於數組元素的判斷,可直接通過上述方式判斷。得到的將是布爾值。
array([[False,False,False],
[False,Ture,False],
[False,False,False],],dtype=bool)
上慄很好的展示了應該如何使用布爾值(vector[equal_to_ten])可以取出相應元素或整行整列。哈哈哈
非但找出相應元素,而且可以通過vector來進行數據更新。
數據元素類型轉換
# The axis dictates which dimension we perform the operation on
#1 means that we want to perform the operation on each row, and 0 means on each column
就說牛不牛,哈哈 a.shape幾行幾列? a.ndim維度 a.dtype.name數據類型名 a.size元素個數
構造全1矩陣。2個3行4列。
構造等差數列。下限:0,上限:2,等差:0.3
產生的爲 (-1,1)區間上的隨機的值。
np.linspace( 0, 2*pi, 100 ) 在區間內(0,2pi)內平均的取100個值。
矩陣乘法。兩個矩陣相乘
如果是A*B,則爲對應位置,對應數字相乘。
數組元素更新:e^(元素相應數字)。sqrt就是根號。
floor 的意思是 對隨機數向下取整。
.ravel 方法 可以把一個矩陣拉伸變成一個向量(多維變一維)。
.reshape(3,-1) 指的是把原數組直接轉換成 3 行 ?列的數組。
.hstack((a,b)) 矩陣拼接操作。把數組 a,b 左右拼接起來。Horizontal
.vstack((a,b)) 矩陣拼接操作。把數組 a,b 上下拼接起來。Vertical
.hsplit(a,?) 矩陣切割操作。橫着對矩陣進行切分。第一項爲目標矩陣,第二項爲切分個數。
.vsplit(a,?) 矩陣切割操作。豎着對矩陣進行切分。第一項爲目標矩陣,第二項爲切分個數。
關於複製的操作。
.copy()
排序和索引的問題。:。
.argmax 與 .argmin兩個方法可以分別找出最大最小值。當然了,我們可以操作與指定 行 或 列。
.tile 方法做的是對原數組做一個拓展。.tile(a,(3,5)) 方法代表對 a 數組的 行擴大3倍,列擴大5倍。
.sort() 方法。如a.sort(axis=1)對數組a的行進行排序。
.argsort(a) 方法。對數組a中的數據對應的索引進行排序(升序)。
參考文獻:
唐課程:python科學計算庫——Numpy