2010-12-02 12 views
2

私はan earlier version of this yesterdayを投稿しましたが、誰かがその投稿を編集して閉じたように見えるので、このバージョンをその投稿に追加できないようです。もう一つのS字状回帰方程式の質問

私は以下のことを行う以下のスクリプトを持っています:
1.)S字状のデータに最もよくフィットする曲線をプロットします。
2.)xとyの新しい最大値と最小値に基づいてデータのサイズを変更します。
.3.)サイズ変更されたデータの新しい最適曲線を計算してプロットします。

手順1と2はうまくいくようですが、手順3はうまくいきません。スクリプトを実行すると、サイズ変更されたデータに対して完全に無効な曲線がプロットされます。

サイズ変更後のデータに対して真の最良適合シグモイド曲線を作成してプロットするために、以下のコードを修正する方法を誰にも教えていただけますか?可能な最大値と最小値のスペクトルにわたってサイズを変更すると、再現性がある必要があります。

は、私は次のコード行で定義されているNew_pに問題を追跡することができるように見える:

New_p, New_cov, New_infodict, New_mesg, New_ier = scipy.optimize.leastsq( 
    residuals,New_p_guess,args=(NewX,NewY),full_output=1,warning=True) 

しかし、私はそれよりも問題に深く取得する方法を見つけ出すことはできません。問題はグローバル変数とローカル変数の違いと関係しているかもしれませんが、おそらくそれは別のものです。

import numpy as np 
import matplotlib.pyplot as plt 
import scipy.optimize 

def GetMinRR(age): 
    MaxHR = 208-(0.7*age) 
    MinRR = (60/MaxHR)*1000 
    return MinRR 

def sigmoid(p,x): 
    x0,y0,c,k=p 
    y = c/(1 + np.exp(-k*(x-x0))) + y0 
    return y 

def residuals(p,x,y): 
    return y - sigmoid(p,x) 

def resize(x,y,xmin=0.0,xmax=1.0,ymin=0.0,ymax=1.0): 
    # Create local variables 
    NewX = [t for t in x] 
    NewY = [t for t in y] 
    # If the mins are greater than the maxs, then flip them. 
    if xmin>xmax: xmin,xmax=xmax,xmin 
    if ymin>ymax: ymin,ymax=ymax,ymin 
    #----------------------------------------------------------------------------------------------  
    # The rest of the code below re-calculates all the values in x and then in y with these steps: 
    #  1.) Subtract the actual minimum of the input x-vector from each value of x 
    #  2.) Multiply each resulting value of x by the result of dividing the difference 
    #   between the new xmin and xmax by the actual maximum of the input x-vector 
    #  3.) Add the new minimum to each value of x 
    # Note: I wrote in x-notation, but the identical process is also repeated for y 
    #----------------------------------------------------------------------------------------------  
    # Subtracts right operand from the left operand and assigns the result to the left operand. 
    # Note: c -= a is equivalent to c = c - a 
    NewX -= x.min() 

    # Multiplies right operand with the left operand and assigns the result to the left operand. 
    # Note: c *= a is equivalent to c = c * a 
    NewX *= (xmax-xmin)/NewX.max() 

    # Adds right operand to the left operand and assigns the result to the left operand. 
    # Note: c += a is equivalent to c = c + a 
    NewX += xmin 

    # Subtracts right operand from the left operand and assigns the result to the left operand. 
    # Note: c -= a is equivalent to c = c - a 
    NewY -= y.min() 

    # Multiplies right operand with the left operand and assigns the result to the left operand. 
    # Note: c *= a is equivalent to c = c * a 
    NewY *= (ymax-ymin)/NewY.max() 

    # Adds right operand to the left operand and assigns the result to the left operand. 
    # Note: c += a is equivalent to c = c + a 
    NewY += ymin 

    return (NewX,NewY) 

# Declare raw data for use in creating logistic regression equation 
x = np.array([821,576,473,377,326],dtype='float') 
y = np.array([255,235,208,166,157],dtype='float') 

# Call resize() function to re-calculate coordinates that will be used for equation 
MinRR=GetMinRR(50) 
MaxRR=1200 
minLVET=(y[4]/x[4])*MinRR 
maxLVET=(y[0]/x[0])*MaxRR 

