'Linearregression of two dataframes

I have two dataframes:

df = pd.DataFrame([{'A': -4, 'B': -3, 'C': -2, 'D': -1, 'E': 2, 'F': 4, 'G': 8, 'H': 6, 'I': -2}])

df2 looks like this (just a cutout; in total there are ~100 rows).

df2 = pd.DataFrame({'Date': [220412004, 220412004, 220412004, 220412006], 'A': [-0.15584, -0.11446, -0.1349, -0.0458], 'B': [-0.11826, -0.0833, -0.1025, -0.0216], 'C': [-0.0611, -0.0413, -0.0645, -0.0049], 'D': [-0.04461, -0.022693, -0.0410, 0.0051], 'E': [0.0927, 0.0705, 0.0923, 0.0512], 'F': [0.1453, 11117, 0.1325, 0.06205], 'G': [0.30077, 0.2274, 0.2688, 0.1077], 'H': [0.2449, 0.1860, 0.2274, 0.09328], 'I': [-0.0706, -0.0612, -0.0704, -0.02953]})

    Date          A           B            C          D          E         F         G           H       I
3   220412004   -0.15584    -0.11826    -0.0611    -0.04461    0.0927   0.1453    0.30077    0.2449   -0.0706
4   220412004   -0.11446    -0.0833     -0.0413    -0.022693   0.0705   0.11117   0.2274     0.1860   -0.0612
5   220412004   -0.1349     -0.1025     -0.0645    -0.0410     0.0923   0.1325    0.2688     0.2274   -0.0704
7   220412006   -0.0458     -0.0216     -0.0049     0.0051     0.0512   0.06205   0.1077     0.09328  -0.02953

Now I want to iterate through df2 and make a linear regression of each row (of df2) as y-axis with df as base (x-Axis).

My approach was:

import numpy as np
import pandas as pd
from sklearn.metrics import r2_score

for index, row in df2.iterrows():
     reg = np.polyfit(df, row, 1)
     predict = np.poly1d(reg) 
     trend = np.polyval(reg, df)
     std = row.std() 
     r2 = np.round(r2_score(row.values, predict(df)), 5) 
 

However, I get this error:

TypeError: can only concatenate str (not "float") to str

Any ideas? Thx in advance



Solution 1:[1]

The documentation of the Numpy indicates that the x and y in polyfit function have shape (M,) and shape (M,) or (M, K). You do not comply with this agreement for the x and 'y'. It should be used like np.polyfit(df.values[0], row.values, 1).

Note that the date must be passed to the index.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammadreza Riahi