forked from adi0509/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logistic_Regression.py
69 lines (50 loc) · 2.02 KB
/
Logistic_Regression.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
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
"""
This Is A Simple Logistic Regression Program To Classify Iris Species
Resources:
(1) https://towardsdatascience.com/building-a-logistic-regression-in-python-301d27367c24
(2) https://towardsdatascience.com/logistic-regression-a-simplified-approach-using-python-c4bc81a87c31
(3) https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html
"""
# Import the dependencies
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
#Load the data set
data = sns.load_dataset("iris")
data.head()
#Prepare the training set
# X = feature values, all the columns except the last column
X = data.iloc[:, :-1]# or data.loc[:,'sepal_length':'petal_width']
# y = target values, last column of the data frame
y = data.iloc[:, -1]# or data.loc[:,'species']
# Plot the relation of each feature with each species
plt.xlabel('Features')
plt.ylabel('Species')
pltX = data.loc[:, 'sepal_length']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='blue', label='sepal_length')
pltX = data.loc[:, 'sepal_width']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='green', label='sepal_width')
pltX = data.loc[:, 'petal_length']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='red', label='petal_length')
pltX = data.loc[:, 'petal_width']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='black', label='petal_width')
plt.legend(loc=4, prop={'size':8})
plt.show()
#Split the data into 80% training and 20% testing
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#Train the model
model = LogisticRegression()
model.fit(x_train, y_train) #Training the model
#Test the model
predictions = model.predict(x_test)
print(predictions)
#Check precision, recall, f1-score
print( classification_report(y_test, predictions) )
print( accuracy_score(y_test, predictions))