Spaces:
Sleeping
Sleeping
File size: 15,199 Bytes
4aabe49 6220b0b e3d2388 d0e6aa2 a57a561 c900c6b d0e6aa2 a087647 4091ace 6220b0b da142a4 4091ace 6220b0b d0e6aa2 da142a4 d0e6aa2 b91f0fb 7b1a7ea 76674dd d0e6aa2 7b1a7ea d0e6aa2 7b1a7ea d0e6aa2 7b1a7ea d0e6aa2 7b1a7ea d0e6aa2 da142a4 76674dd b91f0fb 76674dd d0e6aa2 0632bea da142a4 d0e6aa2 b91f0fb 7b1a7ea 76674dd d0e6aa2 7b1a7ea d0e6aa2 f4d646d d0e6aa2 7b1a7ea d0e6aa2 7b1a7ea d0e6aa2 7b1a7ea d0e6aa2 7b1a7ea 9fd87e5 d0e6aa2 a57a561 b91f0fb 7b1a7ea 76674dd a57a561 d0e6aa2 a57a561 6a3d6f6 6220b0b a57a561 6220b0b 9fd87e5 a57a561 6220b0b a57a561 da142a4 6220b0b b91f0fb 7b1a7ea 76674dd 6220b0b d0e6aa2 a57a561 6220b0b e3d2388 76674dd da142a4 ba54088 76674dd ba54088 6a3d6f6 9fd87e5 6a3d6f6 7b1a7ea 0632bea c7e58c3 0632bea da142a4 8fa22b4 a087647 0632bea 0320f0b 9fd87e5 0320f0b 9fd87e5 0632bea 0320f0b 655972b 0320f0b 655972b 0320f0b 655972b 0320f0b 655972b b7ae97f 655972b b7ae97f a57a561 655972b 76674dd da142a4 76674dd b91f0fb 76674dd b91f0fb 76674dd b91f0fb 76674dd b91f0fb 76674dd 0632bea 76674dd 7b1a7ea 76674dd 9730dcc f65e0e7 7d3ab38 8fa22b4 7d3ab38 7b1a7ea 7d3ab38 6a3d6f6 7b1a7ea 7d3ab38 6a3d6f6 7b1a7ea 7d3ab38 6a3d6f6 7b1a7ea 7d3ab38 ded7e4c 12e3ab0 6a3d6f6 7d3ab38 12e3ab0 6a3d6f6 f65e0e7 ded7e4c 12e3ab0 f65e0e7 6a3d6f6 7d3ab38 12e3ab0 7d3ab38 12e3ab0 7d3ab38 6a3d6f6 f65e0e7 7b1a7ea 76674dd 4091ace 99f87a3 9fd87e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
import sqlite3
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import streamlit as st
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from collections import Counter
import numpy as np
# Function to load data from SQLite database
@st.cache_data
def load_data(db_file):
conn = sqlite3.connect(db_file)
return conn
# Function to fetch genre movie releases by year
@st.cache_data
def fetch_genre_movie_releases(conn):
query = r'''
SELECT startYear, genres
FROM title_basics
WHERE titleType = 'movie' AND startYear != '\N' AND genres != '\N'
'''
df = pd.read_sql_query(query, conn)
# Split genres and explode to separate rows
df['genres'] = df['genres'].str.split(',')
df = df.explode('genres')
# Convert startYear to numeric
df['startYear'] = pd.to_numeric(df['startYear'])
# Group by startYear and genre, count the number of movies
genre_counts = df.groupby(['startYear', 'genres']).size().reset_index(name='count')
return genre_counts
# Function to fetch data for filled line chart of movie release years
@st.cache_data
def fetch_movie_release_years(conn):
query_release_years = r'''
SELECT startYear, COUNT(*) as count
FROM title_basics
WHERE titleType = 'movie' AND startYear != '\N'
GROUP BY startYear
ORDER BY startYear
'''
df_release_years = pd.read_sql_query(query_release_years, conn)
return df_release_years
# Function to fetch data and create box plot of average rating by first_genre
@st.cache_data
def fetch_and_plot_average_rating_by_genre(conn):
query = r'''
SELECT tb.tconst, tb.primaryTitle, tr.averageRating, tb.genres
FROM title_basics tb
JOIN title_ratings tr ON tb.tconst = tr.tconst
WHERE tb.titleType = 'movie' AND tb.genres IS NOT NULL AND tb.genres != '\N'
'''
df = pd.read_sql_query(query, conn)
# Function to extract the first genre from the genres list
def extract_first_genre(genres):
if genres:
return genres.split(',')[0].strip()
else:
return None
# Apply the function to extract the first genre
df['first_genre'] = df['genres'].apply(extract_first_genre)
# Drop rows where first_genre is None (shouldn't be necessary if genres column is clean)
df = df.dropna(subset=['first_genre'])
# Create a box plot of average rating by first_genre
fig = px.box(df, x='first_genre', y='averageRating',
labels={'first_genre': 'Genre', 'averageRating': 'Average Rating'},
title='Average Rating of Movies by First Genre')
# Update layout for dark theme
fig.update_layout(plot_bgcolor='black', paper_bgcolor='black', font=dict(color='yellow'))
return fig
# Function to create word cloud of genres
def create_genre_wordcloud(conn):
query = r'''
SELECT genres
FROM title_basics
WHERE titleType = 'movie' AND genres IS NOT NULL AND genres != '\N'
'''
df = pd.read_sql_query(query, conn)
# Process genres
genres = df['genres'].str.split(',', expand=True).stack().replace('\\N', pd.NA).dropna().reset_index(drop=True)
genre_counts = Counter(genres)
# Generate the word cloud
wordcloud = WordCloud(width=800, height=400, background_color='black', colormap='inferno').generate_from_frequencies(genre_counts)
# Display the word cloud
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Top Genres in IMDb Dataset', color='yellow') # Set title color to yellow
st.pyplot(plt.gcf()) # Pass the current figure explicitly to st.pyplot()
# Function to find best movie of each genre by numVotes * averageRating
@st.cache_data
def find_best_movies_by_genre(conn):
query = r'''
SELECT tb.tconst, tb.primaryTitle, tb.startYear, tb.genres, tr.averageRating, tr.numVotes
FROM title_basics tb
JOIN title_ratings tr ON tb.tconst = tr.tconst
WHERE tb.titleType = 'movie' AND tb.genres IS NOT NULL AND tb.genres != '\N'
'''
df = pd.read_sql_query(query, conn)
# Split genres and select the first genre for each movie
df['genre'] = df['genres'].str.split(',', expand=True)[0]
# Calculate score based on numVotes * averageRating
df['score'] = df['numVotes'] * df['averageRating']
# Get the best movie (highest score) for each genre
idx = df.groupby('genre')['score'].idxmax()
best_movies_by_genre = df.loc[idx, ['genre', 'primaryTitle', 'startYear', 'averageRating', 'numVotes', 'score']] \
.sort_values(by='score', ascending=False).reset_index(drop=True)
return best_movies_by_genre
# Function to plot stacked area chart of genre movie releases by year using Plotly Express
@st.cache_data
def plot_stacked_genre_movie_releases(genre_counts):
fig = px.area(genre_counts, x='startYear', y='count', color='genres',
title='Stacked Genre Movie Releases by Year',
labels={'startYear': 'Year', 'count': 'Number of Movies', 'genres': 'Genre'},
line_group='genres', # This groups lines by genre
hover_name='genres', # This sets the genre as the hover label
hover_data={'count': ':.0f'}) # Format hover data as integer
fig.update_layout(xaxis_tickmode='linear', # Ensure x-axis ticks are shown in a linear manner
xaxis_range=[2000, 2025], # Adjust x-axis range if needed
plot_bgcolor='black', # Set plot background color to black
paper_bgcolor='black', # Set paper background color to black
font=dict(color='yellow') # Set text color to yellow
)
fig.update_traces(line=dict(color='yellow')) # Set line color for the chart
return fig
# Function to plot global map of total films per region using Plotly Express
@st.cache_data
def plot_global_map():
df = pd.read_csv('movie_region.csv')
# Country code to name mapping
country_mapping = {
'AF': 'Afghanistan', 'AX': 'Åland Islands', 'AL': 'Albania', 'DZ': 'Algeria', 'AS': 'American Samoa',
'AD': 'Andorra', 'AO': 'Angola', 'AI': 'Anguilla', 'AQ': 'Antarctica', 'AG': 'Antigua and Barbuda',
'AR': 'Argentina', 'AM': 'Armenia', 'AW': 'Aruba', 'AU': 'Australia', 'AT': 'Austria',
'AZ': 'Azerbaijan', 'BS': 'Bahamas', 'BH': 'Bahrain', 'BD': 'Bangladesh', 'BB': 'Barbados',
'BY': 'Belarus', 'BE': 'Belgium', 'BZ': 'Belize', 'BJ': 'Benin', 'BM': 'Bermuda',
'BT': 'Bhutan', 'BO': 'Bolivia', 'BA': 'Bosnia and Herzegovina', 'BW': 'Botswana', 'BR': 'Brazil',
'BN': 'Brunei Darussalam', 'BG': 'Bulgaria', 'BF': 'Burkina Faso', 'BI': 'Burundi', 'KH': 'Cambodia',
'CM': 'Cameroon', 'CA': 'Canada', 'CV': 'Cape Verde', 'KY': 'Cayman Islands', 'CF': 'Central African Republic',
'TD': 'Chad', 'CL': 'Chile', 'CN': 'China', 'CO': 'Colombia', 'KM': 'Comoros',
'CG': 'Congo', 'CD': 'Congo, Democratic Republic of the', 'CK': 'Cook Islands', 'CR': 'Costa Rica', 'HR': 'Croatia',
'CU': 'Cuba', 'CY': 'Cyprus', 'CZ': 'Czech Republic', 'DK': 'Denmark', 'DJ': 'Djibouti',
'DM': 'Dominica', 'DO': 'Dominican Republic', 'EC': 'Ecuador', 'EG': 'Egypt', 'SV': 'El Salvador',
'GQ': 'Equatorial Guinea', 'ER': 'Eritrea', 'EE': 'Estonia', 'ET': 'Ethiopia', 'FJ': 'Fiji',
'FI': 'Finland', 'FR': 'France', 'GA': 'Gabon', 'GM': 'Gambia', 'GE': 'Georgia',
'DE': 'Germany', 'GH': 'Ghana', 'GR': 'Greece', 'GL': 'Greenland', 'GD': 'Grenada',
'GU': 'Guam', 'GT': 'Guatemala', 'GN': 'Guinea', 'GW': 'Guinea-Bissau', 'GY': 'Guyana',
'HT': 'Haiti', 'VA': 'Holy See (Vatican City State)', 'HN': 'Honduras', 'HK': 'Hong Kong', 'HU': 'Hungary',
'IS': 'Iceland', 'IN': 'India', 'ID': 'Indonesia', 'IR': 'Iran', 'IQ': 'Iraq',
'IE': 'Ireland', 'IL': 'Israel', 'IT': 'Italy', 'CI': "Cote d'Ivoire", 'JM': 'Jamaica',
'JP': 'Japan', 'JO': 'Jordan', 'KZ': 'Kazakhstan', 'KE': 'Kenya', 'KI': 'Kiribati',
'KP': "Korea, Democratic People's Republic of", 'KR': 'Korea, Republic of', 'KW': 'Kuwait', 'KG': 'Kyrgyzstan', 'LA': "Lao People's Democratic Republic",
'LV': 'Latvia', 'LB': 'Lebanon', 'LS': 'Lesotho', 'LR': 'Liberia', 'LY': 'Libyan Arab Jamahiriya',
'LI': 'Liechtenstein', 'LT': 'Lithuania', 'LU': 'Luxembourg', 'MO': 'Macao', 'MK': 'Macedonia, The Former Yugoslav Republic of',
'MG': 'Madagascar', 'MW': 'Malawi', 'MY': 'Malaysia', 'MV': 'Maldives', 'ML': 'Mali',
'MT': 'Malta', 'MH': 'Marshall Islands', 'MR': 'Mauritania', 'MU': 'Mauritius', 'YT': 'Mayotte',
'MX': 'Mexico', 'FM': 'Micronesia', 'MD': 'Moldova, Republic of', 'MC': 'Monaco', 'MN': 'Mongolia',
'ME': 'Montenegro', 'MS': 'Montserrat', 'MA': 'Morocco', 'MZ': 'Mozambique', 'MM': 'Myanmar',
'NA': 'Namibia', 'NR': 'Nauru', 'NP': 'Nepal', 'NL': 'Netherlands', 'AN': 'Netherlands Antilles',
'NC': 'New Caledonia', 'NZ': 'New Zealand', 'NI': 'Nicaragua', 'NE': 'Niger', 'NG': 'Nigeria',
'NU': 'Niue', 'NF': 'Norfolk Island', 'MP': 'Northern Mariana Islands', 'NO': 'Norway', 'OM': 'Oman',
'PK': 'Pakistan', 'PW': 'Palau', 'PS': 'Palestinian Territory, Occupied', 'PA': 'Panama', 'PG': 'Papua New Guinea',
'PY': 'Paraguay', 'PE': 'Peru', 'PH': 'Philippines', 'PN': 'Pitcairn', 'PL': 'Poland',
'PT': 'Portugal', 'PR': 'Puerto Rico', 'QA': 'Qatar', 'RO': 'Romania', 'RU': 'Russian Federation',
'RW': 'Rwanda', 'RE': 'Reunion', 'BL': 'Saint Barthelemy', 'SH': 'Saint Helena', 'KN': 'Saint Kitts and Nevis',
'LC': 'Saint Lucia', 'MF': 'Saint Martin', 'PM': 'Saint Pierre and Miquelon', 'VC': 'Saint Vincent and the Grenadines', 'WS': 'Samoa',
'SM': 'San Marino', 'ST': 'Sao Tome and Principe', 'SA': 'Saudi Arabia', 'SN': 'Senegal', 'RS': 'Serbia',
'SC': 'Seychelles', 'SL': 'Sierra Leone', 'SG': 'Singapore', 'SK': 'Slovakia', 'SI': 'Slovenia',
'SB': 'Solomon Islands', 'SO': 'Somalia', 'ZA': 'South Africa', 'GS': 'South Georgia and the South Sandwich Islands', 'ES': 'Spain',
'LK': 'Sri Lanka', 'SD': 'Sudan', 'SR': 'Suriname', 'SJ': 'Svalbard and Jan Mayen', 'SZ': 'Swaziland',
'SE': 'Sweden', 'CH': 'Switzerland', 'SY': 'Syrian Arab Republic', 'TW': 'Taiwan', 'TJ': 'Tajikistan',
'TZ': 'Tanzania, United Republic of', 'TH': 'Thailand', 'TL': 'Timor-Leste', 'TG': 'Togo', 'TK': 'Tokelau',
'TO': 'Tonga', 'TT': 'Trinidad and Tobago', 'TN': 'Tunisia', 'TR': 'Turkey', 'TM': 'Turkmenistan',
'TV': 'Tuvalu', 'UG': 'Uganda', 'UA': 'Ukraine', 'AE': 'United Arab Emirates', 'GB': 'United Kingdom',
'US': 'United States', 'UY': 'Uruguay', 'UZ': 'Uzbekistan', 'VU': 'Vanuatu', 'VE': 'Venezuela',
'VN': 'Viet Nam', 'VG': 'Virgin Islands, British', 'VI': 'Virgin Islands, U.S.', 'WF': 'Wallis and Futuna', 'EH': 'Western Sahara',
'YE': 'Yemen', 'ZM': 'Zambia', 'ZW': 'Zimbabwe'
}
# Replace country codes with full country names
df['region'] = df['region'].map(country_mapping)
# Group by country and count the number of films
df_grouped = df.groupby('region').size().reset_index(name='total_films')
# Apply log transformation to handle outliers
df_grouped['log_total_films'] = np.log1p(df_grouped['total_films'])
# Create a choropleth map with the log-transformed data
fig = px.choropleth(
df_grouped,
locations='region',
locationmode='country names',
color='log_total_films',
hover_name='region',
color_continuous_scale='solar', # Change the color scheme here
labels={'log_total_films': 'Total Films'}
)
# Update layout of the map
fig.update_layout(
title={
'text': 'Total Films by Country',
'x': 0.5, # Center the title
'xanchor': 'center',
'yanchor': 'top',
'font': {'color': 'white'} # Set title text color to white
},
geo=dict(
showframe=False,
showcoastlines=False,
projection_type='orthographic' # Change projection to orthographic for a globe appearance
),
plot_bgcolor='black', # Set plot background color to black
paper_bgcolor='black', # Set paper background color to black
font=dict(color='white') # Set text color to white
)
fig.show()
# Function to fetch summary info
@st.cache_data
def fetch_summary_info(conn):
# Fetch total count of movies
query_total_movies = r'''
SELECT COUNT(*) as total_movies
FROM title_basics
WHERE titleType = 'movie'
'''
total_movies = pd.read_sql_query(query_total_movies, conn).iloc[0]['total_movies']
# Fetch total count of years
query_total_years = r'''
SELECT COUNT(DISTINCT startYear) as total_years
FROM title_basics
WHERE titleType = 'movie' AND startYear IS NOT NULL AND startYear != '\N'
'''
total_years = pd.read_sql_query(query_total_years, conn).iloc[0]['total_years']
# Fetch average rating of movies
query_avg_rating = r'''
SELECT AVG(averageRating) as avg_rating
FROM title_ratings
'''
avg_rating = pd.read_sql_query(query_avg_rating, conn).iloc[0]['avg_rating']
return total_movies, total_years, avg_rating
# Main Streamlit app
def run_app():
st.title('IMDb Movie Data Analysis')
# Load data from SQLite database
conn = load_data('imdb_data.db')
genre_counts = fetch_genre_movie_releases(conn)
total_movies, total_years, avg_rating = fetch_summary_info(conn)
# Layout for summary info in three columns
col1, col2, col3 = st.columns(3)
with col1:
st.subheader('Total Movies')
st.metric(label='zzz', value=total_movies)
with col2:
st.subheader('Total Years of Movie Data')
st.metric(label='zzz', value=total_years)
with col3:
st.subheader('Average Movie Rating')
st.metric(label='zzz', value=f'{avg_rating:.2f}')
# Find and display best movies by genre
best_movies_by_genre = find_best_movies_by_genre(conn)
fig_global_map = plot_global_map()
fig_genre_movie_releases = plot_stacked_genre_movie_releases(genre_counts)
# Layout for best movies by genre in two columns
col1, col2 = st.columns(2)
with col1:
st.subheader('Genre Movie Releases by Year')
st.plotly_chart(fig_genre_movie_releases)
with col2:
st.subheader('Global Map of Films')
st.plotly_chart(fig_global_map, use_container_width=True)
fig_avg_rating_by_genre = fetch_and_plot_average_rating_by_genre(conn)
# Layout for Plotly charts in three columns
col1, col2, col3 = st.columns(3)
with col1:
st.subheader('Best Movies by Genre')
st.dataframe(best_movies_by_genre)
with col2:
create_genre_wordcloud(conn)
with col3:
st.subheader('Average Rating by Genre')
st.plotly_chart(fig_avg_rating_by_genre)
# Close database connection
conn.close()
if __name__ == '__main__':
run_app() |