xbe.at

Data science

Mean Centering and Min-Max Normalization for User Ratings

8 min read, 16 sections

Contents
  1. Introduction to Data Normalization
  2. Mean Centering: Concept and Implementation
  3. Visualizing Mean Centered Data
  4. Min-Max Scaling: Concept and Implementation
  5. Visualizing Min-Max Scaled Data
  6. Comparing Mean Centering and Min-Max Scaling
  7. Real-Life Example: Movie Ratings
  8. Interpreting Normalized Movie Ratings
  9. Real-Life Example: Product Reviews
  10. Normalizing Product Ratings
  11. Interpreting Normalized Product Ratings
  12. Choosing Between Mean Centering and Min-Max Scaling
  13. Choosing Between Mean Centering and Min-Max Scaling
  14. Practical Tips for Normalizing User Ratings
  15. Practical Tips for Normalizing User Ratings
  16. Additional Resources

Introduction to Data Normalization

Data normalization is a crucial preprocessing step in many machine learning and data analysis tasks. It helps to bring different features or variables to a common scale, which can improve the performance and convergence of various algorithms. In this presentation, we'll focus on two popular normalization techniques: mean centering and min-max scaling, specifically in the context of user ratings.

import numpy as np
import matplotlib.pyplot as plt

# Sample user ratings data
ratings = np.array([3, 5, 2, 4, 1, 5, 3, 4, 2, 5])

plt.figure(figsize=(10, 5))
plt.subplot(121)
plt.bar(range(len(ratings)), ratings)
plt.title("Original Ratings")
plt.subplot(122)
plt.hist(ratings, bins=5)
plt.title("Distribution of Ratings")
plt.tight_layout()
plt.show()

Mean Centering: Concept and Implementation

Mean centering is a normalization technique that subtracts the mean value of a dataset from each data point. This process shifts the distribution of the data to have a mean of zero. For user ratings, mean centering can help remove bias and highlight deviations from the average rating.

def mean_center(data):
    return data - np.mean(data)

centered_ratings = mean_center(ratings)

print("Original ratings:", ratings)
print("Centered ratings:", centered_ratings)
print("Mean of centered ratings:", np.mean(centered_ratings))

Visualizing Mean Centered Data

Let's visualize the effect of mean centering on our user ratings data. We'll create a bar plot comparing the original ratings to the mean-centered ratings.

plt.figure(figsize=(12, 5))
plt.subplot(121)
plt.bar(range(len(ratings)), ratings)
plt.title("Original Ratings")
plt.ylim(0, 6)
plt.subplot(122)
plt.bar(range(len(centered_ratings)), centered_ratings)
plt.title("Mean Centered Ratings")
plt.axhline(y=0, color='r', linestyle='-')
plt.ylim(-3, 3)
plt.tight_layout()
plt.show()

Min-Max Scaling: Concept and Implementation

Min-max scaling, also known as normalization, is a technique that scales the data to a fixed range, typically between 0 and 1. This method preserves the relative differences between data points while bringing them to a common scale. For user ratings, min-max scaling can be useful when you want to maintain the proportional differences between ratings.

def min_max_scale(data):
    return (data - np.min(data)) / (np.max(data) - np.min(data))

scaled_ratings = min_max_scale(ratings)

print("Original ratings:", ratings)
print("Min-max scaled ratings:", scaled_ratings)
print("Min of scaled ratings:", np.min(scaled_ratings))
print("Max of scaled ratings:", np.max(scaled_ratings))

Visualizing Min-Max Scaled Data

Now, let's visualize the effect of min-max scaling on our user ratings data. We'll create a bar plot comparing the original ratings to the min-max scaled ratings.

plt.figure(figsize=(12, 5))
plt.subplot(121)
plt.bar(range(len(ratings)), ratings)
plt.title("Original Ratings")
plt.ylim(0, 6)
plt.subplot(122)
plt.bar(range(len(scaled_ratings)), scaled_ratings)
plt.title("Min-Max Scaled Ratings")
plt.ylim(0, 1)
plt.tight_layout()
plt.show()

Comparing Mean Centering and Min-Max Scaling

Let's compare the effects of mean centering and min-max scaling on the same dataset. This comparison will help us understand the differences between these two normalization techniques and their impact on the data distribution.

plt.figure(figsize=(15, 5))
plt.subplot(131)
plt.bar(range(len(ratings)), ratings)
plt.title("Original Ratings")
plt.ylim(0, 6)
plt.subplot(132)
plt.bar(range(len(centered_ratings)), centered_ratings)
plt.title("Mean Centered Ratings")
plt.axhline(y=0, color='r', linestyle='-')
plt.ylim(-3, 3)
plt.subplot(133)
plt.bar(range(len(scaled_ratings)), scaled_ratings)
plt.title("Min-Max Scaled Ratings")
plt.ylim(0, 1)
plt.tight_layout()
plt.show()

