Spaces:
Sleeping
Sleeping
Development
commited on
Commit
•
c843a3d
0
Parent(s):
Add files
Browse files- Salary_Data.csv +31 -0
- model.py +37 -0
- request.py +6 -0
- server.py +32 -0
Salary_Data.csv
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
YearsExperience,Salary
|
2 |
+
1.1,39343.00
|
3 |
+
1.3,46205.00
|
4 |
+
1.5,37731.00
|
5 |
+
2.0,43525.00
|
6 |
+
2.2,39891.00
|
7 |
+
2.9,56642.00
|
8 |
+
3.0,60150.00
|
9 |
+
3.2,54445.00
|
10 |
+
3.2,64445.00
|
11 |
+
3.7,57189.00
|
12 |
+
3.9,63218.00
|
13 |
+
4.0,55794.00
|
14 |
+
4.0,56957.00
|
15 |
+
4.1,57081.00
|
16 |
+
4.5,61111.00
|
17 |
+
4.9,67938.00
|
18 |
+
5.1,66029.00
|
19 |
+
5.3,83088.00
|
20 |
+
5.9,81363.00
|
21 |
+
6.0,93940.00
|
22 |
+
6.8,91738.00
|
23 |
+
7.1,98273.00
|
24 |
+
7.9,101302.00
|
25 |
+
8.2,113812.00
|
26 |
+
8.7,109431.00
|
27 |
+
9.0,105582.00
|
28 |
+
9.5,116969.00
|
29 |
+
9.6,112635.00
|
30 |
+
10.3,122391.00
|
31 |
+
10.5,121872.00
|
model.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Simple Linear Regression
|
2 |
+
|
3 |
+
'''
|
4 |
+
This model predicts the salary of the employ based on experience using simple linear regression model.
|
5 |
+
'''
|
6 |
+
|
7 |
+
# Importing the libraries
|
8 |
+
import numpy as np
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
import pandas as pd
|
11 |
+
import pickle
|
12 |
+
import requests
|
13 |
+
import json
|
14 |
+
|
15 |
+
# Importing the dataset
|
16 |
+
dataset = pd.read_csv('Salary_Data.csv')
|
17 |
+
X = dataset.iloc[:, :-1].values
|
18 |
+
y = dataset.iloc[:, 1].values
|
19 |
+
|
20 |
+
# Splitting the dataset into the Training set and Test set
|
21 |
+
from sklearn.model_selection import train_test_split
|
22 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3, random_state = 0)
|
23 |
+
|
24 |
+
# Fitting Simple Linear Regression to the Training set
|
25 |
+
from sklearn.linear_model import LinearRegression
|
26 |
+
regressor = LinearRegression()
|
27 |
+
regressor.fit(X_train, y_train)
|
28 |
+
|
29 |
+
# Predicting the Test set results
|
30 |
+
y_pred = regressor.predict(X_test)
|
31 |
+
|
32 |
+
# Saving model to disk
|
33 |
+
pickle.dump(regressor, open('model.pkl','wb'))
|
34 |
+
|
35 |
+
# Loading model to compare the results
|
36 |
+
model = pickle.load( open('model.pkl','rb'))
|
37 |
+
print(model.predict([[1.8]]))
|
request.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# URL
|
2 |
+
url = 'http://localhost:5000/api'
|
3 |
+
|
4 |
+
# Change the value of experience that you want to test
|
5 |
+
r = requests.post(url,json={'exp':1.8,})
|
6 |
+
print(r.json())
|
server.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Create API of ML model using flask
|
2 |
+
|
3 |
+
'''
|
4 |
+
This code takes the JSON data while POST request an performs the prediction using loaded model and returns
|
5 |
+
the results in JSON format.
|
6 |
+
'''
|
7 |
+
|
8 |
+
# Import libraries
|
9 |
+
import numpy as np
|
10 |
+
from flask import Flask, request, jsonify
|
11 |
+
import pickle
|
12 |
+
|
13 |
+
app = Flask(__name__)
|
14 |
+
|
15 |
+
# Load the model
|
16 |
+
model = pickle.load(open('model.pkl','rb'))
|
17 |
+
|
18 |
+
@app.route('/api',methods=['POST'])
|
19 |
+
def predict():
|
20 |
+
# Get the data from the POST request.
|
21 |
+
data = request.get_json(force=True)
|
22 |
+
|
23 |
+
# Make prediction using model loaded from disk as per the data.
|
24 |
+
prediction = model.predict([[np.array(data['exp'])]])
|
25 |
+
|
26 |
+
# Take the first value of prediction
|
27 |
+
output = prediction[0]
|
28 |
+
|
29 |
+
return jsonify(output)
|
30 |
+
|
31 |
+
if __name__ == '__main__':
|
32 |
+
app.run(port=5000, debug=True)
|