introduction to python for statistics,analysis笔记3

一、产生数组和矩阵

1、linspace(start,end,number),产生在start和end数之间number个数

>>> x = linspace(0, 10, 11)
>>> x
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])

2、logspace(start,end,number) 产生number个数,在10**start,10**end之间,相当于指数函数,在x轴平均分成number个数,求指数。

和10**linspace(start,end,number)效果一样

3、arange(l,u,s)

4、meshgrid()

>>> x = arange(5)
>>> y = arange(3)
>>> X,Y = meshgrid(x,y)
>>> X
array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
>>> Y
array([[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2]])

5、ix_(a,b)不规则选取元素,其中a,b可以是列表或元组

>>> x = reshape(arange(25.0),(5,5))
>>> x
array([[ 0., 1., 2., 3., 4.],
[ 5., 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14.],
[ 15., 16., 17., 18., 19.],
[ 20., 21., 22., 23., 24.]])
>>> x[ix_([2,3],[0,1,2])] # Rows 2 & 3, cols 0, 1 and 2
array([[ 10., 11., 12.],
[ 15., 16., 17.]])
>>> x[2:4,:3] # Same, standard slice
array([[ 10., 11., 12.],
[ 15., 16., 17.]])
>>> x[ix_([0,3],[0,1,4])] # No slice equiv

二、近似

1、around, round

x=np.random.randn(3)
print x
print np.around(x)
print np.around(x,2)#近似精度为2位小数
 
[ 0.23073931  1.08865135 -0.95564268]
[ 0.  1. -1.]
[ 0.23  1.09 -0.96]

2、floor(x)、ceil(x)、

三、统计特性

1/sum,计算和

a=np.reshape(np.arange(10),(2,5));
print a,'
'
print np.sum(a),'
'
print np.sum(a,0),'
'
print np.sum(a,1)
[[0 1 2 3 4]
 [5 6 7 8 9]] 

45 

[ 5  7  9 11 13] 

[10 35]

2/prod跟sum一样的特性,他是计算乘积的

a=np.reshape(np.arange(1,5),(2,2));
print a,'
'
​
print np.prod(a),'
'
print np.prod(a,0),'
'
print np.prod(a,1),'
'
[[1 2]
 [3 4]] 

24 

[3 8] 

[ 2 12] 

3、exp、log--相当于ln()、log10、sqrt、square、absolute, abs、sign都是对元素的操作

a=np.random.randn(2,3);
print a,'
'
print np.abs(a)
print np.sign(a)
[[
-0.35632202 -0.56913468 -0.5054189 ] [-0.13182024 1.62914028 1.57704769]] [[ 0.35632202 0.56913468 0.5054189 ] [ 0.13182024 1.62914028 1.57704769]] [[-1. -1. -1.] [-1. 1. 1.]]

4、对于复数的运算,下列运算也是元素的运算

  • real(A)或A.real,复数的实部
  • imag(A)或A.imag,复数的虚部
  • conj(A), conjugate,共轭复数

5、unique(A)是对所有元素操作,相当于python中的set(),去重效果

6、in1d(A,B)

>>> x = arange(10.0)
>>> y = arange(5.0,15.0)
>>> in1d(x,y)
array([False, False, False, False, False, True, True, True, True, True], dtype=bool)

7、union1d(A,B),returns the unique set of elements in 2 arrays.相当于集合并

8、intersect1d(A,B)相当于集合中的取交

9、setdiff1d(A,B),在集合A中,不在集合B中

10、setxor1d(A,B),相当于取集合异或,只在一个集合中的元素

11、sort

a=np.random.randn(2,3);
print a
print np.sort(a,1)
print np.sort(a,0)
print np.sort(a,None)

[[ 2.33262004 -2.17579511  1.02508041]
 [-0.11651321  1.02673882  1.25183328]]

[[-2.17579511  1.02508041  2.33262004]
 [-0.11651321  1.02673882  1.25183328]]

[[-0.11651321 -2.17579511  1.02508041]
 [ 2.33262004  1.02673882  1.25183328]]

[-2.17579511 -0.11651321  1.02508041  1.02673882  1.25183328  2.33262004]

注意:A.sort()和sort(A)之间的不同,一个会改变数据结构,一个不会。

>>> x = randn(3)
>>> x
array([ 2.70362768, -0.80380223, -0.10376901])
>>> sort(x)
array([-0.80380223, -0.10376901, 2.70362768])
>>> x
array([ 2.70362768, -0.80380223, -0.10376901])
>>> x.sort() # In-place, changes x
>>> x
array([-0.80380223, -0.10376901, 2.70362768])

12、max, amax, argmax, min, amin, argmin

max是数组的方法,amax是函数,argtmax返回

a=np.random.randn(3,5);
print a
print np.amax(a,1)
print np.amax(a,0)
print np.amax(a,None)

[[ -1.20363617e-01   6.09840964e-01  -2.42821192e-01  -1.87136859e+00
   -9.24036132e-01]
 [ -2.12137767e-04  -4.49847000e-01   6.05104140e-02   5.00253683e-01
    1.63359279e+00]
 [ -3.41458128e-01  -9.52592527e-01   8.66845911e-01  -1.26919405e+00
    1.67080515e+00]]

[ 0.60984096  1.63359279  1.67080515]

[ -2.12137767e-04   6.09840964e-01   8.66845911e-01   5.00253683e-01
   1.67080515e+00]

1.67080515388

13、minimum(A,B), maximum(A,B)比较两个数组,返回两个数组对应位置中最小的或最大的数

 
原文地址:https://www.cnblogs.com/zhaopengcheng/p/5399737.html