• 数据分析 第三周 (numpy数组的处理 , numpy数组的运用与画图的结合)笔记


    numpy 从文件中读取数据 loadtxt

    #np.loadtxt(frame = "路径" , dtype = 数据类型 , delimiter = "间隔符" , skiprows = x 间隔行 , unpack = True / False 是否转置)
    
    import numpy as np
    
    us_file_path = "F:/All date/US_video_data_numbers.csv"
    uk_file_path = "F:/All date/GB_video_data_numbers.csv"
    
    t1 = np.loadtxt(us_file_path,delimiter=",",dtype="int")
    t2 = np.loadtxt(uk_file_path,delimiter=",",dtype="int")
    
    print(t1)
    print("*"*100)
    print(t2)
    
    #矩阵的转置(t.T)
    
    tt = np.array(range(12)).reshape(2,6)
    print(tt)
    print("*"*100)
    ts = tt.T
    print(ts)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    数组的索引和切片

    #数组的索引 和 切片
    import numpy as np
    
    us_file_path = "F:/All date/US_video_data_numbers.csv"
    uk_file_path = "F:/All date/GB_video_data_numbers.csv"
    
    t1 = np.loadtxt(us_file_path,delimiter=",",dtype="int")
    t2 = np.loadtxt(uk_file_path,delimiter=",",dtype="int")
    
    
    #取连续的数据用 “:”
    
    #取第2 - 3行    
    print(t2[ 1 : 3 , : ])
    print("-" * 100)
    #取第2 - 3列    
    print(t2[ : , 1 : 3 ])
    print("-" * 100)
    
    #取不连续的数据用 “[ ]”
    
    #取第 2 , 3 ,5 行
    print(t2[[1 , 2 , 4],  :  ])
    print("-" * 100)
    #取第 2 , 3 列
    print(t2[ : , [1 , 2] ])
    
    
    print("-" * 100)
    #取多个不相邻的点
    #选出来的结果是(0,0) (2,1) (2,3)
    print(t2[[0,2,2],[0,1,3]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    用索引和切片修改数组的值

    #用索引和切片修改数组的值
    
    import numpy as np
    
    
    #用某个条件对数组进行修改
    
    t1 = np.array(range(10)).reshape(2,5)
    t1[t1 < 5] = 2
    print(t1)
    
    
    print("-" * 100)
    #用切片和索引修改
    t1[ : , [1] ] = 3
    
    print(t1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    用索引交换行和列

    #交换行和列
    
    import numpy as np
    
    t1 = np.array(range(10)).reshape(2,5)
    
    print(t1)
    print("-" * 100)
    
    #用类似于交换两个数的方式交换行和列
    t1[ [0,1] , ] = t1[ [1,0] , ]
    
    print(t1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    数据的合并

    #合并两组数据
    # np.hstack  水平拼接     np.vstack 竖直拼接  里面放 tuple
    # np.zeros 生成一个全是 0 的数组  np.ones 生成一个全是 1 的数组 里面放 tuple
    
    
    #任务:把两个国家的数据打标记后拼接
    
    
    import numpy as np
    
    us_file_path = "F:/All date/US_video_data_numbers.csv"
    uk_file_path = "F:/All date/GB_video_data_numbers.csv"
    
    text1 = np.loadtxt(us_file_path , delimiter = ',' , dtype = "int")
    text2 = np.loadtxt(uk_file_path , delimiter = ',' , dtype = "int")
    
    
    # shape[0] 获取行数 shape 获取某一维的大小
    num1 = text1.shape[0]
    num2 = text2.shape[0]
    
    f1 = np.zeros((num1 , 1))
    f2 = np.ones((num2 , 1))
    
    text1 = np.hstack((text1 , f1))
    text2 = np.hstack((text2 , f2))
    
    text3 = np.vstack((text1 , text2)) 
    
    text3 = text3.astype("int")
    
    print(text3)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    求矩阵某一维最大值位置

    # 创建一个单位矩阵 np.eye
    
    def printf(x):
        print(x)
        print("-" * 60)
    import numpy as np
    
    n1 = np.eye(7)
    printf(n1)
    
    #求某一维的 最大 最小值 的位置 np.armax np.armin(数组 , axis = 维度)
    
    t1 = np.argmax(n1 , axis = 0)
    printf(t1)
    n1[n1 == 1] = -1
    printf(n1)
    
    t2 = np.argmin(n1 , axis = 1)
    printf(t2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    numpy 随机数的运用 (seed and randint)

    # seed 产生一个随机数种子保证每次的随机数都相等
    # randint(low , high , (type)) 产生一个有范围有形状的随机数数组
    
    def printf(x):
        print(x)
        print("-" * 60)
    
    import numpy.random as npr
    # import numpy as np
    
    npr.seed(10)
    
    t = npr.randint(0 , 30 , (3 , 4))
    
    printf(t)
    
    #深拷贝和浅拷贝
    
    #赋值操作都是浅拷贝 , 一个会影响另一个
    #但是复制操作是深拷贝 , 不会产生影响
    
    a = t.copy()
    
    a[a <= 40] = 0
    
    printf(a)
    printf(t)
    
    b = a
    
    a[a == 0] = 1
    
    printf(b)
    
    #深拷贝互不影响
    #浅拷贝藕断丝连
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    numpy 数组 nan 的计数 np.count_nonzeros

    
    # np.nan != np.nan 利用这个性质求出 nan 的个数
    # np.count_zero()
    
    #统计 nan 的个数
    
    import numpy as np
    import numpy.random as npr
    
    def printf(x):
        print(x)
        print("-" * 60)
        
    t = npr.randint(0, 30, (3, 5))
    printf(t)
    
    # printf(type(np.nan))
    
    t = t.astype("float")
    
    t[t < 10] = np.nan    
    
    printf(t)
    
    
    print(np.count_nonzero(t!=t))
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    应用:用列平均值 替换数组中 nan 值

    #用列平均值替换数组所有 nan
    
    import numpy as np
    # import numpy.random as npr
    
    def printf(x):
        print(x)
        print("-" * 60)
        
        
    def change(t1):
        
        for i in range(t1.shape[1]):
            nl = t1[ : , i ]
            
            cnt = np.count_nonzero(nl != nl)
            
            if(cnt != 0):
                nl[nl != nl] = nl[nl == nl].mean()
            
    #t.mean(axis = x) 求平均值
    #t.max(axis = x)  求最大值
    #t.sum(axis = x)  求和    
            
            
            
        
    if __name__ == "__main__":
        
        t = np.array(range(15)).reshape(3,5).astype("float")
        
        t[ 2 , 3 : ] = np.nan
        
        printf(t)
        
        change(t)
        
        printf(t)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    numpy 数组提取文件数据然后做正方图

    import numpy as np
    from matplotlib import pyplot as plt
    import matplotlib as mtb
    
    
    def printf(x):
        print(x)
        print("-" * 60)
        
    mtb.rcParams['font.sans-serif'] = ["SimHei"]
    mtb.rcParams["axes.unicode_minus"] = False
    plt.figure(figsize = (15,5) , dpi = 80)
    
    
    us_file_path = "F:/All date/US_video_data_numbers.csv"
    uk_file_path = "F:/All date/GB_video_data_numbers.csv"
    
    t1 = np.loadtxt(us_file_path,delimiter=",",dtype="int")
    t2 = np.loadtxt(uk_file_path,delimiter=",",dtype="int")
    
    us = t1[ : , -1]
    uk = t2[ : , -1]
    #一定要处理离群数据
    us = us[us <= 10000]
    uk = us[us <= 10000]
    
    # printf(max(us))
    
    d = 500
    
    num = (max(us) - min(us)) // d
    
    
    
    plt.hist(us , [min(us) + i * d for i in range(num + 2)], color = "#FF7F50")
    
    
    plt.xticks(range(min(us) , max(us) + 2 * d , d))
    plt.yticks(range(1,1000,50))
    
    
    #设置标签
    plt.xlabel("评论数量")
    plt.ylabel("人数")
    plt.title("美国油管前1000评论数量统计")
    
    
    # 设置网格 alpha 是清晰度
    plt.grid(alpha = 0.3 , color = "#000000")
    
    
    #显示图像
    plt.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    在这里插入图片描述

    numpy提取文件数据做散点图

    import numpy as np
    from matplotlib import pyplot as plt
    import matplotlib as mtb
    
    
    def printf(x):
        print(x)
        print("-" * 60)
        
    mtb.rcParams['font.sans-serif'] = ["SimHei"]
    mtb.rcParams["axes.unicode_minus"] = False
    plt.figure(figsize = (15,5) , dpi = 80)
    
    
    us_file_path = "F:/All date/US_video_data_numbers.csv"
    uk_file_path = "F:/All date/GB_video_data_numbers.csv"
    
    t1 = np.loadtxt(us_file_path,delimiter=",",dtype="int")
    t2 = np.loadtxt(uk_file_path,delimiter=",",dtype="int")
    
    us_p = t1[ : , -1]
    uk_p = t2[ : , -1]
    us_l = t1[ : , 1]
    uk_l = t2[ : , 1]
    
    plt.scatter(us_p , us_l , label = "美国" , color = "#4B0082"  )
    
    plt.xticks(range(min(us_p) , max(us_p) , 30000))
    
    
    #设置标签
    plt.xlabel("评论数")
    plt.ylabel("喜爱数")
    plt.title("美国评论数与喜爱数的关系")
    
    #设置网格 alpha 是清晰度
    # plt.grid(alpha = 0.3 , color = "#006400")
    
    #设置图例
    #两步 , 先在折线中设置标签 , 再用legend函数显示
    plt.legend(loc = 2)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    在这里插入图片描述

  • 相关阅读:
    【Java】Deque接口与List接口中的remove方法
    Go语言指针介绍
    webpack5基础--05_处理图片资源
    手机越用越慢?试试这4个秘籍,让手机流畅如新
    JSP文件上传
    Grafana+Prometheus+Pushgateway实现对emqx的监控
    轮询以及webSocket与socket.io原理
    网络学习:邻居发现协议NDP
    Spring学习第6篇: 基于注解使用IOC
    Visual Studio2019 与 MySQL连接 版本关系
  • 原文地址:https://blog.csdn.net/woshilichunyang/article/details/127135489