Real-Life Example: Movie Ratings

Consider a movie recommendation system that uses user ratings. We'll demonstrate how mean centering and min-max scaling can be applied to a dataset of movie ratings.

# Sample movie ratings from multiple users
movie_ratings = np.array([
    [4, 3, 5, 2, 1],  # User 1
    [3, 4, 3, 1, 2],  # User 2
    [5, 5, 5, 3, 4],  # User 3
    [2, 1, 3, 4, 5]   # User 4
])

print("Original movie ratings:")
print(movie_ratings)

# Apply mean centering
centered_movie_ratings = np.apply_along_axis(mean_center, 1, movie_ratings)
print("\nMean centered movie ratings:")
print(centered_movie_ratings)

# Apply min-max scaling
scaled_movie_ratings = np.apply_along_axis(min_max_scale, 1, movie_ratings)
print("\nMin-max scaled movie ratings:")
print(scaled_movie_ratings)

Interpreting Normalized Movie Ratings

Let's interpret the results of our movie ratings normalization. Mean centering helps identify which movies each user rated above or below their average rating. Min-max scaling shows the relative preference of each user for the movies they've rated.

def interpret_ratings(original, centered, scaled):
    for i, (orig, cent, scale) in enumerate(zip(original, centered, scaled)):
        print(f"User {i+1}:")
        print(f"  Original ratings: {orig}")
        print(f"  Centered ratings: {cent}")
        print(f"  Scaled ratings:   {scale}")
        print(f"  Interpretation:")
        print(f"    - Highest rated movie: {np.argmax(orig) + 1}")
        print(f"    - Lowest rated movie:  {np.argmin(orig) + 1}")
        print(f"    - Movies above average: {np.where(cent > 0)[0] + 1}")
        print(f"    - Movies below average: {np.where(cent < 0)[0] + 1}")
        print()

interpret_ratings(movie_ratings, centered_movie_ratings, scaled_movie_ratings)

Real-Life Example: Product Reviews

Consider an e-commerce platform that collects product reviews. We'll demonstrate how normalization techniques can be applied to analyze customer satisfaction across different product categories.

# Sample product ratings for different categories
categories = ['Electronics', 'Clothing', 'Books', 'Home & Kitchen']
product_ratings = np.array([
    [4.2, 3.8, 4.5, 4.0],  # Category averages
    [0.8, 1.0, 0.5, 0.7]   # Standard deviations
])

def generate_ratings(mean, std, num_ratings=1000):
    return np.clip(np.random.normal(mean, std, num_ratings), 1, 5)

category_ratings = [generate_ratings(m, s) for m, s in zip(product_ratings[0], product_ratings[1])]

plt.figure(figsize=(12, 6))
plt.boxplot(category_ratings, labels=categories)
plt.title("Product Ratings by Category")
plt.ylabel("Rating")
plt.show()

Normalizing Product Ratings

We'll apply mean centering and min-max scaling to the product ratings to compare customer satisfaction across categories, accounting for different rating scales and biases.

# Calculate overall mean and range
overall_mean = np.mean([np.mean(ratings) for ratings in category_ratings])
overall_min = min([np.min(ratings) for ratings in category_ratings])
overall_max = max([np.max(ratings) for ratings in category_ratings])

# Apply normalization
centered_category_ratings = [ratings - np.mean(ratings) + overall_mean for ratings in category_ratings]
scaled_category_ratings = [(ratings - overall_min) / (overall_max - overall_min) for ratings in category_ratings]

plt.figure(figsize=(15, 5))
plt.subplot(131)
plt.boxplot(category_ratings, labels=categories)
plt.title("Original Ratings")
plt.subplot(132)
plt.boxplot(centered_category_ratings, labels=categories)
plt.title("Mean Centered Ratings")
plt.subplot(133)
plt.boxplot(scaled_category_ratings, labels=categories)
plt.title("Min-Max Scaled Ratings")
plt.tight_layout()
plt.show()

Interpreting Normalized Product Ratings

Let's interpret the results of our product ratings normalization. This analysis can help identify categories that perform better or worse than others, accounting for different rating behaviors across categories.

def interpret_product_ratings(original, centered, scaled):
    for i, category in enumerate(categories):
        orig_mean = np.mean(original[i])
        cent_mean = np.mean(centered[i])
        scale_mean = np.mean(scaled[i])
        
        print(f"{category}:")
        print(f"  Original mean: {orig_mean:.2f}")
        print(f"  Centered mean: {cent_mean:.2f}")
        print(f"  Scaled mean:   {scale_mean:.2f}")
        print(f"  Interpretation:")
        print(f"    - {'Above' if cent_mean > overall_mean else 'Below'} overall average")
        print(f"    - Relative satisfaction: {scale_mean:.2%}")
        print()

