-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_sklearn_LinearRegression.py
More file actions
78 lines (59 loc) · 2.68 KB
/
Python_sklearn_LinearRegression.py
File metadata and controls
78 lines (59 loc) · 2.68 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Python tutorial using scikit-learn for linear regression on diabetes dataset.
# Linear Regression is a common type of model for predictive analysis.
# The model is a linear approach to modeling the relationship between a scalar response (dependent variable) and explanatory variables (independent variable).
# Python is an interpreted, high-level, general-purpose programming language.
# sci-kit learn or sklearn is an high-level machine learning library for python.
# NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
# Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy.
''' Linear Regression Model
y = X * beta + c + E
y = target
X = data
beta = coefficients
c = intercept
E = Error
'''
# Import python libraries
import matplotlib.pylab as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn import datasets
from sklearn import metrics
from sklearn.model_selection import train_test_split
# Import the diabetes dataset
dataSet = datasets.load_diabetes()
# Analyze the feature set of the data
#print("features: ", dataSet.feature_names)
# Analyze the target set of the data
#print("Labels: ", dataSet.target_names)
# Analyze the dataset's shape
#print("Data's Shape: ", dataSet.data.shape)
# Analyze the target set's shape of the data
#print("Data's Target Shape: ", dataSet.target.shape)
# Analyze the first five entires of the dataset's values
#print("Data's First Five: ", dataSet.data[0:5])
# Analyze the target set of the data
#print("Data's Target: ", dataSet.target)
# Split the whole dataset into a seperate training and testing dataset
X_train, X_test, y_train, y_test = train_test_split(dataSet.data, dataSet.target, test_size=0.2, random_state=0)
# Create the linear regression model
theModel = LinearRegression()
# Train the model using the training sets
theModel.fit(X_train, y_train)
# Predict the testing dataset using the recently created Linear regression model.
theModel_Predict = theModel.predict(X_test)
# Analyze the accuracy score of the model using the testing dataset
score = theModel.score(X_test, y_test)
print("Accuracy: ", score)
# Analyze the model's Coefficients
theModel_Coefficients = theModel.coef_
print("Coefficients: ", theModel_Coefficients)
# Analyze the model's Intercept
theModel_Intercept = theModel.intercept_
print("Intercept: ", theModel_Intercept)
# Analyze the Linear regression model's results using a plot
plt.plot(y_test, theModel_Predict, '.')
x = np.linspace(0, 330, 100)
y = x
plt.plot(x, y)
plt.show()