#x,y=resize(x,y,xmin=0.3, ymin=0.3) 
NewX,NewY=resize(x,y,xmin=MinRR,xmax=MaxRR,ymin=minLVET,ymax=maxLVET) 
print 'x is: ',x 
print 'y is: ',y 
print 'NewX is: ',NewX 
print 'NewY is: ',NewY 

# p_guess is the starting estimate for the minimization 
p_guess=(np.median(x),np.median(y),1.0,1.0) 
New_p_guess=(np.median(NewX),np.median(NewY),1.0,1.0) 

# Calls the leastsq() function, which calls the residuals function with an initial 
# guess for the parameters and with the x and y vectors. The full_output means that 
# the function returns all optional outputs. Note that the residuals function also 
# calls the sigmoid function. This will return the parameters p that minimize the 
# least squares error of the sigmoid function with respect to the original x and y 
# coordinate vectors that are sent to it. 
p, cov, infodict, mesg, ier = scipy.optimize.leastsq( 
    residuals,p_guess,args=(x,y),full_output=1,warning=True) 

New_p, New_cov, New_infodict, New_mesg, New_ier = scipy.optimize.leastsq( 
    residuals,New_p_guess,args=(NewX,NewY),full_output=1,warning=True) 

# Define the optimal values for each element of p that were returned by the leastsq() function. 
x0,y0,c,k=p 
print('''Reference data:\ 
x0 = {x0} 
y0 = {y0} 
c = {c} 
k = {k} 
'''.format(x0=x0,y0=y0,c=c,k=k)) 

New_x0,New_y0,New_c,New_k=New_p 
print('''New data:\ 
New_x0 = {New_x0} 
New_y0 = {New_y0} 
New_c = {New_c} 
New_k = {New_k} 
'''.format(New_x0=New_x0,New_y0=New_y0,New_c=New_c,New_k=New_k)) 

# Create a numpy array of x-values 
xp = np.linspace(x.min(), x.max(), x.max()-x.min()) 
New_xp = np.linspace(NewX.min(), NewX.max(), NewX.max()-NewX.min()) 
# Return a vector pxp containing all the y values corresponding with the x-values in xp 
pxp=sigmoid(p,xp) 
New_pxp=sigmoid(New_p,New_xp) 

# Plot the results 
plt.plot(x, y, '>', xp, pxp, 'g-') 
plt.plot(NewX, NewY, '^',New_xp, New_pxp, 'r-') 
plt.xlabel('x') 
plt.ylabel('y',rotation='horizontal') 
plt.grid(True) 
plt.show() 
+2

あなたは10人の異なるユーザーの姿の下で20の質問をしました。あなたは1つの答えを受け入れたことはありませんし、誰にも感謝しました。私はそれが良い方法を理解する時間だとお勧めします。私は感謝の意を表明したいだけでなく、いつ質問に答えたのか、そうでないのかを知ることは有益です。 – tom10

+0

@ tom10:10人の異なるユーザーアカウントは明らかに意図的ではありません。彼らはすべて同じ名前とアバター画像を持ち、すべて「未登録のユーザー」とタグ付けされています。これはどういうことか分かりませんが、意図的ではありません。 –

+0

@Sven - 明らかにあなたが正しいと思われます。しかし、これらの質問やこれらすべてのユーザーアカウントでは、このユーザーがシステムの仕組みを把握する必要があるようです。 – tom10

答えて

2

これを試してください:あなたの他の関連する質問が閉じていない

alt text

import numpy as np 
import matplotlib.pyplot as plt 
import scipy.optimize 

def GetMinRR(age): 
    MaxHR = 208-(0.7*age) 
    MinRR = (60/MaxHR)*1000 
    return MinRR 

def sigmoid(p,x): 
    x0,y0,c,k=p 
    y = c/(1 + np.exp(-k*(x-x0))) + y0 
    return y 

def residuals(p,x,y): 
    return y - sigmoid(p,x) 

def resize(arr,lower=0.0,upper=1.0): 
    # Create local copy 
    result=arr.copy() 
    # If the mins are greater than the maxs, then flip them. 
    if lower>upper: lower,upper=upper,lower 
    #----------------------------------------------------------------------------------------------  
    # The rest of the code below re-calculates all the values in x and then in y with these steps: 
    #  1.) Subtract the actual minimum of the input x-vector from each value of x 
    #  2.) Multiply each resulting value of x by the result of dividing the difference 
    #   between the new xmin and xmax by the actual maximum of the input x-vector 
    #  3.) Add the new minimum to each value of x 
    #----------------------------------------------------------------------------------------------  
    # Subtracts right operand from the left operand and assigns the result to the left operand. 
    # Note: c -= a is equivalent to c = c - a 
    result -= result.min() 

    # Multiplies right operand with the left operand and assigns the result to the left operand. 
    # Note: c *= a is equivalent to c = c * a 
    result *= (upper-lower)/result.max() 

    # Adds right operand to the left operand and assigns the result to the left operand. 
    # Note: c += a is equivalent to c = c + a 
    result += lower 
    return result 


