title: How to do a linear regression for each three rows in Python?
body: <p>I want to make a linear regression for each three rows using for-loop and count, but I couldn't make it because I am confused with the input (x &amp; y) for linear regression.</p>
  
  <p>Here it is the code:</p>
  
  <pre><code>from sklearn.metrics import mean_squared_error, r2_score
  import statsmodels.api as sm
  import numpy as np
  
  year=data_['Year']
  value=data_['Value']
  count=0
  for a,b in zip(year,value):
      print(a,b) 
      count = count+1[input][1]
      window_type='rolling'
  
      if count%3 == 0 :
          x=data_.loc[0:3,['Year']]
          y=data_.loc[0:3,['Value']]
  
          reg=linear_model.LinearRegression()
          x_train,x_test,y_train,y_test=train_test_split(x,y,test_size = 0.2 ,random_state=3)
          reg.fit(x,y)
  
          y4=4*reg.coef_ + reg.intercept_
  
          print("Equation : 4 *", reg.coef_, "+", reg.intercept_)
          print("Y4 : ", y4)
          print("====")
  </code></pre>
  
  <p>Actual result:</p>
  
  <pre><code>1 6.262008
  2 5.795994
  3 5.082562
  Equation : 4 * [[-76.71615936]] + [209.89679764]
  Y4 :  [[-96.96783982]]
  ====
  1 285.433511
  2 260.43560099999996
  3 238.71312400000002
  Equation : 4 * [[-76.71615936]] + [209.89679764]
  Y4 :  [[-96.96783982]]
  ====
  1 2.595145
  2 2.508278
  3 2.67997
  Equation : 4 * [[-76.71615936]] + [209.89679764]
  Y4 :  [[-96.96783982]]
  ====
  </code></pre>
  
  <p>The output I desire is:
  every three rows result a different Y4</p>
  
  <p>Please help me fixing this problem.</p>