title: creating baseline regression model with average and min values in python
body: <p>I want to compare results of my regression analysis with encoded categorical variables with two baseline models where the baseline predictions are specified as the average or min values of the groups. I've chosen Rsquare and MAE for comparison. Below is a simplified example of my code for illustration. It works in that it gives me an output which I think achieves my goal. Is this the correct and/or best way to do this?</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  from sklearn.linear_model import LinearRegression
  from sklearn.model_selection import train_test_split 
  from sklearn import metrics
  
  df = pd.DataFrame([['a1','c1',10],
                     ['a1','c2',15],
                     ['a1','c3',20],
                     ['a1','c1',15],
                     ['a2','c2',20],
                     ['a2','c3',15],
                     ['a2','c1',20],
                     ['a2','c2',15],
                     ['a3','c3',20],
                     ['a3','c3',15],
                     ['a3','c3',15],
                     ['a3','c3',20]], columns=['aid','cid','T'])
  
  df_dummies = pd.get_dummies(df, columns=['aid','cid'],prefix_sep='',prefix='')
  df_dummies
  
  X = df_dummies
  y = df_dummies['T']
  
  # train test split 80-20
  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
  
  regr = LinearRegression()
  regr.fit(X_train, y_train)
  
  y_pred = regr.predict(X_test)
  print('R-squared:', metrics.r2_score(y_test, y_pred))
  print('MAE:', metrics.mean_absolute_error(y_test, y_pred))
  
  # Baseline model with group average as prediction
  y_pred = df.groupby('aid').agg({'T': ['mean']})
  print('R-squared:', metrics.r2_score(y_test, y_pred))
  print('MAE:', metrics.mean_absolute_error(y_test, y_pred))
  
  # Baseline model with group min as prediction
  y_pred = df.groupby('aid').agg({'T': ['min']})
  print('R-squared:', metrics.r2_score(y_test, y_pred))
  print('MAE:', metrics.mean_absolute_error(y_test, y_pred))
  </code></pre>