2017-09-14 95 views
1

爲高斯函數計算一個函數高斯(x,m = 0,s = 1)。 在[m-5s,m + 5s]中寫出n個均勻間隔x值的x和f(x)值的格式良好的表格。 (選擇S,M,N,只要你喜歡)初學者python,簡單函數

這裏就是我所做的迄今:

from math import * 
from numpy import linspace 

def gaussian(x, m=0, s=1): 
    fx=(1/((sqrt(2*pi))*s)*exp(-0.5*(((x - m)/s))**2)) 
    return fx 

我想使變量S和M全球,

xmin=m-5s 
xmax=m+5s 
x=linspace(xmin, xmax, 10) 

然後調用函數從一個for循環,我循環過去x

我的第一次嘗試是嘗試在函數內的所有內容,但我的導師說在函數外定義x更好。如果s和m只存在於函數內部,我該如何觸及它們 - 或者我應該以另一種方式來解決這個問題? 任何幫助都是有好處的,請記住,我幾個星期才學會了這一點。

回答

0

如何:

from numpy import linspace 
from math import * 

def gaussian(x, m, s): 
    fx=(1/((sqrt(2*pi))*s)*exp(-0.5*(((x - m)/s))**2)) 
    return fx 

m = 0 
s = 1 
xmin = m-5*s 
xmax = m+5*s 
x = linspace(xmin, xmax, 10) 

for val in x: 
    print "f(",val,") = ",gaussian(val,m,s) 
+0

是的,它的作品,但它不是作弊嗎?哈哈 –

+0

爲什麼作弊?我不明白你的意思。你有一個計算高斯概率的函數,並且如你所提到的那樣使用它作爲X值!如果這樣做,你介意接受這個作爲你的答案嗎? – xrr

0

矢量化這與NumPy的,以避免需要循環:

import numpy as np 
def gaussian(x, m, s): 
    fx = (1/((np.sqrt(2*np.pi))*s)*np.exp(-0.5*(((x - m)/s))**2)) 
    return fx 

m=0; s=1  
x = np.linspace(m-5*s, m+5*s, num=100) 

print(gaussian(x)) 
[ 1.48671951e-06 2.45106104e-06 3.99989037e-06 6.46116639e-06 
    1.03310066e-05 1.63509589e-05 2.56160812e-05 3.97238224e-05 
    6.09759040e-05 9.26476353e-05 1.39341123e-04 2.07440309e-04 
    3.05686225e-04 4.45889725e-04 6.43795498e-04 9.20104770e-04 
    1.30165384e-03 1.82273110e-03 2.52649578e-03 3.46643792e-03 
    4.70779076e-03 6.32877643e-03 8.42153448e-03 1.10925548e-02 
    1.44624148e-02 1.86646099e-02 2.38432745e-02 3.01496139e-02 
    3.77369231e-02 4.67541424e-02 5.73380051e-02 6.96039584e-02 
    8.36361772e-02 9.94771388e-02 1.17117360e-01 1.36486009e-01 
    1.57443188e-01 1.79774665e-01 2.03189836e-01 2.27323506e-01 
    2.51741947e-01 2.75953371e-01 2.99422683e-01 3.21590023e-01 
    3.41892294e-01 3.59786558e-01 3.74773979e-01 3.86422853e-01 
    3.94389234e-01 3.98433802e-01 3.98433802e-01 3.94389234e-01 
    3.86422853e-01 3.74773979e-01 3.59786558e-01 3.41892294e-01 
    3.21590023e-01 2.99422683e-01 2.75953371e-01 2.51741947e-01 
    2.27323506e-01 2.03189836e-01 1.79774665e-01 1.57443188e-01 
    1.36486009e-01 1.17117360e-01 9.94771388e-02 8.36361772e-02 
    6.96039584e-02 5.73380051e-02 4.67541424e-02 3.77369231e-02 
    3.01496139e-02 2.38432745e-02 1.86646099e-02 1.44624148e-02 
    1.10925548e-02 8.42153448e-03 6.32877643e-03 4.70779076e-03 
    3.46643792e-03 2.52649578e-03 1.82273110e-03 1.30165384e-03 
    9.20104770e-04 6.43795498e-04 4.45889725e-04 3.05686225e-04 
    2.07440309e-04 1.39341123e-04 9.26476353e-05 6.09759040e-05 
    3.97238224e-05 2.56160812e-05 1.63509589e-05 1.03310066e-05 
    6.46116639e-06 3.99989037e-06 2.45106104e-06 1.48671951e-06] 

對於表:

import pandas as pd 
pd.DataFrame({'x' : x, 'gauss' : gaussian(x)}) 

至於您的評論:

我的導師說在函數外面定義x更好。如果s 和m只存在於函數內部,我怎麼能達到它們 - 或者 我應該以另一種方式來解決這個問題嗎?

這主要取決於你是否想x是的ms功能。如果總是這樣,那麼它是x,你應該納入您的函數(函數體中定義本地x):

def gaussian(m, s, num): 
    x = np.linspace(m-5*s, m+5*s, num=num) 
    fx = (1/((np.sqrt(2*np.pi))*s)*np.exp(-0.5*(((x - m)/s))**2)) 
    return fx 

無論哪種方式,有沒有需要處理global這裏,這就是你應該可能會避免,除非你有一個很好的理由。

在我上面第一次定義的gaussian中設置事物的方式,您將x,ms作爲自變量。也就是說,您可以指定一些不依賴於ms的其他x。如果您希望x始終是ms的函數,那麼將其直接合併到您的函數中,以避免必須在函數之外指定它。

0

我要去嘗試和複製我認爲你要去的東西。 Python確實有global關鍵字,但我不確定這是您需要的(here's是您需要全局變量時的一個很好的解釋)。

from numpy import linspace, sqrt, pi, exp 

def gaussian(x, m=0, s=1): 
    fx=(1/((sqrt(2*pi))*s)*exp(-0.5*(((x - m)/s))**2)) 
    return fx 

#we must define m and s in order to calculate our bounds for linspace 
m = 5 
s = 1 

#you must have a '*' with multiplication. 5s is not equivelent to 5*s 
xmin = m - 5 * s 
xmax = m + 5 * s 

x = linspace(xmin, xmax, 10) 

#apply your gaussian to every value in x 
fx = [gaussian(a, m=m, s=s) for a in x] 

print(fx)