-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandwritten text generation.py
More file actions
49 lines (40 loc) · 1.6 KB
/
Copy pathhandwritten text generation.py
File metadata and controls
49 lines (40 loc) · 1.6 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
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Sample dataset (replace this with a real dataset
data = {
"customer_id": [1, 2, 3, 4, 5],
"monthly_usage": [10, 50, 5, 70, 20],
"customer_age": [25, 40, 22, 35, 30],
"subscription_length": [12, 24, 6, 36, 18],
"churn": [1, 0, 1, 0, 1] # 1 = Churned, 0 = Retained
}
df = pd.DataFrame(data)
# Features and target variable
X = df.drop(columns=["customer_id", "churn"])
y = df["churn"]
# Split 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)
# Train a classifier
classifier = RandomForestClassifier(n_estimators=100, random_state=42)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Model Accuracy: {accuracy:.2f}")
# Interactive loop for real-time predictions
while True:
try:
usage = float(input("Enter monthly usage: "))
age = int(input("Enter customer age: "))
subscription = int(input("Enter subscription length (months): "))
new_X = np.array([[usage, age, subscription]])
predicted_churn = classifier.predict(new_X)
print(f"Predicted Churn: {'Yes' if predicted_churn[0] == 1 else 'No'}")
except ValueError:
print("Invalid input. Please enter numerical values.")
except KeyboardInterrupt:
print("\nExiting...")
break