Machine Learning courses with 110+ Real-time projects Start Now!!
Program 1
# Suspicious Login Detection Using Logistic Regression
# Detect whether a login attempt is normal or suspicious
# based on parameter like login time, location, device type, and previous failed attempts.
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import tkinter as tk
from tkinter import messagebox
# Load dataset
df = pd.read_csv("login_data.csv")
# Features and label
X = df[['LoginTime', 'LoginLocation', 'DeviceType', 'FailedAttempts']] # Independed
y = df['Suspicious'] # Depended
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# print(len(X_train))
# print(len(y_train))
# print(len(X_test))
# print(len(y_test))
# Train model
model = LogisticRegression()
model.fit(X_train, y_train)
print(model)
#
# # Evaluate
y_pred = model.predict(X_test)
# print(y_pred)
# print("Accuracy:", accuracy_score(y_test, y_pred))
# # Predict single input (Example)
def predict_login():
try:
time = int(entry_time.get())
location = int(entry_location.get())
device = int(entry_device.get())
attempts = int(entry_attempts.get())
features=[[time, location, device, attempts]]
result = model.predict(features)[0]
msg = "Suspicious Login Detected!" if result == 1 else "Login is Normal."
messagebox.showinfo("Prediction Result", msg)
except ValueError:
messagebox.showerror("Error", "Please enter valid integer values.")
app = tk.Tk()
app.title("Suspicious Login Detection")
app.geometry("400x300")
tk.Label(app, text="Login Time (0–23)",font=("Helvetica", 10, "bold")).pack()
entry_time = tk.Entry(app)
entry_time.pack()
tk.Label(app, text="Login Location (1=Known, 0=Unknown)",font=("Helvetica", 10, "bold")).pack()
entry_location = tk.Entry(app)
entry_location.pack()
tk.Label(app, text="Device Type (1=Known, 0=Unknown)",font=("Helvetica", 10, "bold")).pack()
entry_device = tk.Entry(app)
entry_device.pack()
tk.Label(app, text="Failed Attempts",font=("Helvetica", 10, "bold")).pack()
entry_attempts = tk.Entry(app)
entry_attempts.pack()
tk.Button(app, text="Predict", command=predict_login,font=("Arial", 12), bg="blue", fg="white", padx=10, pady=5).pack(pady=10)
app.mainloop()