forked from Davisy/Model-Deployment-by-using-Flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
32 lines (24 loc) · 870 Bytes
/
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
# import packages
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle
from sklearn.metrics import r2_score
#load dataset
dataset = pd.read_csv('data/salary_data.csv')
# split data into features and target
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
#split the data into train and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.05, random_state = 0)
# create a model
regressor = LinearRegression()
#train the model
regressor.fit(X_train, y_train)
#perform prediction
y_pred = regressor.predict(X_test)
# you can check the peformance of the model from the following code
#print("R2 score: {}".format(r2_score(y_test,y_pred)))
#save the trained model
pickle.dump(regressor, open('models/regressor.pkl','wb'))