|
5 | 5 | import seaborn as sns |
6 | 6 | import warnings |
7 | 7 | from numpy.conftest import dtype |
| 8 | +from sklearn.linear_model import LogisticRegression |
| 9 | +from sklearn.metrics import accuracy_score, confusion_matrix, classification_report |
8 | 10 |
|
| 11 | +model = LogisticRegression() |
9 | 12 | warnings.simplefilter("ignore") |
10 | 13 |
|
11 | 14 |
|
|
42 | 45 | plt.title("Correlation Matrix") |
43 | 46 | plt.show() |
44 | 47 |
|
45 | | -#Box Plot for Outlier |
46 | | -for col in ['SepalLength', 'SepalWidth','PetalLength','PetalWidth']: |
47 | | - sns.boxplot(df[col]) |
48 | | - plt.title(f'Box plot of {col}') |
49 | | - plt.show() |
50 | | - |
51 | | - |
52 | 48 | #Define x and y |
53 | 49 |
|
54 | 50 | x=df[['SepalLength', 'SepalWidth','PetalWidth','PetalLength']] #Independent Variables |
|
66 | 62 | print("Y_test shape:", y_test.shape) |
67 | 63 |
|
68 | 64 |
|
69 | | -print(df.describe()) |
70 | | -from email.errors import FirstHeaderLineIsContinuationDefect |
71 | | -import pandas as pd |
72 | | -import numpy as np |
73 | | -import matplotlib.pyplot as plt |
74 | | -import seaborn as sns |
75 | | -import warnings |
76 | | -from numpy.conftest import dtype |
77 | | - |
78 | | -warnings.simplefilter("ignore") |
| 65 | +model.fit(x_train,y_train) |
| 66 | +y_pred=model.predict(x_test) |
79 | 67 |
|
80 | | - |
81 | | -#Import iris Dataset. |
82 | | -df=pd.read_csv(r'C:\Users\PC\Downloads\iris\iris.data') |
| 68 | +#calculate accuracy and confusion matrix |
| 69 | +accuracy = accuracy_score(y_test,y_pred)*100 |
| 70 | +conf_matrix = confusion_matrix(y_test,y_pred) |
83 | 71 |
|
84 | 72 |
|
85 | | -# Assign column names to the dataset |
86 | | -df.columns = ['SepalLength', 'SepalWidth', 'PetalLength','PetalWidth', 'Species'] |
87 | | - |
88 | | -# Dataset Info |
89 | | -df.info() |
90 | | - |
91 | | -# Checking for null values |
92 | | -print(df.isnull().sum()) |
93 | | - |
94 | | -# Display column names |
95 | | -print(df.columns) |
96 | | - |
97 | | -#Value counts for Species column |
98 | | -print("Unique Species:", df['Species'].unique()) |
99 | | -print("Duplicate Rows:", df.duplicated().sum()) |
| 73 | +print("Accuracy of the model is {:.2f}%".format(accuracy)) |
| 74 | +print("Confusion Matrix: ") |
| 75 | +print(conf_matrix) |
100 | 76 |
|
101 | | -#Visualize the species count |
102 | | -sns.pairplot(df,hue='Species', palette='husl') |
103 | | -sns.set(style="whitegrid") #seaborn style |
104 | | -plt.title("Pair Plot of Iris DataSet") |
105 | | -plt.show() |
| 77 | +#print Classification report |
| 78 | +print("\nClassification Report:") |
| 79 | +print(classification_report(y_test, y_pred)) |
106 | 80 |
|
| 81 | +#visualize confusion report |
107 | 82 |
|
108 | | -#Heatmaps |
109 | | -plt.figure(figsize=(10,6)) |
110 | | -sns.heatmap(df.drop('Species', axis=1).corr(),annot=True,cmap='coolwarm') |
111 | | -plt.title("Correlation Matrix") |
| 83 | +plt.figure(figsize=(8,6)) |
| 84 | +sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=model.classes_,yticklabels=model.classes_) |
| 85 | +plt.xlabel('Predicted Labels') |
| 86 | +plt.ylabel('True Lables') |
| 87 | +plt.title('Confusion Matrix') |
112 | 88 | plt.show() |
113 | 89 |
|
114 | | -#Box Plot for Outlier |
115 | | -for col in ['SepalLength', 'SepalWidth','PetalLength','PetalWidth']: |
116 | | - sns.boxplot(df[col]) |
117 | | - plt.title(f'Box plot of {col}') |
118 | | - plt.show() |
119 | | - |
120 | | - |
121 | | -#Define x and y |
122 | | - |
123 | | -x=df[['SepalLength', 'SepalWidth','PetalWidth','PetalLength']] #Independent Variables |
124 | | -y=df['Species'] #Dependent Variable |
125 | | - |
126 | | -# Split the dataset into training and testing sets |
127 | | -from sklearn.model_selection import train_test_split |
128 | | -x_train, x_test, y_train, y_test=train_test_split (x,y,random_state=0, test_size=0.2) #20% Test Set |
129 | | - |
130 | | -#Check Shapes |
131 | | - |
132 | | -print("X_train shape:", x_train.shape) |
133 | | -print("X_test shape:", x_test.shape) |
134 | | -print("Y_train shape:", y_train.shape) |
135 | | -print("Y_test shape:", y_test.shape) |
136 | | - |
137 | | - |
138 | 90 | print(df.describe()) |
0 commit comments