简体   繁体   English

使用pandas在python中查找数据帧的每一行的最小二乘线性回归

[英]Finding the least squares linear regression for each row of a dataframe in python using pandas

I have the a dataframe: 我有一个数据帧:

(day/month/year) (日月年)

df = pd.DataFrame({'Name': ['A', 'B', 'C'], 
                   'Date0': ['01/01/1999','01/06/1999','01/01/1979'], 'V0': [29,44,21],
                   'Date1': ['08/01/2000','07/01/2000','01/01/2000'],'V1': [35, 45, 47]})

I want to interpolate the age for each row to find 'V_10' which is the value at the date 10/08/1999 using a linear regression. 我想插入每行的年龄,找到'V_10',这是使用线性回归在10/08/1999日期的值。 For example in the first case I would get something like: 例如,在第一种情况下,我会得到类似的东西:

Slope   0.01609
Y-intercept     29.00    
df = pd.DataFrame({'Name': ['A', 'B', 'C'], 
                   'Date0': ['01/01/1999','01/06/1999','01/01/1979'], 'V0': [29,44,21],
                   'Date1': ['08/01/2000','07/01/2000','01/01/2000'],'V1': [35, 45, 47], 
                   'V_10':[32.57]})

I hope my calculations are correct. 我希望我的计算是正确的。

And what if I want an exponential regression or worse a custom function I have? 如果我想要指数回归或更糟糕的自定义函数怎么办?

I'm not sure if this is what you're after, but for a linear interpolation you could do the following: 我不确定这是否是您所追求的,但对于线性插值,您可以执行以下操作:

import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline

df = pd.DataFrame({'Name': ['A', 'B', 'C'], 
               'Date0': ['01/01/1999','01/06/1999','01/01/1979'], 'V0': [29,44,21],
               'Date1': ['08/01/2000','07/01/2000','01/01/2000'],'V1': [35, 45, 47]})
df['Target'] = pd.to_datetime('10/08/1999')
df['Date0'] = pd.to_datetime(df['Date0'])
df['Date1'] = pd.to_datetime(df['Date1'])
df['Target'] = pd.to_datetime(df['Target'])

def regress(xs, ys, newx, reference=pd.to_datetime('1/1/1900'), retype='linear', fit_intercept=True, degree=None):
    xs = [(x - reference).days for x in xs]
    xs = np.array(xs).reshape(-1,1)
    ys = np.array(ys)
    if retype == 'linear':
        lm = LinearRegression(fit_intercept=fit_intercept)
    elif retype == 'polynomial':
        lm = Pipeline([('poly', PolynomialFeatures(degree=degree)),
                   ('linear', LinearRegression(fit_intercept=fit_intercept))])
    else:
        return print('Need to specify other regression type.')
    lm.fit(xs,ys)
    return lm.predict(np.array((newx - reference).days).reshape(-1, 1))[0]

# Linear regression example
df['V10'] = df.apply(lambda x: regress([x.Date0,x.Date1], [x.V0,x.V1], x.Target, retype='linear'), axis=1)
# 2nd-degree polynomial regression example
df['V11']=df.apply(lambda x: regress([x.Date0,x.Date1], [x.V0,x.V1], x.Target, retype='polynomial', degree=2), axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM