zoukankan      html  css  js  c++  java
  • Numpy学习

     # 使用numpy生成数组,得到ndarray类型
        t1 = np.array([1, 2, 3, ])
        print(t1)
        print(type(t1))
        t2 = np.arange(4, 12, 2)  # 2步长
        print(t2)
        t3 = np.array(range(10))
        print(t3)
    
        t3.dtype  # t3中元素的数据类型
        t4 = np.array(range(1, 4), dtype="int8")  # 指定数据类型
        t4.astype(float)#修改数据类型
        t5 = np.array([random.random() for i in range(10)])  # 取小数
        t6 = np.round(t5, 2)  # 小数保留两位
        print(t2.shape)  # 形状(行数,列数)
        t5.reshape((2, 5))  # 改变形状
        t7 = np.arange(24).reshape((2, 3, 4))
        print(t7)
        t7.reshape((24,))
        # 下面俩都是二维的
        t7.reshape((1, 24))
        t7.reshape((24, 1))
        t7 = t7.reshape((4, 6))
        print(t7.shape[0], t7.shape[1])  # 行与列
        t7.flatten()  # 展开成一维度
        t7 = t7 + 2  # 广播机制,都加2
        t8 = np.arange(100, 124).reshape((4, 6))
        print(t7 + t8)  # 对应值相加
        t9 = np.arange(6)
        print(t7 - t9)  # 相同维度操作
        # 行列都不一样将无法计算
        # 在形状上包含就可运算
    uk_file_path="./test_data/GB_video_data_numbers.csv"
        us_file_path="./test_data/US_video_data_numbers.csv"
        t1=np.loadtxt(us_file_path,delimiter=",",dtype="int",skiprows=0,unpack=False)#skiprows,跳过前n行 unpack转置
        print(t1)
        print("*"*100)
        t1[2]#取行
        t1[2:]#取多行,连续
        t1[[2,8,10]]#取多行,不连续
        t1[1,:]#[行,列]取第二行,:代表都要
        t1[:,0]#取第一例
        t1[2,3]#取第3行第4列
        t1_flag=t1[2:5,1:4]#取多行多列
        print(t1_flag)
        t1[[0,1,2],[1,2,2]]#取多个不相邻的点,[0,1] ,[1,2],[2,2]
        t1[t1<1100]=3#将小于1100的替换为3
        np.where(t1<10,0,10)#t1<10赋值为0,否则赋值为10,三元运算符
        t1.clip(1000,20000)#将小于1000的替换为1000,将大于20000的替换为20000
        np.nan==np.nan#两个nan不一定相等,not a number
        np.count_nonzero(t1!=t1)#统计nan的个数
    #nan加任何数都是nan
    np.sum(t1_flag)#将所有数相加
    t2=np.arange(12).reshape((3,4))
    print(t2)
    #注意对“轴”的理解
    print(np.sum(t2,axis=0))#行相加
    print(np.sum(t2,axis=1))#列相加
    #numpy常用函数
    t2.sum()
    print(t2.mean(axis=0))#均值
    print(np.median(t2,axis=1))#中值
    #最大最小值
    t2.max()
    t2.min()
    np.ptp(t2)#极值
    print(t2.std())#标准差
     
  • 相关阅读:
    react native 之页面跳转
    react native 之异步请求
    react native 之 redux
    react native 之页面布局
    ES6的相关新属性
    css中的选择器
    js中return的作用
    校园商铺-7商品类别模块-2商品类别列表从后到前
    校园商铺-6店铺编辑列表和列表功能-9店铺管理页面的前端开发
    校园商铺-6店铺编辑列表和列表功能-8店铺列表展示前端开发
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/15559597.html
Copyright © 2011-2022 走看看