-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlin_reg.py
More file actions
59 lines (50 loc) · 1.84 KB
/
lin_reg.py
File metadata and controls
59 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from statsmodels.tsa.arima.model import ARIMA
import utils as utils
import clean_data_final as clean_data
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
import matplotlib.pyplot as plt
from pmdarima.arima import auto_arima
from sklearn.model_selection import TimeSeriesSplit
from sklearn.metrics import mean_squared_error
# To run a model on a specified auction type, pick from the following as the parameter for df:
# 'df_all'
# 'four_week'
# 'eight_week'
# 'thirteen_week'
# 'seventeen_week'
# 'twenty_six_week'
# 'fifty_two_week'
# 'cmb'
def get_linear_regression_model(df):
data = utils.get_auction_type_df(df)
data = utils.resample_data(data)
x_train, y_train, x_test, y_test = utils.split_train_test(data, 0.8)
x_train = np.delete(x_train, 0, axis=1) # delete the time data
x_test = np.delete(x_test, 0, axis=1)
# Fit the regression
model = LinearRegression()
model.fit(x_train, y_train)
y_pred = model.predict(x_test)
r_sq = model.score(x_test, y_test)
y_pred_train = model.predict(x_train)
MSE_train = mean_squared_error(y_train, y_pred_train)
MSE = mean_squared_error(y_test, y_pred)
print(f"MSE train: {MSE_train}")
print(f"MSE: {MSE}")
print(f"coefficient of determination: {r_sq}")
print(f"intercept: {model.intercept_}")
print(f"slope: {model.coef_}")
plt.plot(y_test, label='True Values')
plt.plot(y_pred, label='Predicted Values')
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Linear Regression Model: True Values vs. Predicted Values')
plt.legend()
plt.show()
get_linear_regression_model('four_week')