python
{
"cells": [
{
"cell_type": "markdown",
"id": "d7e2d4d1",
"metadata": {},
"source": [
"# Fuzzy Logic Analysis of Movie Data\n",
"\n",
"## Introduction\n",
"\n",
"In this notebook, we will perform a fuzzy logic analysis on a dataset of movies. The goal is to classify movies based on their IMDb rating, release year, and the number of votes they have received. We will employ fuzzy logic principles to handle the inherent uncertainty and vagueness in classifying movies into categories such as *Old Classic*, *Modern Hit*, or *Underrated Gem*.\n",
"\n",
"## Dataset Overview\n",
"\n",
"The dataset contains the following columns:\n",
"\n",
"- `title`: The title of the movie.\n",
"- `type`: The type of the content (e.g., movie, series).\n",
"- `genres`: The genres associated with the movie.\n",
"- `releaseYear`: The year the movie was released.\n",
"- `imdbAverageRating`: The average IMDb rating of the movie (0.0 - 10.0).\n",
"- `imdbNumVotes`: The number of votes the movie has received on IMDb (up to 2,961,822).\n",
"- `availableCountries`: The countries where the movie is available.\n",
"\n",
"We will read the data from `./data/data-cleaned.csv`."
]
},
{
"cell_type": "markdown",
"id": "0c6acd7f",
"metadata": {},
"source": [
"## Importing Necessary Libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cdfba8b9",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import skfuzzy as fuzz\n",
"from skfuzzy import control as ctrl\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"id": "89a3c9b6",
"metadata": {},
"source": [
"## Loading the Dataset"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d95d1edc",
"metadata": {},
"outputs": [],
"source": [
"# Load the dataset\n",
"data = pd.read_csv('./data/data-cleaned.csv')\n",
"\n",
"# Display the first few rows\n",
"data.head()"
]
},
{
"cell_type": "markdown",
"id": "e7fc9235",
"metadata": {},
"source": [
"## Defining Fuzzy Variables\n",
"\n",
"We will define fuzzy variables for the following inputs and output:\n",
"\n",
"### Input Variables\n",
"- **IMDb Rating** (`imdb_rating`): Ranges from 0.0 to 10.0.\n",
"- **Release Year** (`release_year`): Ranges from 1900 to 2025.\n",
"- **Number of Votes** (`num_votes`): Ranges from 0 to 3,000,000.\n",
"\n",
"### Output Variable\n",
"- **Movie Quality** (`movie_quality`): A qualitative measure (e.g., *Low*, *Medium*, *High*)."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2256d896",
"metadata": {},
"outputs": [],
"source": [
"# Define the universal variables\n",
"imdb_rating = ctrl.Antecedent(np.arange(0, 10.1, 0.1), 'imdb_rating')\n",
"release_year = ctrl.Antecedent(np.arange(1900, 2026, 1), 'release_year')\n",
"num_votes = ctrl.Antecedent(np.arange(0, 3000001, 10000), 'num_votes')\n",
"movie_quality = ctrl.Consequent(np.arange(0, 11, 1), 'movie_quality')"
]
},
{
"cell_type": "markdown",
"id": "2cfcaf5d",
"metadata": {},
"source": [
"## Membership Functions\n",
"\n",
"We will define the membership functions for each variable. These functions map the crisp input values to degrees of membership between 0 and 1."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "6029b95d",
"metadata": {},
"outputs": [],
"source": [
"# IMDb Rating Membership Functions\n",
"imdb_rating['low'] = fuzz.trimf(imdb_rating.universe, [0, 0, 5])\n",
"imdb_rating['medium'] = fuzz.trimf(imdb_rating.universe, [4, 5.5, 7])\n",
"imdb_rating['high'] = fuzz.trimf(imdb_rating.universe, [6, 10, 10])\n",
"\n",
"# Release Year Membership Functions\n",
"release_year['classic'] = fuzz.trapmf(release_year.universe, [1900, 1900, 1970, 1990])\n",
"release_year['modern'] = fuzz.trapmf(release_year.universe, [1985, 2000, 2010, 2025])\n",
"\n",
"# Number of Votes Membership Functions\n",
"num_votes['few'] = fuzz.trimf(num_votes.universe, [0, 0, 100000])\n",
"num_votes['average'] = fuzz.trimf(num_votes.universe, [50000, 200000, 500000])\n",
"num_votes['many'] = fuzz.trimf(num_votes.universe, [400000, 3000000, 3000000])\n",
"\n",
"# Movie Quality Membership Functions\n",
"movie_quality['low'] = fuzz.trimf(movie_quality.universe, [0, 0, 5])\n",
"movie_quality['medium'] = fuzz.trimf(movie_quality.universe, [4, 5.5, 7])\n",
"movie_quality['high'] = fuzz.trimf(movie_quality.universe, [6, 10, 10])"
]
},
{
"cell_type": "markdown",
"id": "f3f721b1",
"metadata": {},
"source": [
"### Visualizing Membership Functions"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a70edf9f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7f571a6713d0>]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7f571a625cd0>]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7f571a599e20>]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Visualize IMDb Rating Membership Functions\n",
"imdb_rating.view()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "a56c8eb1",
"metadata": {},
"outputs": [],
"source": [
"# Visualize Release Year Membership Functions\n",
"release_year.view()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "a3cd6b65",
"metadata": {},
"outputs": [],
"source": [
"# Visualize Number of Votes Membership Functions\n",
"num_votes.view()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0e9bccfe",
"metadata": {},
"outputs": [],
"source": [
"# Visualize Movie Quality Membership Functions\n",
"movie_quality.view()"
]
},
{
"cell_type": "markdown",
"id": "3958d38c",
"metadata": {},
"source": [
"## Defining Fuzzy Rules\n",
"\n",
"We will define the fuzzy rules to model the decision-making logic. The rules are based on expert knowledge about what constitutes a *High*, *Medium*, or *Low* quality movie."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e3102beb",
"metadata": {},
"outputs": [],
"source": [
"rule1 = ctrl.Rule(imdb_rating['high'] & num_votes['many'], movie_quality['high'])\n",
"rule2 = ctrl.Rule(imdb_rating['medium'] & num_votes['average'], movie_quality['medium'])\n",
"rule3 = ctrl.Rule(imdb_rating['low'] & num_votes['few'], movie_quality['low'])\n",
"rule4 = ctrl.Rule(imdb_rating['high'] & release_year['classic'], movie_quality['high'])\n",
"rule5 = ctrl.Rule(imdb_rating['medium'] & release_year['modern'], movie_quality['medium'])\n",
"rule6 = ctrl.Rule(imdb_rating['low'] | num_votes['few'], movie_quality['low'])"
]
},
{
"cell_type": "markdown",
"id": "3fcf7fa9",
"metadata": {},
"source": [
"## Building the Control System"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "1170c33a",
"metadata": {},
"outputs": [],
"source": [
"movie_quality_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6])\n",
"movie_quality_simulation = ctrl.ControlSystemSimulation(movie_quality_ctrl)"
]
},
{
"cell_type": "markdown",
"id": "ad86e640",
"metadata": {},
"source": [
"## Applying the Fuzzy Model to the Dataset\n",
"\n",
"We will apply the fuzzy model to each movie in the dataset and compute its quality score."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "279540ab",
"metadata": {},
"outputs": [],
"source": [
"# Define a function to compute movie quality\n",
"def compute_movie_quality(row):\n",
" # Set input values\n",
" movie_quality_simulation.input['imdb_rating'] = row['imdbAverageRating']\n",
" movie_quality_simulation.input['release_year'] = row['releaseYear']\n",
" movie_quality_simulation.input['num_votes'] = row['imdbNumVotes']\n",
" \n",
" # Compute the fuzzy result\n",
" movie_quality_simulation.compute()\n",
" \n",
" return movie_quality_simulation.output['movie_quality']\n",
"\n",
"# Apply the function to the dataset\n",
"data['movie_quality_score'] = data.apply(compute_movie_quality, axis=1)"
]
},
{
"cell_type": "markdown",
"id": "5b6ec7b6",
"metadata": {},
"source": [
"## Analyzing the Results"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "11a3e5f7",
"metadata": {},
"outputs": [],
"source": [
"# Display the movies with the highest quality scores\n",
"data.sort_values(by='movie_quality_score', ascending=False).head(10)"
]
},
{
"cell_type": "markdown",
"id": "5ef34d1f",
"metadata": {},
"source": [
"## Visualizing the Movie Quality Distribution"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "783e6f1a",
"metadata": {},
"outputs": [],
"source": [
"# Plot the distribution of movie quality scores\n",
"plt.figure(figsize=(10,6))\n",
"plt.hist(data['movie_quality_score'], bins=20, color='skyblue', edgecolor='black')\n",
"plt.title('Distribution of Movie Quality Scores')\n",
"plt.xlabel('Movie Quality Score')\n",
"plt.ylabel('Number of Movies')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "e47bc537",
"metadata": {},
"source": [
"## Conclusion\n",
"\n",
"Using fuzzy logic, we have created a model to evaluate movies based on their IMDb rating, release year, and number of votes. This approach allows us to handle the uncertainties and subjective judgments inherent in movie quality assessment. The results can aid in identifying movies that are considered high quality according to the defined fuzzy rules."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}