# Declare raw data for use in creating logistic regression equation 
x = np.array([821,576,473,377,326],dtype='float') 
y = np.array([255,235,208,166,157],dtype='float') 

# Call resize() function to re-calculate coordinates that will be used for equation 
MinRR=GetMinRR(50) 
MaxRR=1200 
# x[-1] returns the last value in x 
minLVET=(y[-1]/x[-1])*MinRR 
maxLVET=(y[0]/x[0])*MaxRR 

print(MinRR, MaxRR) 
#x,y=resize(x,y,xmin=0.3, ymin=0.3) 
NewX=resize(x,lower=MinRR,upper=MaxRR) 
NewY=resize(y,lower=minLVET,upper=maxLVET) 
print 'x is: ',x 
print 'y is: ',y 
print 'NewX is: ',NewX 
print 'NewY is: ',NewY 

# p_guess is the starting estimate for the minimization 
p_guess=(np.median(x),np.min(y),np.max(y),0.01) 
New_p_guess=(np.median(NewX),np.min(NewY),np.max(NewY),0.01) 

# Calls the leastsq() function, which calls the residuals function with an initial 
# guess for the parameters and with the x and y vectors. The full_output means that 
# the function returns all optional outputs. Note that the residuals function also 
# calls the sigmoid function. This will return the parameters p that minimize the 
# least squares error of the sigmoid function with respect to the original x and y 
# coordinate vectors that are sent to it. 
p, cov, infodict, mesg, ier = scipy.optimize.leastsq( 
    residuals,p_guess,args=(x,y),full_output=1,warning=True) 

New_p, New_cov, New_infodict, New_mesg, New_ier = scipy.optimize.leastsq( 
    residuals,New_p_guess,args=(NewX,NewY),full_output=1,warning=True) 

# Define the optimal values for each element of p that were returned by the leastsq() function. 
x0,y0,c,k=p 
print('''Reference data:\ 
x0 = {x0} 
y0 = {y0} 
c = {c} 
k = {k} 
'''.format(x0=x0,y0=y0,c=c,k=k)) 

New_x0,New_y0,New_c,New_k=New_p 
print('''New data:\ 
New_x0 = {New_x0} 
New_y0 = {New_y0} 
New_c = {New_c} 
New_k = {New_k} 
'''.format(New_x0=New_x0,New_y0=New_y0,New_c=New_c,New_k=New_k)) 

# Create a numpy array of x-values 
xp = np.linspace(x.min(), x.max(), x.max()-x.min()) 
New_xp = np.linspace(NewX.min(), NewX.max(), NewX.max()-NewX.min()) 
# Return a vector pxp containing all the y values corresponding with the x-values in xp 
pxp=sigmoid(p,xp) 
New_pxp=sigmoid(New_p,New_xp) 

# Plot the results 
plt.plot(x, y, '>', xp, pxp, 'g-') 
plt.plot(NewX, NewY, '^',New_xp, New_pxp, 'r-') 
plt.xlabel('x') 
plt.ylabel('y',rotation='horizontal') 
plt.grid(True) 
plt.show() 
が、あなたが登録した表示されます。ここ

は私の完全なコードの現在のドラフトでありますstackoverflowは this userthis userと同じであると認識しないため、他の質問を編集することはできません。

上記のコードで主に行ったのは、New_p_guessです。 最初の推測の正しい値を見つけることは、芸術の一種です。それがアルゴリズム的に行うことができれば、scipyはあなたにそれをするように求めていないだろう。少しの分析だけでなく、データに対する「気持ち」を助けることができます。ソリューションがおおよそどのように見えるかを事前に知っているので、問題のコンテキスト内でどのような値が合理的であるかを知ることも役立ちます。 (それは、ちょうど私がk = 0.01を選択する方法を推測したと言っていることの長い道のりです)