-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_reg_simplified.py
More file actions
44 lines (35 loc) · 1.29 KB
/
log_reg_simplified.py
File metadata and controls
44 lines (35 loc) · 1.29 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
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# Load and split the digits dataset
# X, y = load_digits(return_X_y=True)
digits = load_digits()
# Separate the data and target into X and y
X = digits.data # Feature matrix (images)
y = digits.target # Labels (0 to 9)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Standardize features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Train and evaluate models
models = {
"SVM": SVC(kernel='linear'),
"Logi_Regre": LogisticRegression(max_iter=10000)
}
accuracies = {}
for name, model in models.items():
model.fit(X_train, y_train)
y_pred= model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
accuracies[name] = accuracy
# Print individual model accuracies
#print(f"SVM Accuracy: {accuracies['SVM']:.4f}")
#print(f"Logistic Regression Accuracy: {accuracies['Logi_Regre']:.4f}")
print("SVM Accuracy: %.4f" % accuracies['SVM'])
print("Logistic Regression Accuracy: %.4f" % accuracies['Logi_Regre'])