-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_advertising_model.py
48 lines (35 loc) · 1.13 KB
/
train_advertising_model.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
import pandas as pd
# read data
df = pd.read_csv("https://raw.githubusercontent.com/erkansirin78/datasets/master/Advertising.csv")
print(df.head())
# Feature matrix
X = df.iloc[:, 1:-1].values
print(X.shape)
print(X[:3])
# Output variable
y = df.iloc[:, -1]
print(y.shape)
print(y[:6])
# split test train
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
# train model
from sklearn.ensemble import RandomForestRegressor
estimator = RandomForestRegressor(n_estimators=200)
estimator.fit(X_train, y_train)
# Test model
y_pred = estimator.predict(X_test)
from sklearn.metrics import r2_score
r2 = r2_score(y_true=y_test, y_pred=y_pred)
print("R2: ".format(r2))
# Save Model
import joblib
joblib.dump(estimator, "saved_models/03.randomforest_with_advertising.pkl")
# make predictions
# Read models
estimator_loaded = joblib.load("saved_models/03.randomforest_with_advertising.pkl")
# Prediction set
X_manual_test = [[230.1,37.8,69.2]]
print("X_manual_test", X_manual_test)
prediction = estimator_loaded.predict(X_manual_test)
print("prediction", prediction)