-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprc.py
More file actions
89 lines (65 loc) · 2.3 KB
/
preprc.py
File metadata and controls
89 lines (65 loc) · 2.3 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
# ==============================
# Step 1: Import Libraries
# ==============================
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
# ==============================
# Step 2: Create Dataset
# ==============================
data = {
"Hours_Studied": [5, 2, np.nan, 8],
"Attendance": [80, np.nan, 60, 90],
"Result": ["Pass", "Fail", "Fail", "Pass"]
}
df = pd.DataFrame(data)
print("Original Data:\n", df)
# ==============================
# Step 3: Handle Missing Values
# ==============================
df['Hours_Studied']=df["Hours_Studied"].fillna(df["Hours_Studied"].mean(), inplace=True)
df["Attendance"]=df["Attendance"].fillna(df["Attendance"].mean(), inplace=True)
print("\nAfter Handling Missing Values:\n", df)
# ==============================
# Step 4: Encode Target Variable
# ==============================
le = LabelEncoder()
df["Result"] = le.fit_transform(df["Result"])
print("\nAfter Encoding Target:\n", df)
# ==============================
# Step 5: Feature Scaling
# ==============================
scaler = StandardScaler()
df[["Hours_Studied", "Attendance"]] = scaler.fit_transform(
df[["Hours_Studied", "Attendance"]]
)
print("\nAfter Feature Scaling:\n", df)
# ==============================
# Step 6: Split Data
# ==============================
X = df[["Hours_Studied", "Attendance"]]
y = df["Result"]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42
)
# ==============================
# Step 7: Train Model
# ==============================
model = LogisticRegression()
model.fit(X_train, y_train)
# ==============================
# Step 8: Prediction
# ==============================
y_pred = model.predict(X_test)
print("\nPredicted Values:", y_pred)
print("Actual Values :", y_test.values)
# ==============================
# Step 9: Evaluation
# ==============================
accuracy = accuracy_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
print("\nModel Accuracy:", accuracy)
print("Confusion Matrix:\n", cm)