2014-01-21 70 views
3

我有一些數據,我試圖擬合三個多項式。我已經在三個主屏幕,Draw和Away中分別安裝了polys。問題是有一個限制多元多項式擬合足球數據

Home+Draw+Away=1

以下代碼計算每個列的多項式係數。

home_coeffs = polyfit(match_rating, y_home,1) 
draw_coeffs = polyfit(match_rating, y_draw,2) 
away_coeffs = polyfit(match_rating, y_away,2) 

我該如何執行約束或將問題重新表達爲多變量多項式擬合?

Home Draw Away 
MatchRating    
-26  0.000000 0.000000 1.000000 
-24  0.000000 0.000000 1.000000 
-22  0.500000 0.000000 0.500000 
-21  0.111111 0.333333 0.555556 
-20  0.250000 0.000000 0.750000 
-19  0.500000 0.333333 0.166667 
-18  0.100000 0.000000 0.900000 
-17  0.111111 0.222222 0.666667 
-16  0.187500 0.375000 0.437500 
-15  0.240000 0.120000 0.640000 
-14  0.272727 0.272727 0.454545 
-13  0.214286 0.250000 0.535714 
-12  0.219512 0.463415 0.317073 
-11  0.333333 0.214286 0.452381 
-10  0.208955 0.238806 0.552239 
-9 0.357143 0.285714 0.357143 
-8 0.430556 0.291667 0.277778 
-7 0.283784 0.405405 0.310811 
-6 0.288462 0.298077 0.413462 
-5 0.402299 0.218391 0.379310 
-4 0.379630 0.259259 0.361111 
-3 0.420561 0.317757 0.261682 
-2 0.426752 0.292994 0.280255 
-1 0.452174 0.260870 0.286957 
0 0.419118 0.330882 0.250000 
1 0.553957 0.251799 0.194245 
2 0.514925 0.268657 0.216418 
3 0.483333 0.308333 0.208333 
4 0.465347 0.326733 0.207921 
5 0.575758 0.202020 0.222222 
6 0.587500 0.212500 0.200000 
7 0.584615 0.230769 0.184615 
8 0.594203 0.275362 0.130435 
9 0.609375 0.312500 0.078125 
10 0.714286 0.122449 0.163265 
11 0.780000 0.160000 0.060000 
12 0.769231 0.128205 0.102564 
13 0.764706 0.117647 0.117647 
14 0.653846 0.230769 0.115385 
15 0.826087 0.130435 0.043478 
16 0.500000 0.375000 0.125000 
17 1.000000 0.000000 0.000000 
18 1.000000 0.000000 0.000000 
19 0.714286 0.142857 0.142857 
20 1.000000 0.000000 0.000000 
21 0.666667 0.333333 0.000000 
22 1.000000 0.000000 0.000000 
26 1.000000 0.000000 0.000000 

的情節我已經在這裏生產:

enter image description here enter image description here enter image description here

回答

4

您可以使用scipy.optimize.leastsq()。下面是完整的代碼:

import pandas as pd 
import io 
import numpy as np 

txt = """ Home Draw Away 
-26  0.000000 0.000000 1.000000 
-24  0.000000 0.000000 1.000000 
-22  0.500000 0.000000 0.500000 
-21  0.111111 0.333333 0.555556 
-20  0.250000 0.000000 0.750000 
-19  0.500000 0.333333 0.166667 
-18  0.100000 0.000000 0.900000 
-17  0.111111 0.222222 0.666667 
-16  0.187500 0.375000 0.437500 
-15  0.240000 0.120000 0.640000 
-14  0.272727 0.272727 0.454545 
-13  0.214286 0.250000 0.535714 
-12  0.219512 0.463415 0.317073 
-11  0.333333 0.214286 0.452381 
-10  0.208955 0.238806 0.552239 
-9 0.357143 0.285714 0.357143 
-8 0.430556 0.291667 0.277778 
-7 0.283784 0.405405 0.310811 
-6 0.288462 0.298077 0.413462 
-5 0.402299 0.218391 0.379310 
-4 0.379630 0.259259 0.361111 
-3 0.420561 0.317757 0.261682 
-2 0.426752 0.292994 0.280255 
-1 0.452174 0.260870 0.286957 
0 0.419118 0.330882 0.250000 
1 0.553957 0.251799 0.194245 
2 0.514925 0.268657 0.216418 
3 0.483333 0.308333 0.208333 
4 0.465347 0.326733 0.207921 
5 0.575758 0.202020 0.222222 
6 0.587500 0.212500 0.200000 
7 0.584615 0.230769 0.184615 
8 0.594203 0.275362 0.130435 
9 0.609375 0.312500 0.078125 
10 0.714286 0.122449 0.163265 
11 0.780000 0.160000 0.060000 
12 0.769231 0.128205 0.102564 
13 0.764706 0.117647 0.117647 
14 0.653846 0.230769 0.115385 
15 0.826087 0.130435 0.043478 
16 0.500000 0.375000 0.125000 
17 1.000000 0.000000 0.000000 
18 1.000000 0.000000 0.000000 
19 0.714286 0.142857 0.142857 
20 1.000000 0.000000 0.000000 
21 0.666667 0.333333 0.000000 
22 1.000000 0.000000 0.000000 
26 1.000000 0.000000 0.000000""" 
df = pd.read_csv(io.BytesIO(txt), delim_whitespace=True, index_col=0) 

from scipy import optimize 

x = df.index.values 
y1 = df.Home.values 
y2 = df.Draw.values 
y3 = df.Away.values 

def f(params): 
    a, b, c, d, e = params 
    oy1 = a + b*x 
    oy2 = c + d*x + e*x*x 
    oy3 = 1.0 - oy1 - oy2 
    return oy1, oy2, oy3 

def error(params): 
    oy1, oy2, oy3 = f(params) 
    e1 = y1 - oy1 
    e2 = y2 - oy2 
    e3 = y3 - oy3 
    return np.concatenate((e1, e2, e3)) 

params = optimize.leastsq(error, [1, 1, 1, 1, 1])[0] 

oy1, oy2, oy3 = f(params) 

import pylab as pl 
pl.plot(x, y1) 
pl.plot(x, oy1) 

pl.plot(x, y2) 
pl.plot(x, oy2) 

pl.plot(x, y3) 
pl.plot(x, oy3) 

這裏是輸出:

enter image description here

這裏是PARAMS:

[ 4.97460839e-01 1.71243863e-02 2.74933473e-01 -1.58439751e-03 
    -3.48952223e-04] 
+1

可愛圖:) – TomAugspurger

+0

的確不錯,可愛!但是,我看不到如何從scipy導入?我複製粘貼代碼到IPython的筆記本,但 '導入錯誤:沒有模塊scipy' – aerkenemesis

+2

命名請從http://scipy.org – HYRY