Linear Regression
import pandas as pd
## Create DataSet
dataset = pd.DataFrame.from_dict(
[
{"name": "Suresh", "duration": 2, "marks": 20, "pass": False},
{"name": "Ramesh", "duration": 8, "marks": 80, "pass": True},
{"name": "Akshay", "duration": 4, "marks": 60, "pass": True},
{"name": "Mahesh", "duration": 9, "marks": 80, "pass": True},
{"name": "Shekhar", "duration": 7, "marks": 70, "pass": True},
{"name": "SomeOne", "duration": 3, "marks": 30, "pass": False},
]
)
dataset
Define dependent(y) and independent(x) variable
y = dataset['marks']
x = dataset['duration']
# Import numpy
import numpy as np
x
## For .fit() function, independent variable should be in 2D
# Convert to 2D
new_x = x.values.reshape(len(x), 1)
# Now we have 2D value
new_x
Create and Train Model, and predict
# Import Module
from sklearn.linear_model import LinearRegression
# Create Model
model = LinearRegression()
# Train Model
model.fit(new_x, y)
# Print Coefficient
model.coef_
# Print Intercept
model.intercept_
# Predict marks for 5 Hrs
model.predict([[5]])
Save model for future
# Import module
from sklearn.externals import joblib
# This saves model in file - MarksPrediction.pk1
joblib.dump(model, 'MarksPrediction.pk1')
Use a saved model
# Create a new model from the PK1 file
new_model = joblib.load('MarksPrediction.pk1')
# Predict using this model
new_model.predict([[5]])