Python基礎
1.格式化輸出:
使用f-string
print(f"str{vvariable}") 花括號( curly braces )中可以為式子
The content between the curly braces is evaluated when producing the output.
Numpy相關
1.np.array 與np.ndarray的區別
即ndarray是類,而array是函數,array構建的是一個ndarray的對象,使用默認構造函數創建的ndarray對象的數組元素是隨機值,而numpy提供了一系列的創建ndarray對象的函數,array()就是其中的一種;
https://blog.csdn.net/weixin_43708622/article/details/111138281
2.np.reshape 與np.resize的區別
在numpy模塊中,我們經常會使用resize 和 reshape,在具體使用中,通常是使用resize改變數組的尺寸大小,使用reshape用來增加數組的維度。
Reshape
The previous example used reshape to shape the array.
a = np.arange(6).reshape(-1, 2)
This line of code first created a 1-D Vector of six elements. It then reshaped that vector into a 2-D array using the reshape command. This could have been written:
a = np.arange(6).reshape(3, 2)
To arrive at the same 3 row, 2 column array.
The -1 argument tells the routine to compute the number of rows given the size of the array and the number of columns.
| 屬性 | 說明 |
|---|---|
| ndarray.ndim | 秩,即軸的數量或維度的數量 |
| ndarray.shape | 數組的維度,對于矩陣,n 行 m 列 |
| ndarray.size | 數組元素的總個數,相當于 .shape 中 n*m 的值 |
| ndarray.dtype | ndarray 對象的元素類型 |
| ndarray.itemsize | ndarray 對象中每個元素的大小,以字節為單位 |
| ndarray.flags | ndarray 對象的內存信息 |
| ndarray.real | ndarray元素的實部 |
| ndarray.imag | ndarray 元素的虛部 |
| ndarray.data | 包含實際數組元素的緩沖區,由于一般通過數組的索引獲取元素,所以通常不需要使用這個屬性。 |
3.注意區別np.array([[1, 3, 5, 7, 9]])與np.array([1, 3, 5, 7, 9])的區別
前者的shape為(1,5),后者為(5,)
浙公網安備 33010602011771號