- 一个在Python中做科学计算的基础库,重在数值计算, 也是大部分PYTHON科学计算库的基础库,多用于在大型、 多维数组上执行数值运算。
- 数组的形状:
- 广播原则
The broadcasting behaviors in NumPy are based on the idea of extending the smaller array so that it has compatible shape with the larger array. This involves two steps:
1.The smaller array is “broadcast” across the larger array so that they have the same number of dimensions. This is done by adding “1” dimensions to the smaller array as necessary.
2.The dimensions of the smaller array are repeated to match the corresponding dimensions of the larger array.
Once the arrays have compatible shapes, they can be operated on element-wise. NumPy will automatically perform the necessary broadcasting to make the operation work.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1], [2], [3]])
c = a + b
print(c)
4. 轴axis:
- Numpy读取数据:
import numpy as np
us_file_path = "./youtube_video_data/US_video_data_numbers.csv"
gb_file_path = "./youtube_video_data/GB_video_data_numbers.csv"
t1 = np.loadtxt(us_file_path,delimiter=",",dtype="int")
t2 = np.loadtxt(us_file_path,delimiter=",",dtype="int",unpack=True) # Transpose the array
print(t1)
print("*"*100)
print(t2)
- Numpy中的转置:
- Numpy的索引和切片:
布尔索引
三元运算符
Clip
- nan和inf:
@desc: np.isnan() is a mask and returns an array of boolean values
"""
import numpy as np
# replace nan value with the mean value of other numbers in the same column
def fill_ndarray(t1):
for i in range(t1.shape[1]): # iterate each column
cur_col = t1[:, i]
if np.count_nonzero(np.isnan(cur_col)) != 0: # 说明有nan
col_not_nan = cur_col[~np.isnan(cur_col)] # find out all the non-NaN numbers
cur_col[np.isnan(cur_col)] = col_not_nan.mean() # replace NaN with the mean values of the other non-NaN numbers
t1[:, i] = cur_col
return t1
if __name__ == '__main__':
t1 = np.arange(12).reshape((3, 4)).astype("float")
t1[1, 2:] = np.nan
print(t1)
t1 = fill_ndarray(t1)
print(t1)
9. 数组的拼接和行列交换:
10. 其他方法: