首页 > 代码库 > python科学计算之numpy

python科学计算之numpy

1.np.logspace(start,stop,num): 函数表示的意思是;在(start,stop)间生成等比数列num个

eg: 

import numpy as npprint np.logspace(1,4,4)

结果为: [    10.    100.   1000.  10000.]

2. np.fromstring(‘admin‘,dtype=np.int8):函数的作用是将字符串装换成对应的ascii值

import numpy as npprint np.fromstring(admin,dtype= np.int8)

结果为: [ 97 100 109 105 110]

3.自定义自己的数据类型:

import numpy as np

student = np.dtype({‘names‘: [‘name‘, ‘age‘], ‘formats‘: [‘S32‘, ‘i‘]})
print student
xiaoming = np.array([(‘gong‘, 12)], dtype=student)
print xiaoming
print xiaoming[0][‘name‘]
print xiaoming[0][‘age‘]

结果:

[(‘name‘, ‘S32‘), (‘age‘, ‘<i4‘)]
[(‘gong‘, 12)]

gong

12

4.以等差的形式生成一维数组:

import numpy as npprint np.linspace(0,4,6)

结果:[ 0.   0.8  1.6  2.4  3.2  4. ]

5.使用frompyfun进行加速科学计算

import numpy as npdef func(a, b):    return a + b;x = np.linspace(1, 4, 6)fx = np.frompyfunc(lambda x: func(x, 0.6), 1, 1)print  fx(x)

结果:

[1.6 2.2 2.8000000000000003 3.4 4.0 4.6]

6. np.dot([1,2],[2,3])为矩阵的内积(矩阵相乘)计算

结果:8

7.np.inner(a,b)为列向量之和

8.np.outer(a,b)为行向量对应相乘.

python科学计算之numpy