CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
veeralakrishna

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: veeralakrishna/DataCamp-Project-Solutions-Python
Path: blob/master/Disney Movies and Box Office Success/notebook.ipynb
Views: 1229
Kernel: Python 3

1. The dataset

Walt Disney Studios is the foundation on which The Walt Disney Company was built. The Studios has produced more than 600 films since their debut film, Snow White and the Seven Dwarfs in 1937. While many of its films were big hits, some of them were not. In this notebook, we will explore a dataset of Disney movies and analyze what contributes to the success of Disney movies.

First, we will take a look at the Disney data compiled by Kelly Garrett. The data contains 579 Disney movies with six features: movie title, release date, genre, MPAA rating, total gross, and inflation-adjusted gross.

Let's load the file and see what the data looks like.

# Import pandas library import pandas as pd # Read the file into gross gross = pd.read_csv("datasets/disney_movies_total_gross.csv", parse_dates=["release_date"]) # Print out gross gross.head()

2. Top ten movies at the box office

Let's started by exploring the data. We will check which are the 10 Disney movies that have earned the most at the box office. We can do this by sorting movies by their inflation-adjusted gross (we will call it adjusted gross from this point onward).

# Sort data by the adjusted gross in descending order inflation_adjusted_gross_desc = gross.sort_values(by='inflation_adjusted_gross', ascending=False) # Display the top 10 movies inflation_adjusted_gross_desc.head(10)

3. Movie genre trend

From the top 10 movies above, it seems that some genres are more popular than others. So, we will check which genres are growing stronger in popularity. To do this, we will group movies by genre and then by year to see the adjusted gross of each genre in each year.

# Extract year from release_date and store it in a new column gross['release_year'] = pd.DatetimeIndex(gross["release_date"]).year # Compute mean of adjusted gross per genre and per year group = gross.groupby(['genre','release_year']).mean() # Convert the GroupBy object to a DataFrame genre_yearly = group.reset_index() # Inspect genre_yearly genre_yearly.head(10)

4. Visualize the genre popularity trend

We will make a plot out of these means of groups to better see how box office revenues have changed over time.

# Import seaborn library import seaborn as sns # Plot the data sns.relplot(x='release_year', y='inflation_adjusted_gross', kind='line', hue='genre', data=genre_yearly)
<seaborn.axisgrid.FacetGrid at 0x7f2ae7f1ce10>

5. Data transformation

The line plot supports our belief that some genres are growing faster in popularity than others. For Disney movies, Action and Adventure genres are growing the fastest. Next, we will build a linear regression model to understand the relationship between genre and box office gross.

Since linear regression requires numerical variables and the genre variable is a categorical variable, we'll use a technique called one-hot encoding to convert the categorical variables to numerical. This technique transforms each category value into a new column and assigns a 1 or 0 to the column.

For this dataset, there will be 11 dummy variables, one for each genre except the action genre which we will use as a baseline. For example, if a movie is an adventure movie, like The Lion King, the adventure variable will be 1 and other dummy variables will be 0. Since the action genre is our baseline, if a movie is an action movie, such as The Avengers, all dummy variables will be 0.

# Convert genre variable to dummy variables genre_dummies = pd.get_dummies(data=gross['genre'], drop_first=True) # Inspect genre_dummies genre_dummies.head()

6. The genre effect

Now that we have dummy variables, we can build a linear regression model to predict the adjusted gross using these dummy variables.

From the regression model, we can check the effect of each genre by looking at its coefficient given in units of box office gross dollars. We will focus on the impact of action and adventure genres here. (Note that the intercept and the first coefficient values represent the effect of action and adventure genres respectively). We expect that movies like the Lion King or Star Wars would perform better for box office.

# Import LinearRegression from sklearn.linear_model import LinearRegression # Build a linear regression model regr = LinearRegression() # Fit regr to the dataset regr.fit(genre_dummies, gross["inflation_adjusted_gross"]) # Get estimated intercept and coefficient values action = regr.intercept_ adventure = regr.coef_[[0]][0] # Inspect the estimated intercept and coefficient values print((action, adventure))
(102921757.36842033, 87475654.70909958)

7. Confidence intervals for regression parameters (i)

Next, we will compute 95% confidence intervals for the intercept and coefficients. The 95% confidence intervals for the intercept a and coefficient bi means that the intervals have a probability of 95% to contain the true value a and coefficient bi respectively. If there is a significant relationship between a given genre and the adjusted gross, the confidence interval of its coefficient should exclude 0.

We will calculate the confidence intervals using the pairs bootstrap method.

# Import a module import numpy as np # Create an array of indices to sample from inds = np.arange(len(gross['genre'])) # Initialize 500 replicate arrays size = 500 bs_action_reps = np.empty(size) bs_adventure_reps = np.empty(size)

8. Confidence intervals for regression parameters (ii)

After the initialization, we will perform pair bootstrap estimates for the regression parameters. Note that we will draw a sample from a set of (genre, adjusted gross) data where the genre is the original genre variable. We will perform one-hot encoding after that.

# Generate replicates for i in range(size): # Resample the indices bs_inds = np.random.choice(inds, size=len(inds)) # Get the sampled genre and sampled adjusted gross bs_genre = gross['genre'][bs_inds] bs_gross = gross['inflation_adjusted_gross'][bs_inds] # Convert sampled genre to dummy variables bs_dummies = pd.get_dummies(data=gross['genre'] , drop_first=True) # Build and fit a regression model regr = LinearRegression().fit(bs_dummies, bs_gross) # Compute replicates of estimated intercept and coefficient bs_action_reps[i] = regr.intercept_ bs_adventure_reps[i] = regr.coef_[[0]][0]

9. Confidence intervals for regression parameters (iii)

Finally, we compute 95% confidence intervals for the intercept and coefficient and examine if they exclude 0. If one of them (or both) does, then it is unlikely that the value is 0 and we can conclude that there is a significant relationship between that genre and the adjusted gross.

# Compute 95% confidence intervals for intercept and coefficient values confidence_interval_action = np.percentile(bs_action_reps, [2.5, 97.5]) confidence_interval_adventure = np.percentile(bs_adventure_reps, [2.5, 97.5]) # Inspect the confidence intervals print(confidence_interval_action) print(confidence_interval_adventure)
[7.04284578e+07 2.23480716e+08] [-1.14853915e+08 8.54028713e+07]

10. Should Disney make more action and adventure movies?

The confidence intervals from the bootstrap method for the intercept and coefficient do not contain the value zero, as we have already seen that lower and upper bounds of both confidence intervals are positive. These tell us that it is likely that the adjusted gross is significantly correlated with the action and adventure genres.

From the results of the bootstrap analysis and the trend plot we have done earlier, we could say that Disney movies with plots that fit into the action and adventure genre, according to our data, tend to do better in terms of adjusted gross than other genres. So we could expect more Marvel, Star Wars, and live-action movies in the upcoming years!

# should Disney studios make more action and adventure movies? more_action_adventure_movies = True