-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinear_regression_basic.py
58 lines (33 loc) · 1.76 KB
/
linear_regression_basic.py
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
##############################################################################
# IMPORT REQUIRED PACKAGES
##############################################################################
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score
##############################################################################
# IMPORT SAMPLE DATA
##############################################################################
my_df = pd.read_csv("sample_data_regression.csv")
##############################################################################
# SPLIT INPUT VARIABLES & OUTPUT VARIABLES
##############################################################################
X = my_df.drop(["output"], axis = 1)
y = my_df["output"]
##############################################################################
# SPLIT OUT TRAINING & TEST SETS
##############################################################################
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
##############################################################################
# INSTANTIATE OUR MODEL
##############################################################################
regressor = LinearRegression()
##############################################################################
# MODEL TRAINING
##############################################################################
regressor.fit(X_train, y_train)
##############################################################################
# MODEL ASSESSMENT
##############################################################################
y_pred = regressor.predict(X_test)
r2_score(y_test, y_pred)