Numpy常用函数sum, shape, reshape, argsort, tile,where
来源:互联网 发布:淘宝什么是转化率 编辑:程序博客网 时间:2024/05/16 06:24
- sum函数
- shape函数
- reshape函数
- argsort函数
- tile函数
- where函数
转载请附上:原文链接
sum函数
import numpy as np #导入numpy模块,将np作为其别名a=np.array([[0,1,2],[2,1,3]])np.sum(a) #将矩阵的所有元素相加,结果为:9np.sum(a,axis=0) #将矩阵a的每一列相加,结果为:array([2,2,5])np.sum(a,axis=1) #将矩阵a的每一行相加,结果为:array([3,6])
shape函数
import numpy as npa = np.array([[1,2,3],[4,5,6]])np.shape(a) #求矩阵a的各维度值:行数和列数,结果为(2,3)a.shape #功能同上,结果为(2,3)a.shape[0] #返回行数,结果为2a.shape[1] #返回列数,结果为3
reshape函数
a = np.array([1,2,3,4])b = a.reshape((2,2)) #将a重排列成2行2列,结果b=array([[1,2],[3,4]])b = a.reshape((1,2)) #错误,a的reshape必须与a原本大小一致b = a.reshape((4,-1)) #4表示4行,-1表示Python根据a原来的大小以及b给定的4行,自己计算b的列数,等价于b=a.reshape((4,1)),运行结果b=array([[1],[2],[3],[4]])
argsort函数
x = np.array([7,3,5])np.argsort(x) #按照数组值升序排列,返回其下标索引,结果为array([1,2,0])np.argsort(-x) #按照数组值降序排列,返回其下标索引,结果为array([0,2,1])x[np.argsort(x)] #按照升序返回数组值,结果为array([3,5,7])x[np.argsort(x)] #按照降序返回数组值,结果为array([7,5,3])
tile函数
numpy.tile(A, reps)
将A重复reps次。
import numpy as npa = np.array([[1,2],[3,4]])np.tile(a, 2) #当重复reps的纬度小于矩阵的纬度时,往前补1,此句等价于np.tile(a,(1,2))##结果为array([[1,2,1,2], [3,4,3,4]])np.tile(a,(2,1)) #结果为array([[1,2], [3,4], [1,2], [3,4]])
where函数
np.where的作用是,根据条件生成新的数组 result=np.where(condition, x, y)
np.where的作用等同于if(condition) x else y
,当符合条件时,result=x
,不符合材料时,result=y
例子,将arr=np.array([[2,-1],[-3,4]])
中所有正数替换为1,负数替换为0
arr=np.where(arr>0, 1, 0)
运行结果:arr=array([[1,0],[0,1]])
将np.where应用在数据处理中的例子:
data=np.array([[3,7,0],[3,4,0],[6,8,1],[7,7,1]])x = data[:,0:2] #所有行,第0和1列y = data[:,2] #所有行,第2列pos = np.where(y==1) #记录标号y为1的行的行号,结果pos=array([2,3])neg = np.where(y==0) #记录标号y为0的行的行号,结果neg=array([0,1])
阅读全文
0 0
- Numpy常用函数sum, shape, reshape, argsort, tile,where
- numpy的argsort和sum和tile函数
- python的tile、shape sum、argsort
- python的numpy包中shape,tile,argsort函数与sorted函数使用方法
- python numpy.shape 和 numpy.reshape函数
- python numpy.shape 和 numpy.reshape函数
- python numpy.shape 和 numpy.reshape函数
- python numpy.shape 和 numpy.reshape函数
- python之tile函数,shape函数,argsort函数介绍
- numpy常用函数(power、sum、tile、transpose等)
- numpy常用函数(power、sum、tile、transpose等)
- numpy argsort排序函数
- NumPy 中argsort函数
- Numpy-argsort函数
- numpy的常用函数reshape、matmul
- python 中的tile函数,shape函数,sum函数
- numpy函数:tile函数
- numpy中argsort函数用法
- jquery实现记住密码功能
- 如何优雅的编写Python并发程序(Gevent)
- Object类中方法总结之toString()
- JVM内存设置 Tomcat JVM Eclipse
- oracle 11g、11gr2不能导出空表的多种解决方法
- Numpy常用函数sum, shape, reshape, argsort, tile,where
- linux环境安装jdk ,tomcat
- Scala-Actor并行wordcount
- 屏幕高度的获取以及高度的自适应问题
- FreeMarker 生成合同
- K`th number
- framework dyld: Symbol not found: _OBJC_CLASS_xxx
- Java集合及concurrent并发包总结
- Javascript中的按值传递与引用传递