interpret_product_ratings(category_ratings, centered_category_ratings, scaled_category_ratings)

Choosing Between Mean Centering and Min-Max Scaling

The choice between mean centering and min-max scaling depends on your specific use case and the characteristics of your data. Here are some considerations:

Mean Centering:

  • Preserves the spread of the data
  • Useful for highlighting deviations from the mean
  • Maintains the unit of measurement
  • Can handle negative values

Min-Max Scaling:

  • Bounds the data within a specific range (usually [0, 1])
  • Preserves zero values
  • Useful when you need a bounded range for your algorithm
  • Can be sensitive to outliers

Choosing Between Mean Centering and Min-Max Scaling

# Demonstrating the effect of outliers on min-max scaling
ratings_with_outlier = np.append(ratings, [10])  # Adding an outlier

plt.figure(figsize=(12, 5))
plt.subplot(121)
plt.bar(range(len(ratings_with_outlier)), min_max_scale(ratings_with_outlier))
plt.title("Min-Max Scaled (With Outlier)")
plt.subplot(122)
plt.bar(range(len(ratings_with_outlier)), mean_center(ratings_with_outlier))
plt.title("Mean Centered (With Outlier)")
plt.tight_layout()
plt.show()

Practical Tips for Normalizing User Ratings

  1. Consider the distribution of your data before choosing a normalization technique.
  2. Be aware of the impact of outliers, especially when using min-max scaling.
  3. For user ratings, mean centering can help remove individual user biases.
  4. Min-max scaling can be useful when you need to compare ratings across different scales.
  5. Always validate your normalization results and their impact on your specific application.

Practical Tips for Normalizing User Ratings

# Demonstrating the impact of normalization on a machine learning model
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Generate sample data
X = np.random.rand(1000, 2) * 10  # Features
y = (X[:, 0] + X[:, 1] > 10).astype(int)  # Target

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train and evaluate without normalization
model_raw = LogisticRegression()
model_raw.fit(X_train, y_train)
y_pred_raw = model_raw.predict(X_test)
accuracy_raw = accuracy_score(y_test, y_pred_raw)

# Train and evaluate with min-max scaling
X_train_scaled = min_max_scale(X_train)
X_test_scaled = min_max_scale(X_test)
model_scaled = LogisticRegression()
model_scaled.fit(X_train_scaled, y_train)
y_pred_scaled = model_scaled.predict(X_test_scaled)
accuracy_scaled = accuracy_score(y_test, y_pred_scaled)

print(f"Accuracy without normalization: {accuracy_raw:.4f}")
print(f"Accuracy with min-max scaling: {accuracy_scaled:.4f}")

Additional Resources

For those interested in diving deeper into data normalization techniques and their applications, here are some recommended resources:

  1. "A Survey on Data Preprocessing Methods and Techniques for Sentiment Analysis" by Akhilesh Kumar Gupta and Syed Imtiyaz Hassan (ArXiv:2103.02572) URL: https://arxiv.org/abs/2103.02572
  2. "Normalization as a Preprocessing Step for Outlier Detection" by Zhenzhou Wang and Tao Li (ArXiv:2011.13220) URL: https://arxiv.org/abs/2011.13220
  3. "Feature Scaling in Support Vector Data Description" by Myungjin Choi, Jaesung Lee, and Changha Hwang (ArXiv:1412.4276) URL: https://arxiv.org/abs/1412.4276

These papers provide in-depth discussions on various normalization techniques, their impact on data analysis, and their applications in machine learning algorithms.

More in Data science

  1. NumPy Broadcasting Simplifying Python Code6 min

    NumPy broadcasting is a powerful mechanism that allows arrays with different shapes to be used in arithmetic operations. It automatically expands arrays to...

  2. Mastering the Melt Function in Pandas6 min

    The melt function in Pandas is a crucial reshaping tool that transforms wide-format data into long-format data. It converts selected columns into rows...

  3. NumPy Fundamentals for TikTok3 min

    Slide 1 (Introduction): NumPy Fundamentals Discover the power of NumPy, the essential library for scientific computing in Python.

  4. Mastering String Manipulation in Pandas6 min

    Pandas provides powerful string manipulation capabilities through its str accessor. This accessor allows you to apply string operations to entire Series or...

  5. NumPy Shortcuts for Efficient Data Analysis6 min

    NumPy is a fundamental library for scientific computing in Python. This guide will walk you through essential NumPy commands and operations, helping you...

  6. Mastering Python Sets! Unlock Powerful Data Manipulation6 min

    Python sets are unordered collections of unique elements, offering powerful tools for data manipulation and mathematical operations. They provide efficient...