3.23-Numpy

文章介绍了NumPy在Python科学计算中的基础应用,包括数组的广播原则、轴(axis)的概念以及如何使用Numpy读取和处理数据。还涉及了Numpy中的数组转置、布尔索引和处理nan值的方法,如用平均值替换nan。
摘要由CSDN通过智能技术生成
  1. 一个在Python中做科学计算的基础库,重在数值计算, 也是大部分PYTHON科学计算库的基础库,多用于在大型、 多维数组上执行数值运算。
    在这里插入图片描述在这里插入图片描述
  2. 数组的形状:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  3. 广播原则
    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:
在这里插入图片描述
在这里插入图片描述

  1. 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)
  1. Numpy中的转置:
    在这里插入图片描述
  2. Numpy的索引和切片:
    在这里插入图片描述
    布尔索引
    在这里插入图片描述
    三元运算符
    在这里插入图片描述
    Clip
    在这里插入图片描述
  3. 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. 其他方法:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值