安装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)
Generate arithmetic sequence
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's ufunc operation
ufunc calculation speed is very fast, it is implemented by calling c language to generate
an array first
>>> x = np.arange(1,10,1)
>>> x
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Calculate the sin value of the array
>>> y = np.sin( x)
>>> y
array([ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427,
-0.2794155 , 0.6569866 , 0.98935825, 0.41211849])
It takes time to calculate sin (time import from python) >>> from python package >>>
from 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
use np to calculate the value of 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
Two arrays can add, subtract, multiply and divide power
>>> x = np.array( [1,2,3])
>>> y = np.array([3,2,4])
>>> x+y
array([4, 4, 7])
>>> xy
array([-2 , 0, -1])
>>> x*y
array([ 3, 4, 12])
>>> y/x
array([3, 1, 1])
>>> x**y
array([ 1 , 4, 81])
draw a waveform by calculation
>>> 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 matrix operations
python scientific computing numpy
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=327039687&siteId=291194637
Recommended
Ranking