-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_classifer.py
More file actions
137 lines (97 loc) · 3.72 KB
/
data_classifer.py
File metadata and controls
137 lines (97 loc) · 3.72 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# =========================================================
# PROJECT 2: DATA CLASSIFICATION USING AI
# DecodeLabs Industrial Training Project
# =========================================================
# Step 1: Import Required Libraries
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
# =========================================================
# Step 2: Load Dataset
# =========================================================
# Loading Iris Dataset
iris = load_iris()
# Creating DataFrame
data = pd.DataFrame(iris.data, columns=iris.feature_names)
# Adding Target Column
data['target'] = iris.target
# Display first 5 rows
print("First 5 Rows of Dataset:\n")
print(data.head())
# =========================================================
# Step 3: Understand Dataset
# =========================================================
print("\nDataset Information:\n")
print(data.info())
print("\nDataset Description:\n")
print(data.describe())
print("\nChecking Missing Values:\n")
print(data.isnull().sum())
# =========================================================
# Step 4: Split Features and Labels
# =========================================================
X = data.iloc[:, :-1] # Features
y = data['target'] # Target labels
# =========================================================
# Step 5: Split Dataset into Training and Testing
# =========================================================
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.2,
random_state=42
)
print("\nTraining Data Shape:", X_train.shape)
print("Testing Data Shape:", X_test.shape)
# =========================================================
# Step 6: Feature Scaling
# =========================================================
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# =========================================================
# Step 7: Apply Classification Algorithm
# =========================================================
# Creating KNN Classifier
model = KNeighborsClassifier(n_neighbors=3)
# Train Model
model.fit(X_train, y_train)
# =========================================================
# Step 8: Make Predictions
# =========================================================
y_pred = model.predict(X_test)
print("\nPredicted Values:\n")
print(y_pred)
# =========================================================
# Step 9: Evaluate Model
# =========================================================
accuracy = accuracy_score(y_test, y_pred)
print("\nModel Accuracy:")
print(accuracy * 100, "%")
print("\nClassification Report:\n")
print(classification_report(y_test, y_pred))
print("\nConfusion Matrix:\n")
print(confusion_matrix(y_test, y_pred))
# =========================================================
# Step 10: Test with New Custom Data
# =========================================================
# Example flower measurements
# [sepal length, sepal width, petal length, petal width]
new_data = [[5.1, 3.5, 1.4, 0.2]]
# Scale new data
new_data = scaler.transform(new_data)
# Predict class
prediction = model.predict(new_data)
print("\nPrediction for New Data:", prediction)
# Convert numeric prediction to flower name
flower_name = iris.target_names[prediction][0]
print("Predicted Flower Name:", flower_name)
# =========================================================
# PROJECT COMPLETED
# =========================================================