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号