Data Mining Demo

An overview of Data Mining (DM), its techniques, applications, and challenges.

Warning

This article is a work in progress and may contain incomplete information or inaccuracies. Please verify details from reliable sources.

Data Mining Demo

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# ==========================================
# 1. INPUT DATA (Data Collection)
# ==========================================
# Simulate 10 customers' data (in reality, data comes from SQL Database or CSV)
# Assume 1 = Churn, 0 = Stay
data = {
    'Customer_ID': ['C001', 'C002', 'C003', 'C004', 'C005', 'C006', 'C007', 'C008', 'C009', 'C010'],
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Hannah', 'Ian', 'Jack'],
    'Age': [25, 45, 35, 50, 23, 60, 32, 28, 40, 55],
    'Salary': [18000, 85000, 45000, 120000, 15000, 90000, 50000, 22000, 60000, 110000],
    'Days_Since_Last_Buy': [2, 45, 10, 5, 60, 3, 15, 40, 7, 2],
    'Total_Spend': [500, 12000, 3000, 25000, 200, 15000, 4000, 800, 5500, 30000],
    'Churn_Label': [0, 1, 0, 0, 1, 0, 0, 1, 0, 0] 
}

df = pd.DataFrame(data)

print("--- Raw Data ---

")
print(df.head(3)) # View only the first 3 rows
print("\n")

# ==========================================
# 2. DATA PREPROCESSING
# ==========================================

# --- 2.1 Feature Selection (Select only features that matter) ---


# HINT: ID and Name are Unique Identifiers and have no effect on purchasing behavior. Discard them.
# This is the simplest dimension reduction (Manual Feature Selection)
features = df.drop(['Customer_ID', 'Name', 'Churn_Label'], axis=1)
labels = df['Churn_Label']

# --- 2.2 Data Subsetting (Train/Test Split) ---


# HINT: Split data for "teaching" (Train) and "testing" (Test) to simulate real-world usage
# Randomly assign 80% to train, 20% to test
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2, random_state=42)

# --- 2.3 Normalization (Scaling data) ---


# HINT: Salary in tens of thousands vs Age in tens. If not scaled, the model will bias towards Salary
# We use Min-Max Scaler to scale everything to be between 0 and 1
scaler = MinMaxScaler()

# fit_transform on Train data (remembers Min/Max of Train)
X_train_scaled = scaler.fit_transform(X_train) 
# transform Test data (uses the same Min/Max from Train for fairness)
X_test_scaled = scaler.transform(X_test)

print("--- Data after Preprocessing (Normalized) ---

")
print(pd.DataFrame(X_train_scaled, columns=features.columns).head(3))
print("\n")

# ==========================================
# 3. DATA MINING
# ==========================================

# Choose Algorithm: Decision Tree
# Because it's easy to understand and suitable for beginners
clf = DecisionTreeClassifier(random_state=42)

# Make the model "learn" from prepared data (Machine Learning)
clf.fit(X_train_scaled, y_train)

# Try predicting the test set
y_pred = clf.predict(X_test_scaled)

# ==========================================
# 4. POSTPROCESSING & INFORMATION
# ==========================================

# --- 4.1 Evaluation ---


accuracy = accuracy_score(y_test, y_pred)
print(f"--- Output (Information) ---

")
print(f"Model Accuracy: {accuracy * 100:.2f}%")

# --- 4.2 Pattern Interpretation (See what the model knows) ---


# HINT: Look at which Feature is most important in deciding whether the customer will churn or not
feature_importance = pd.DataFrame({
    'Feature': features.columns,
    'Importance': clf.feature_importances_
}).sort_values(by='Importance', ascending=False)

print("\nFactors affecting membership churn (sorted by importance):")
print(feature_importance)

Additional Explanation

1. Data Preprocessing

In this Code, I combined sub-steps in Step 2:

  • Feature Selection: The command df.drop(['Customer_ID', 'Name'...]) tells it "don't consider Name, it's irrelevant". This is Noise reduction.

  • Dimensionality Reduction: In this Code, we do the basic "discarding columns", but advanced methods (in Tan's book) might use PCA to merge Salary + Total_Spend into one variable. However, in basic practice, good Feature Selection is enough.

  • Normalization: The MinMaxScaler command is very important. Notice that Salary (hundreds of thousands) will shrink to 0.x to be on par with Age (tens). If this step is skipped, the model will always consider Salary the most important because the numbers are large.

2. Data Mining vs Algorithm

  • Data Mining is the name of the big picture process.
  • Decision Tree Classifier is the "tool" or Algorithm we pick to use in this step (like picking a drill to make a hole).

3. Postprocessing

  • Numerical results (y_pred) might be hard to read.

  • So we create feature_importance to translate math into business language:

    "Ah... the most important thing is the number of absent days (Days_Since_Last_Buy). If gone for a long time, there's a high chance to churn."

    This is the discovered Knowledge.

Python Code: Visualizing Decision Tree

# ==========================================
# 5. VISUALIZATION (Bonus: Look at the model)
# ==========================================
import matplotlib.pyplot as plt
from sklearn.tree import plot_tree

# Set the image size (width 15 inches, height 10 inches) for clear viewing
plt.figure(figsize=(15, 10))

# Tree drawing command
plot_tree(clf, 
          feature_names=features.columns,  # Add column names for easy reading (Age, Salary)
          class_names=['Stay (0)', 'Churn (1)'], # Name the results (0=Stay, 1=Churn)
          filled=True,     # Color boxes by result group (Blue=Stay, Orange=Churn)
          rounded=True,    # Make rounded edges for beauty
          fontsize=12)     # Font size

plt.title("Decision Tree Logic for Customer Churn Prediction")
plt.show() # Display the image

⚠️ The most important thing in reading this Diagram (Read before running!)

Because in the Preprocessing step we did Normalization (Min-Max Scaling), all data was compressed to between 0 and 1.

Therefore, in the Diagram you will not see conditions like "Age <= 30 years", but you will see conditions like this instead:

Age <= 0.354

Interpretation: This doesn't mean age 0.3 years! It means "the scaled age value that is less than or equal to 0.354".

Reverse calculation example:

  • If minimum value (0.0) = Age 23
  • If maximum value (1.0) = Age 60
  • The value 0.354 in the model ≈ Actual age about 36 years

This is why a Data Scientist must understand Preprocessing very well, because it affects how results are interpreted for the Business side.

Example of Diagram appearance you will get

When run, you will get an image roughly like this (actual shape will change based on data randomization in each run):

Elements of Diagram:

  • Top box (Root Node): This is the first most important question the model uses to split people. E.g. it might ask about "absent days" first.

  • Box color (Filled Color):

    • 🔵 Blue color (cool tone): Tendency is the group that will "Stay"
    • 🟠 Orange color (warm tone): Tendency is the group that will "Churn"
  • gini: This is the mathematical value that tells "impurity" of data in that box.

    • Closer to 0.0 = That box has only purely people of the same group (very good)

AI Knowledge Assistant

สวัสดีครับ! ผมคือ AI Assistant ประจำเว็บไซต์

คุณสามารถสอบถามข้อมูลด้าน Computer Science, Business, หรือ Finance ได้เลยครับ