-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_sklearn_LogisticRegression.py
More file actions
54 lines (41 loc) · 2.2 KB
/
Python_sklearn_LogisticRegression.py
File metadata and controls
54 lines (41 loc) · 2.2 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
# Python tutorial using scikit-learn for Logistic Regression on the Digits dataset.
# The logistic regression model is regression analysis when the dependent variable is binary (0 or 1).
# 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.
# Import python libraries
from sklearn.datasets import load_digits
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
# Import the digits dataset
dataSet = load_digits()
# Analyze the dataset's shape
#print("Dataset Shape: ", dataSet.data.shape)
# Analyze the target set of the data
#print("Dataset Labels: ", dataSet.target)
# Analyze the images for the first 5 digit in the dataset using matplotlib
plt.figure(figsize=(20,4))
for index, (image, label) in enumerate(zip(dataSet.data[0:5], dataSet.target[0:5])):
plt.subplot(1, 5, index + 1)
plt.imshow(np.reshape(image, (8,8)), cmap=plt.cm.gray)
plt.title('training: %i' % label, fontsize=20)
#plt.show()
# 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.25, random_state=0)
# Create the Logistic Regression model
theModel = LogisticRegression()
# Train the model using the training datasets
theModel.fit(X_train, y_train)
# Predict the testing dataset using the recently created logistic regression model
theModel_Predict = theModel.predict(X_test)
# Score the prediction of the trained model
score = theModel.score(X_test, y_test)
# Analyze the model's accuracy score
print("Model's Accuracy Score: ", score)
# Using the sklearn metrics library analyze the models confusion matrix
cm = metrics.confusion_matrix(y_test, theModel_Predict)
print('\n',cm)