Skip to content

Instantly share code, notes, and snippets.

@kashaziz
Created December 3, 2023 18:00
Show Gist options
  • Save kashaziz/bbb45644c71d588bef51d3b43c21213e to your computer and use it in GitHub Desktop.
Save kashaziz/bbb45644c71d588bef51d3b43c21213e to your computer and use it in GitHub Desktop.
Using Decision Tree Algorithm to predict Customer Buying pattern
"""
This Python script showcases the application of decision trees in the context of e-commerce data analysis.
"""
# Import necessary libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.tree import export_text
# Generate a sample dataset
data = {
'Time_Spent_on_Site': [10, 15, 5, 20, 8, 12, 18, 7],
'Pages_Viewed': [5, 8, 3, 10, 4, 6, 9, 3],
'Clicked_Purchase': [1, 1, 0, 1, 0, 1, 1, 0]
}
df = pd.DataFrame(data)
# Assume 'Clicked_Purchase' is the target variable, and others are features
X = df[['Time_Spent_on_Site', 'Pages_Viewed']]
y = df['Clicked_Purchase']
# Split the data 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)
# Create a Decision Tree model
model = DecisionTreeClassifier()
# Train the model
model.fit(X_train, y_train)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
# Display the confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
# Display results
print(f'Test Accuracy: {accuracy:.2f}')
print('Confusion Matrix:')
print(conf_matrix)
# Visualize the decision tree rules
tree_rules = export_text(model, feature_names=['Time_Spent_on_Site', 'Pages_Viewed'])
print("Decision Tree Rules:")
print(tree_rules)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment