-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_classifier.py
More file actions
42 lines (31 loc) · 1.28 KB
/
example_classifier.py
File metadata and controls
42 lines (31 loc) · 1.28 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
import pandas as pd;
import numpy as np;
from sklearn.model_selection import train_test_split;
from sklearn.linear_model import LogisticRegression;
from sklearn.metrics import classification_report, confusion_matrix;
# Load the dataset
data = pd.read_csv('data.csv');
# separate features and target variable
X = data.drop('approved', axis=1);
y = data['approved'];
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42);
# Deal with missing values (if any)
missing_value_replacement=X_train.mean();
X_train.fillna(missing_value_replacement, inplace=True);
X_test.fillna(missing_value_replacement, inplace=True);
# Train the logistic regression model
model = LogisticRegression();
model.fit(X_train,y_train);
# Make predictions on the test set
y_pred = model.predict(X_test);
# Evaluate the model
print("Confusion Matrix:");
print(confusion_matrix(y_test, y_pred));
print("\nClassification Report:");
print(classification_report(y_test, y_pred));
# Evaluate the model using accuracy score
from sklearn.metrics import accuracy_score;
accuracy = accuracy_score(y_test, y_pred);
print(f"\nAccuracy: {accuracy:.2f}");
print(f"{X_test.shape[0]} samples were tested, and the model achieved an accuracy of {accuracy:.2f}.")