-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic_regression.py
More file actions
49 lines (38 loc) · 1.65 KB
/
logistic_regression.py
File metadata and controls
49 lines (38 loc) · 1.65 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
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import pandas as pd
#Accuracy: 84.05%
# Load dataset
df = pd.read_csv("dataset.csv")
# Split dataset into training and validation sets
train_texts, val_texts, train_labels, val_labels = train_test_split(
df["column_name"].tolist(), df["is_hipaa_sensitive"].tolist(), test_size=0.2, random_state=42, stratify=df["is_hipaa_sensitive"]
)
# Convert text into TF-IDF features
vectorizer = TfidfVectorizer(ngram_range=(1, 2)) # Unigrams & bigrams for better representation
X_train = vectorizer.fit_transform(train_texts)
X_val = vectorizer.transform(val_texts)
# Train Logistic Regression model
clf = LogisticRegression()
clf.fit(X_train, train_labels)
# Make predictions
pred_labels = clf.predict(X_val)
# Calculate accuracy
accuracy = accuracy_score(val_labels, pred_labels)
print(f"Logistic Regression Accuracy: {accuracy * 100:.4f}%")
# Print classification report
print("\nClassification Report:\n", classification_report(val_labels, pred_labels))
# Function to test new examples
def predict_tf_idf(texts):
X_test = vectorizer.transform(texts)
predictions = clf.predict(X_test)
return ["Sensitive" if p == 1 else "Non-sensitive" for p in predictions]
# Test on new column names
test_texts = ["birthDate", "birth_year", "country", "DATE_BIRTH", "color", "food", "jwtToken"]
predictions = predict_tf_idf(test_texts)
# Print predictions
print("\nPredictions:")
for text, pred in zip(test_texts, predictions):
print(f"{text}: {pred}")