zoukankan      html  css  js  c++  java
  • [numpy] 基础练习 (一)

    Numpy常用总结

    基础要打牢,恩。

    基础

    # 0 - 9
    arr = np.arange(10)
    
    # 3*3 bool
    np.full((3,3),true,dtype = bool)
    np.ones((3,3),dtype = bool)
    
    # 奇数替换为-1
    arr[arr%2 ==1] = -1
    
    # 奇数替换为-1并且不改变原来数组
    np.where(arr%2==1,-1,arr)
    
    # 改变形状
    a = np.random.randint(0,100,(3,4))
    a.reshape((-1,6))
    
    # 堆叠 (3,3) (3,3) -> (2,3,3) axis 沿axis向前堆叠,即向前增加一个轴在增加的轴上进行堆叠
    a = np.ramdom.randint(0,100,(3,3))
    b = np.random.randint(0,100,(3,3))
    c = np.stack([a,b],axis = 0)
    
    # 沿已有的轴堆叠 c.shape = (6,3),矩阵形式下等于vstack,同样的也有htack
    a = np.ramdom.randint(0,100,(3,3))
    b = np.random.randint(0,100,(3,3))
    c = np.concatenate([a,b])
    
    # 获取两个数组的公共项,交集
    a = np.array([1,3,5,7,9])
    b = np.array([2,3,5,4,8])
    print(np.intersect1d(a,b))
    
    # 从a中删除与b的公共项
    a = np.array([1,3,5,7,9])
    b = np.array([2,3,5,4,8])
    print(np.setdiff1d(a,b))
    
    # 得到数组元素匹配的位置,np.where得到位置,a==b得到true or false
    a = np.array([1,3,5,7,9])
    b = np.array([2,3,5,4,8])
    print(np.where(a==b))
    
    # 得到一定范围内的数组
    a = np.array([1,3,5,7,9])
    print(a[np.where((a>0) & (a<5))])
    
    # 使得一般函数能够处理numpy对象,一定程度上我认为和map函数异曲同工
    a = np.array([1,2,3,4,5])
    b = np.array([5,4,3,2,1])
    def maxx(x,y):
        if x>y:
            return x
       	else:
            return y
    pair_max = np.vectorize(maxx,otypes = [float])
    print(pair_max(a,b))
    
    # 交换第一列和第二列
    a = np.arange(9).reshape((3,3))
    print(a[:,[1,0,2]])
    
    # 交换第一行和第二行
    a = np.arange(9).reshape((3,3))
    print(a[[1,0,2],:])
    
    # 反转二维数组的行
    a = np.arange(9).reshape((3,3))
    print(a[::-1,:])
    
    # 反转二维数组的列
    a = np.arange(9).reshape((3,3))
    print(a[:,::-1])
    
    # 创建5到10之间随机浮动的二维数组
    print(np.uniform(5,10,(5,3)))
    print(np.random.random())
    
    # 打印numpy数组的时候只保留小数点后三位
    random_arr = np.random.random((5,3))
    np.set_pointoptions(precision = 3)
    print(random_arr)
    
    # 设置科学计数法输出 suppress = False 则为科学计数法,True为小数输出
    random_arr = np.random.random((5,3))/1e3
    np.set_printoptions(supppress = False)
    
    # 设置打印的数目数,多余的省略号代替
    random_arr = np.arange(15)
    np.set_printoptions(threshold = 6) # threshold = np.nan则全部打印
    
    # 计算均值、中位数、标准差
    mu,med,sd = np.mean(x),np.median(x),mp.std(x)
    
    # 归一化到0和1之间
    smin,smax = x.min(),s.max()
    S = (x - smin)/(smax - smin)
    
    # 计算softmax的得分,可以计算多个类别的score,感觉可以记一记
    def softmax(x)
        e_x = np.exp(x - np.max(x))
        return e_x/e_x.sum(axis = 0)
    
    # 在2d数组20个随机位置插入1
    a = np.random.random((9,9))
    i,j = np.where(a)
    a[np.random.choice(i,20),np.random.choice(j,20)] # 两个random choice分别选择了 行和列
    
    # 多个条件过滤Numpy数组
    a = np.arange(50).reshape((-1,5))
    condition = (a>5) & (a<10)
    print(a[condition])
    
    
  • 相关阅读:
    一起谈.NET技术,ASP.NET应用下基于SessionState的“状态编程框架”解决方案 狼人:
    一起谈.NET技术,谈谈ASP.NET皮肤机制的实现 狼人:
    一起谈.NET技术,asp.net控件开发基础(9) 狼人:
    一起谈.NET技术,asp.net控件开发基础(11) 狼人:
    一起谈.NET技术,ASP.NET MVC 3 Beta初体验之实用的WebMail 狼人:
    一起谈.NET技术,关于Silverlight战略转移新闻,我的思考 狼人:
    一起谈.NET技术,ASP.NET MVC 3 Beta初体验之超酷的Chart:3D效果 狼人:
    一起谈.NET技术,asp.net控件开发基础(10) 狼人:
    一起谈.NET技术,asp.net控件开发基础(19) 狼人:
    一起谈.NET技术,asp.net控件开发基础(8) 狼人:
  • 原文地址:https://www.cnblogs.com/aoru45/p/11068720.html
Copyright © 2011-2022 走看看