こんなのはどうですか?

リンク

相互リンク募集中!!
数値計算関連

配列を操作する

from numpy import * # 配列の作成 a = array([1,2,3]) b = array((10,11,12) # 配列の加算 結果:array([11,13,15]) a + b # 配列のデータ型を確認 結果:dtype('<i4') a.dtype # 配列の割り算 結果:array([0,0,1]) a/3 # データ型を指定して、配列の作成 a = array([1,2,3], dtype=float) # 配列版range関数 ,arangeを使用して、配列作成 data = array([0.5, 1.2, 2.2, 3.4, 3.5, 3.4, 3.4, 3.4], float) t = arange(len(data), dtype='float') * 2*pi/(len(data)-1) # すべての配列を出力する。 t[:] # 結果: array([ 0. , 0.8975979 , 1.7951958 , 2.6927937 , # 3.5903916 , 4.48798951, 5.38558741, 6.28318531]) # 配列を部分的に出力する。 t[2:4] # 結果: array([ 1.7951958, 2.6927937]) # t[2:4]と同じ t[slice(2,4)] # 結果: array([ 1.7951958, 2.6927937]) # 等間隔に配列を抽出する t[0:6:2] # 結果: array([ 0. , 1.7951958, 3.5903916]) # 真偽値で配列を抽出する i=array(len(t)*[False],bool) i[2]=True;i[4]=True;i[6]=True t[i] # 結果: array([ 1.7951958 , 3.5903916 , 5.38558741])

配列の積を求める

from numpy import * a = array([[1,2],[2,3]]) b = array([[7,1],[0,1]]) dot(a,b)

配列を作成する

# 連続データから、配列を作る x = linspace(0, 2*pi, 100) y = sin(x) # 等間隔のデータを作る x,y = mgrid[0:10:.1, 0:10:.2] # 配列の演算により配列を作る z = (x+y)**2 # ゼロ行列から、配列をつくる mz = zeros((2,2), dtype=float) mz[0,0] = .5**2 mz[1,1] = 1.6**2

回帰分析をする

from scipy import linspace, polyval, polyfit, sqrt, stats, randn from pylab import plot, title, show , legend #Linear regression example # This is a very simple example of using two scipy tools # for linear regression, polyfit and stats.linregress #Sample data creation #number of points n=50 t=linspace(-5,5,n) #parameters a=0.8; b=-4 x=polyval([a,b],t) #add some noise xn=x+randn(n) #Linear regressison -polyfit - polyfit can be used other orders polys (ar,br)=polyfit(t,xn,1) xr=polyval([ar,br],t) #compute the mean square error err=sqrt(sum((xr-xn)**2)/n) print('Linear regression using polyfit') print('parameters: a=%.2f b=%.2f \n regression: a=%.2f b=%.2f, ms error= %.3f' % (a,b,ar,br,err)) #matplotlib ploting title('Linear Regression Example') plot(t,x,'g.--') plot(t,xn,'k.') plot(t,xr,'r.-') legend(['original','plus noise', 'regression']) show() #Linear regression using stats.linregress (a_s,b_s,r,tt,stderr)=stats.linregress(t,xn) print('Linear regression using stats.linregress') print('parameters: a=%.2f b=%.2f \nregression: a=%.2f b=%.2f, std error= %.3f' % (a,b,a_s,b_s,stderr))

フーリエ変換をする

from scipy import * from pylab import * sample_rate=1000.00 t=r_[0:0.6:1/sample_rate] N=len(t) s=sin(2*pi*50*t)+sin(2*pi*70*t+pi/4) S=fft(s) f=sample_rate*r_[0:(N/2)]/N n=len(f) plot(f,abs(S[0:n])/N) show()

スプライン補間をする

import numpy import scipy import scipy.interpolate x = numpy.arange(10,dtype='float32') * 0.3 y = numpy.cos(x) sp = scipy.interpolate.UnivariateSpline(x,y) sp(0.5) # cos(0.5)=0.8775に近いほど、よい補間といえる

スプライン補間をする2

import numpy import scipy import scipy.interpolate x = numpy.arange(10,dtype='float32') * 0.3 y = numpy.cos(x) rep = scipy.interpolate.splrep(x,y) scipy.interpolate.splev(0.5,rep) # cos(0.5)=0.8775に近いほど、よい補間といえる。

N次曲線でスプライン補間をする

from numpy import arange, cos, linspace, pi, sin, random from scipy.interpolate import splprep, splev # データ作成 t=linspace(0,1.75*2*pi,100) x = sin(t) y = cos(t) z = t # add noise x+= random.normal(scale=0.1, size=x.shape) y+= random.normal(scale=0.1, size=y.shape) z+= random.normal(scale=0.1, size=z.shape) # スプラインパラメータ s=3.0 # smoothness parameter k=2 # spline order nest=-1 # estimate of number of knots needed (-1 = maximal) # find the knot points tckp,u = splprep([x,y,z],s=s,k=k,nest=-1) # スプライン補間をする xnew,ynew,znew = splev(linspace(0,1,400),tckp) # グラフを描画 import pylab pylab.subplot(2,2,1) data,=pylab.plot(x,y,'bo-',label='data') fit,=pylab.plot(xnew,ynew,'r-',label='fit') pylab.legend() pylab.xlabel('x') pylab.ylabel('y') pylab.subplot(2,2,2) data,=pylab.plot(x,z,'bo-',label='data') fit,=pylab.plot(xnew,znew,'r-',label='fit') pylab.legend() pylab.xlabel('x') pylab.ylabel('z') pylab.subplot(2,2,3) data,=pylab.plot(y,z,'bo-',label='data') fit,=pylab.plot(ynew,znew,'r-',label='fit') pylab.legend() pylab.xlabel('y') pylab.ylabel('z') pylab.show() #pylab.savefig('splprep_demo.png')

積分をする

from scipy import * value, err = integrate.quad(func=pow, a=0., b=1., args=(5,)) print value # x^5,区間[0,1]の積分結果
inserted by FC2 system