kobeyan 2016-09-23
安装python(x,y),通过google下载python (x,y)
是exe 安装文件,只能安装到windows上
numpy之ndarray对象
>>> import numpy as np
>>> array([1,2,3,4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'array' is not defined
>>> b=np.array((1,2,3,4,))
>>> d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'd' is not defined
>>> b
array([1, 2, 3, 4])
>>> c=np.array([1,2,3],[2,3,4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: data type not understood
>>> c = np.array([[1,2,3],[2,3,4]])
>>> c
array([[1, 2, 3],
[2, 3, 4]])
查看数组类型
>>> c.dtype
dtype('int32')
>>> c.shape
(2, 3)
>>> c.shape = 2 ,-1
>>> c
array([[1, 2, 3],
[2, 3, 4]])
>>> c.shape = -1,2
>>> c
array([[1, 2],
[3, 2],
[3, 4]])
>>> n = c.reshape((2,3))
>>> n
array([[1, 2, 3],
[2, 3, 4]])
>>> n[0][0]= 10
>>> n
array([[10, 2, 3],
[ 2, 3, 4]])
>>> c
array([[10, 2],
[ 3, 2],
[ 3, 4]])
array([1, 2, 3])
生成等差数列
>>> np.logspace(1,10,3)
array([ 1.00000000e+01, 3.16227766e+05, 1.00000000e+10])
>>> s = "hello"
>>> np.fromstring(s,dtype=np.int8)
array([104, 101, 108, 108, 111], dtype=int8)
结构数组
AttributeError: 'module' object has no attribute 'S32'
>>> person = np.dtype([('names', [('name', np.S25)])])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'S25'
>>> np.dtype({'surname':('S25',0),'age':(np.uint8,25)})
dtype([('surname', 'S25'), ('age', 'u1')])
>>> np.dtype({'surname':('S25',0),'age':(np.uint8,25)})
dtype([('surname', 'S25'), ('age', 'u1')])
>>> np.dtype({'names':['gender','age'], 'formats':['S1',np.uint8]})
dtype([('gender', 'S1'), ('age', 'u1')])
numpy 之ufunc 运算
ufunc计算速度非常快,都是调用c语言实现的
先生成一个数组
>>> x = np.arange(1,10,1)
>>> x
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
计算数组的sin值
>>> y = np.sin(x)
>>> y
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427,
-0.2794155 , 0.6569866 , 0.98935825, 0.41211849])
用普通python包计算sin(x)需要时间
>>> from time import time
>>> x = [i*0.001 for i in xrange(10000)]
>>> start = time()
>>> import math
>>> for i ,t in enumerate(x):
... x[i]=math.sin(t)
...
>>> print time() -start
60.1679999828
用np来计算sin的值
>>> x = [i*0.001 for i in xrange(10000)]
>>> x = np.array(x)
>>> start = time()
>>> np.sin(x,x)
array([ 0. , 0.001 , 0.002 , ..., -0.49290313,
-0.49352947, -0.49415484])
>>> print time()-start
7.43000006676
两个数组可以加减乘除次方运算
>>> x = np.array([1,2,3])
>>> y = np.array([3,2,4])
>>> x+y
array([4, 4, 7])
>>> x-y
array([-2, 0, -1])
>>> x*y
array([ 3, 4, 12])
>>> y/x
array([3, 1, 1])
>>> x**y
array([ 1, 4, 81])
通过计算绘制一个波形图
>>> import numpy as np
>>> def func(x,c,c0,hc):
... x = x - int(x)
... if x >= c: r = 0.0
... elif x < c0: r = x/c0*hc
... else:
... r = ((c-x)/(c-c0))*hc
... return r
...
>>> print func(1,0.6,0.4,1.0)
0.0
>>> print func(0.2,0.6,0.4,1.0)
0.5
>>> print func(0.4,0.6,0.4,1.0)
1.0
>>> x = np.linspace(0,2,100)
>>> y = np.array([func(t,0.6,0.4,1.0) for t in x])
>>> print y
[ 0. 0.05050505 0.1010101 0.15151515 0.2020202 0.25252525
0.3030303 0.35353535 0.4040404 0.45454545 0.50505051 0.55555556
0.60606061 0.65656566 0.70707071 0.75757576 0.80808081 0.85858586
0.90909091 0.95959596 0.97979798 0.87878788 0.77777778 0.67676768
0.57575758 0.47474747 0.37373737 0.27272727 0.17171717 0.07070707
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.02525253 0.07575758 0.12626263 0.17676768 0.22727273 0.27777778
0.32828283 0.37878788 0.42929293 0.47979798 0.53030303 0.58080808
0.63131313 0.68181818 0.73232323 0.78282828 0.83333333 0.88383838
0.93434343 0.98484848 0.92929293 0.82828283 0.72727273 0.62626263
0.52525253 0.42424242 0.32323232 0.22222222 0.12121212 0.02020202
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. ]
#frompyfunc 矩阵预算
>>> import numpy as np
>>> def func(c,c0,hc):
... def trifunc(x):
... x = x - int(x)
... if x >= c:
... r = 0.0
... elif x < c0:
... r = x / c0 * hc
... else:
... r = ((c - x) /(c - c0)) * hc
... return r
... return np.frompyfunc(trifunc , 1, 1)
...
>>> x = np.linspace(0,2,100)
>>> y = func(0.6,0.4,1.0)(x)
>>> print y.astype(np.float64)
[ 0. 0.05050505 0.1010101 0.15151515 0.2020202 0.25252525
0.3030303 0.35353535 0.4040404 0.45454545 0.50505051 0.55555556
0.60606061 0.65656566 0.70707071 0.75757576 0.80808081 0.85858586
0.90909091 0.95959596 0.97979798 0.87878788 0.77777778 0.67676768
0.57575758 0.47474747 0.37373737 0.27272727 0.17171717 0.07070707
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.02525253 0.07575758 0.12626263 0.17676768 0.22727273 0.27777778
0.32828283 0.37878788 0.42929293 0.47979798 0.53030303 0.58080808
0.63131313 0.68181818 0.73232323 0.78282828 0.83333333 0.88383838
0.93434343 0.98484848 0.92929293 0.82828283 0.72727273 0.62626263
0.52525253 0.42424242 0.32323232 0.22222222 0.12121212 0.02020202
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. ]
numpy之矩阵运算