One - One Code All

Blog Content

numpy常用函数np.take

Python 统计学-科学计算   2014-03-02 20:00:03

np.take(a,b): 根据b中元素作为索引,查找a中对应元素

take函数按照索引值从数组中取出相应的元素,如:np.take(c,np.where(c>6))

官网参考:

https://docs.scipy.org/doc/numpy/reference/generated/numpy.take.html

https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.take.html#numpy.ndarray.take


源码参考:https://github.com/numpy/numpy/blob/master/numpy/core/fromnumeric.py


数据分析常用的函数

#加权平均,v作为权重参数

np.average(c, weights=v)


#算术平均

np.mean(c)


#取值范围

np.max(c)

np.min(c)


#ptp函数返回最大值与最小值之间的差值

np.ptp(c)


#median函数返回中位数

np.median(c)


#var方差

np.var(c)


#diff函数返回一个由相邻数组元素的差值构成的数组

np.diff(c)


#std函数计算标准差

np.std(c)


#where函数根据指定的条件返回所有满足条件的数组元素的索引值

idx = np.where(c>6)

# take函数按照索引值从数组中取出相应的元素,np.take(c,np.where(c>6))

data = np.take(idx)


#argmin返回是c数组中最小元素的索引值,argmax返回最大值索引值

np.argmin(c)

np.argmax(c)


#maximum函数可返回多个数组里的各最大值

np.maximum(a, b, c)

#同理minximum返回最小值


#exp函数可计算每个数组元素的指数

np.exp(x)


#linspace(s, e, [n])函数起始s, 终止e,个数n(可选),返回一个元素值在指定的范围内均匀分布的数组

np.linspace(-1, 0, 5)


#fill函数,将数组元素的值全部设置为一个指定的标量值


中所有元素的乘积

b = np.arange(1, 9)

print("b =", b)

print("Factorial", b.prod())

#b = [1 2 3 4 5 6 7 8]

#Factorial 40320


#cumprod方法,计算数组元素的累积乘积

b.cumprod()

#[ 1 2 6 24 120 720 5040 40320]



相关性

#协方差,描述两个变量共同变化趋势

c = np.cov(a,b)


#diagonal返回对角线上的元素

c.diagonal()


#trace计算矩阵的迹,即对角线上元素之和

c.trace()


#corrcoef函数计算相关系数(或者更精确地,相关系数矩阵)

np.corrcoef(a, b)



上一篇:UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 11126: illegal multibyte sequence
下一篇:np.in1d在一个与某个列表匹配的numpy二维数组中查找所有元素

The minute you think of giving up, think of the reason why you held on so long.