当前位置:首页 > 科技 >

[Enthought Traning] Scientific Computing in Python

发布时间:2017-09-11 12:09:14

[Enthought Traning] Scientific Computing in Python Enthought Traning Scientific Computing

Scientific Computering

Data Interpolation Normal Interpolation # Data: x y from scipy.interpolate import interp1d # Basic Interpolation = interp1d(x,y) # With option bounds_error=False, interp1d won't raise an exception on extrapolation Interpolation = interp1d(x,y,bounds_error = False) # Fill nan with num when interpolate on extra Interval Interpolation = interp1d(x,y,bounds_error = False,fill_value=-100) # Kinds of Interpolation Interpolation = interp1d(x,y,kind="linear") # More Opitions : 'nearest','zero','quadratic','cubic',... #Other Interpolation from scipy.interpolate import interp2d, interpnd Radial basis function

Wikipedia

Radial basis functions are typically used to build up function approximations of the form
y(x)=∑i=1Nwi?(||x?xi||)

Approximation schemes of this kind have been particularly used in time series prediction and control of nonlinear systems exhibiting sufficiently simple chaotic behaviour, 3D reconstruction in computer graphics

Types

Gaussian

?(r)=e?(εr)2

Multiquadric:

?(r)=1+(εr)2 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄√

Inverse quadratic

?(r)=11+(εr)2

Inverse multiquadric:

?(r)=11+(εr)2 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄√

from scipy.interpolate.rbf import Rbf Interpolation = Rbf(x, y, function = "multiquadric") from numpy import (linspace, exp, sqrt, mgrid, pi, cos) from mpl_toolkits.mplot3d import Axes3D #3D data x, y = mgrid[-pi/2:pi/2:5j, -pi/2:pi/2:5j] z = cos(sqrt(x**2 + y**2)) #plot 3D scatter fig = figure(figsize=(12,6)) ax = fig.gca(projection="3d") ax.scatter(x,y,z) #interpolate zz = Rbf(x, y, z) #plot 3D surface xx, yy = mgrid[-pi/2:pi/2:50j, -pi/2:pi/2:50j] fig = figure(figsize=(12,6)) ax = fig.gca(projection="3d") ax.plot_surface(xx,yy,zz(xx,yy),rstride=1, cstride=1, cmap=cm.jet) Statistics in Python spicy.stats.stats import scipy.stats.stats as st Probability Distribution

Continuous Distribution Methods

pdf/cdf/rvs/ppf/stats/fit/sf/isf/entropy/…

Distrete Distribution Methods

pmf/cdf/rvs/ppf/stats/sf/isf/…

Continuous Distribution from scipy.stats import norm from scipy stats import t # t distribution # generate random variables x_norm = norm.rvs(size=500) # fit a specific distribution x_mean.x_std = norm.fit(x_norm) # Draw distribution x=linspace(-3,3,50) h = plt.hist(x_norm,normed = Ture ,bins =20) p = plt.plot(x,norm.pdf(x),'r-') #calculate probability based on pdf in certain range and then draw the picture from scipy.stats import norm from scipy.integrate import trapz import numpy as np import matplotlib.pyplot as plt x1 = np.linspace(-2,2,100) p = trapz(norm.pdf(x1),x1) print '{:.2%}'.format(p) fb = plt.fill_between(x1,norm.pdf(x1),color = 'gray') x=np.linspace(-3,3,50) p = plt.plot(x,norm.pdf(x),'k-')

\

More about distribution

loc , scale

P = plt.plot(x,norm.pdf(x,loc = 0.5,scale = 2),label = 's=2') legend() Discrete Distribution # Binom / Poisson / uniform distribution from scipy.stats import binom,poisson,randint # Binomial Distribution num_trials = 50 x = arange(num_trials) p = plot(x, binom(num_trials, 0.5).pmf(x), "o-", label="p=0.5") p = plot(x, binom(num_trials, 0.2).pmf(x), "o-", label="p=0.2") l = legend(loc="upper right") t = title("Binomial distribution with 50 trials")

\

x = arange(0,21) p = plot(x, poisson(1).pmf(x), 'o-', label=r"$\lambda$=1") p = plot(x, poisson(4).pmf(x), 'o-', label=r"$\lambda$=4") p = plot(x, poisson(9).pmf(x), 'o-', label=r"$\lambda$=9") l = legend() t = title("Poisson 'number of occurrences' distribution")

\

Our Own Discrete Distribution

Can be used in sampling

from scipy.stats import rv_discrete # Dice Example xk = [1, 2, 3, 4, 5, 6] pk = [0.3, 0.35, 0.25, 0.05, 0.025, 0.025] loaded = rv_discrete(values=(xk, pk)) samples = loaded.rvs(size=100) Hypothesis Testing from scipy.stats import norm from scipy.stats import ttest_ind, ttest_rel, ttest_1samp #[ttest_ind]Calculates the T-test for the means of TWO INDEPENDENT samples of scores #[ttest_rel]Calculates the T-test on TWO RELATED samples of scores, before and after #[ttest_1samp]Calculates the T-test for the mean of ONE group of scores from scipy.stats import t n1 = norm(loc=0.5, scale=1.0) n2 = norm(loc=0, scale=1.0) n1_samples = n1.rvs(size=100) n2_samples = n2.rvs(size=100) # T test t_val, p_val = ttest_ind(n1_samples, n2_samples) #t_val = 1.05831471112 #p_val = 0.291201401071 Curve Fitting

Random Generate

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:黄石SEO http://huangshi.4567w.com