tiktok_analytics / pages /04_Case Study:_Gaming_Clips.py
harrychangjr
map
20ef43e
raw
history blame contribute delete
No virus
34.6 kB
import streamlit as st
import pandas as pd
import numpy as np
import datetime
import plotly.express as px
import plotly.graph_objects as go
import statsmodels.api as sm
from millify import millify
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
from st_aggrid import AgGrid
import io
import re
import emoji
from collections import Counter
import openpyxl
from gensim.models import Word2Vec
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.model_selection import train_test_split
import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from collections import Mapping
def plot_chart(data, chart_type, x_var, y_var, z_var=None, show_regression_line=False, show_r_squared=False):
scatter_marker_color = 'green'
regression_line_color = 'red'
if chart_type == "line":
fig = px.line(data, x=x_var, y=y_var)
elif chart_type == "bar":
fig = px.bar(data, x=x_var, y=y_var)
elif chart_type == "scatter":
fig = px.scatter(data, x=x_var, y=y_var, color_discrete_sequence=[scatter_marker_color])
if show_regression_line and x_var != 'Date':
X = data[x_var].values.reshape(-1, 1)
y = data[y_var].values.reshape(-1, 1)
model = LinearRegression().fit(X, y)
y_pred = model.predict(X)
r_squared = r2_score(y, y_pred) # Calculate R-squared value
fig.add_trace(
go.Scatter(x=data[x_var], y=y_pred[:, 0], mode='lines', name='Regression Line', line=dict(color=regression_line_color))
)
# Add R-squared value as a text annotation
fig.add_annotation(
x=data[x_var].max(),
y=y_pred[-1, 0],
text=f"R-squared: {r_squared:.4f}",
showarrow=False,
font=dict(size=14),
bgcolor='rgba(255, 255, 255, 0.8)',
bordercolor='black',
borderwidth=1,
borderpad=4
)
elif chart_type == "heatmap":
fig = px.imshow(data, color_continuous_scale='Inferno')
elif chart_type == "scatter_3d":
if z_var is not None:
fig = px.scatter_3d(data, x=x_var, y=y_var, z=z_var, color=data.columns[0])
else:
st.warning("Please select Z variable for 3D line plot.")
return
elif chart_type == "line_3d":
if z_var is not None:
fig = go.Figure(data=[go.Scatter3d(x=data[x_var], y=data[y_var], z=data[z_var], mode='lines')])
fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var)) # Set the axis name
else:
st.warning("Please select Z variable for 3D line plot.")
return
elif chart_type == "surface_3d":
if z_var is not None:
fig = go.Figure(data=[go.Surface(z=data.values)])
fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var)) # Set the axis name
else:
st.warning("Please select Z variable for 3D line plot.")
return
elif chart_type == "radar":
fig = go.Figure()
for col in data.columns[1:]:
fig.add_trace(go.Scatterpolar(r=data[col], theta=data[x_var], mode='lines', name=col))
fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[data[data.columns[1:]].min().min(), data[data.columns[1:]].max().max()])))
st.plotly_chart(fig)
def plot_radar_chart(data, columns):
df = data[columns]
fig = go.Figure()
for i in range(len(df)):
date_label = data.loc[i, 'Date']
fig.add_trace(go.Scatterpolar(
r=df.loc[i].values,
theta=df.columns,
fill='toself',
name=date_label
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, df.max().max()]
)
),
showlegend=True
)
st.plotly_chart(fig)
# Set page title
st.set_page_config(page_title="Case Study: Gaming Clips - Tiktok Analytics Dashboard", page_icon = "📊", layout = "centered", initial_sidebar_state = "auto")
st.header("Case Study: Gaming Clips")
selected_options = ["Background Information", "Uploaded Datasets", "Analysis"]
selected = st.selectbox("Which section would you like to read?", options = selected_options)
st.write("Current selection:", selected)
if selected == "Background Information":
st.subheader("Background Information")
st.markdown("""
Using my [personal TikTok account](https://tiktok.com/@castle_thad), I uploaded 2-4 videos daily for 7 consecutive days (5 to 11 Jun 2023).
This was done to investigate the virality of gaming TikTok videos, and how different genres, hashtag counts and other variables affected the views and likes of such videos.
In the *Uploaded Datasets" section, you can find the original datasets that were downloaded from the TikTok Analytics dashboard, which is accessed via TikTok on desktop.
In particular, the *Video Posts.xlsx* file was manually cleaned to include other columns such as *On-screen text*, *Text-to-speech* and *Duration (of video) in seconds*.
This is done as an attempt to enhance the analysis of the uploaded videos.
Some factors to consider:
- As this is a personal account, the view count is not high unlike a typical upload that can be found on #fyp, #gaming etc
- Only specific genres were explored, namely first-person shooters and third-person hack and slash games.
- The inability of webscraping such video information using unofficial APIs limit the extent of analysis that can be performed.
More updates to this analysis, as well as other case studies will be included in this application over time, to explore the virality of different types of content that can be found on TikTok.
*Updated 12 Jun 2023*
""")
elif selected == "Uploaded Datasets":
st.subheader("Uploaded Datasets")
last7days = pd.read_excel('gaming/Last 7 days.xlsx')
last28days = pd.read_excel('gaming/Last 28 days.xlsx')
last60days = pd.read_excel('gaming/Last 60 days.xlsx')
totalfollowers = pd.read_excel('gaming/Total followers.xlsx')
trendingvideos = pd.read_excel('gaming/Trending videos.xlsx')
videoposts = pd.read_excel('gaming/Video Posts.xlsx')
#function to convert any dataframe to a csv file
@st.cache_data
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
st.write('Last 7 days.xlsx')
st.write(last7days)
#converting the sample dataframe
csv = convert_df(last7days)
#adding a download button to download csv file
st.download_button(
label="Download data as CSV",
data=csv,
file_name='Last 7 days.csv',
mime='text/csv',
)
st.write('Last 28 days.xlsx')
st.write(last28days)
#converting the sample dataframe
csv = convert_df(last28days)
#adding a download button to download csv file
st.download_button(
label="Download data as CSV",
data=csv,
file_name='Last 28 days.csv',
mime='text/csv',
)
st.write('Last 60 days.xlsx')
st.write(last60days)
#converting the sample dataframe
csv = convert_df(last60days)
#adding a download button to download csv file
st.download_button(
label="Download data as CSV",
data=csv,
file_name='Last 60 days.csv',
mime='text/csv',
)
st.write('Total followers.xlsx')
st.write(totalfollowers)
#converting the sample dataframe
csv = convert_df(totalfollowers)
#adding a download button to download csv file
st.download_button(
label="Download data as CSV",
data=csv,
file_name='Total followers.csv',
mime='text/csv',
)
st.write('Trending videos.xlsx')
st.write(trendingvideos)
#converting the sample dataframe
csv = convert_df(trendingvideos)
#adding a download button to download csv file
st.download_button(
label="Download data as CSV",
data=csv,
file_name='Trending videos.csv',
mime='text/csv',
)
st.write('Video Posts.xlsx')
st.write(videoposts)
#converting the sample dataframe
csv = convert_df(videoposts)
#adding a download button to download csv file
st.download_button(
label="Download data as CSV",
data=csv,
file_name='Video Posts.csv',
mime='text/csv',
)
elif selected == "Analysis":
st.subheader("Analysis")
taba, tabb, tabc = st.tabs(["Overview", "Content", "Summary"])
with taba:
st.write("**Overview**")
data = pd.read_excel('gaming/Last 7 days.xlsx')
st.write(data)
x_var = st.sidebar.selectbox("Select X variable for Last 7 days - Overview", data.columns)
y_var = st.sidebar.selectbox("Select Y variable for Last 7 days - Overview", data.columns)
show_regression_line = False
z_var_options = ["None"] + list(data.columns)
z_var = st.sidebar.selectbox("Select Z variable for 3D charts (if applicable) - Overview", z_var_options)
tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8 = st.tabs(["Line", "Bar", "Scatterplot", "Heatmap",
"3D Scatterplot", "3D Lineplot", "3D Surfaceplot", "Radar chart"])
with tab1:
st.write("Lineplot for 'Last 7 days'")
plot_chart(data, "line", x_var, y_var)
with tab2:
st.write("Barplot for 'Last 7 days'")
plot_chart(data, "bar", x_var, y_var)
with tab3:
st.write("Scatterplot for 'Last 7 days'")
show_regression_line = st.checkbox("Show regression line for Last 7 days scatterplot (does not apply when X = Date)")
plot_chart(data, "scatter", x_var, y_var, show_regression_line=show_regression_line)
with tab4:
st.write("Heatmap for 'Last 7 days'")
plot_chart(data, "heatmap", x_var, y_var)
with tab5:
st.write("3D Scatterplot for 'Last 7 days'")
if z_var != "None":
plot_chart(data, "scatter_3d", x_var, y_var, z_var)
with tab6:
st.write("3D Lineplot for 'Last 7 days'")
if z_var != "None":
plot_chart(data, "line_3d", x_var, y_var, z_var)
with tab7:
st.write("3D Surfaceplot for 'Last 7 days'")
if z_var != "None":
plot_chart(data, "surface_3d", x_var, y_var, z_var)
with tab8:
st.write("Radar chart for 'Last 60 days'")
radar_columns = ['Video views', 'Profile views', 'Likes', 'Comments', 'Shares']
plot_radar_chart(data, radar_columns)
# Add more conditions for other specific file names if needed
with tabb:
st.write("**Content**")
def extract_hashtags(title):
if isinstance(title, (str, bytes)):
return re.findall(r'#\w+', title)
else:
return [] # or raise an exception or perform other error handling
# Remove emojis and hashtags from the title
def clean_title(title):
#title_without_emojis = emoji.get_emoji_regexp().sub(u'', title)
title_without_hashtags = re.sub(r'#\w+', '', title)
return title_without_hashtags.strip()
data = pd.read_excel('gaming/Video Posts.xlsx')
data['Video title'] = data['Video title'].fillna("") # Fill missing values with empty string
data['Video title'] = data['Video title'].astype(str) # Convert to string type
data['Hashtags'] = data['Video title'].apply(extract_hashtags)
#data['Emojis'] = data['Video title'].apply(extract_emojis)
data['Cleaned_title'] = data['Video title'].apply(clean_title)
# Add a new column to store the hashtag count
data['Hashtag_count'] = data['Hashtags'].apply(len)
# Convert the 'post time' column to datetime format
data['Post time'] = pd.to_datetime(data['Post time'])
# Create new columns for 'weekday', 'hour' and 'minute'
data['weekday_posted'] = data['Post time'].dt.day_name()
data['hour_posted'] = data['Post time'].dt.hour
data['min_posted'] = data['Post time'].dt.minute
st.write(data)
options = ["Summary", "Total views", "Total shares", "Total likes", "Total comments", "Number of Hashtags", "Hashtag Performance"]
selected_feature = st.selectbox(label="Select feature", options=options, index=0)
if selected_feature == "Summary":
x_var = st.sidebar.selectbox("Select X variable", data.columns)
y_var = st.sidebar.selectbox("Select Y variable", data.columns)
show_regression_line = False
z_var_options = ["None"] + list(data.columns)
z_var = st.sidebar.selectbox("Select Z variable for 3D charts (if applicable)", z_var_options)
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(["Line", "Bar", "Scatterplot", "Heatmap",
"3D Scatterplot", "3D Lineplot", "3D Surfaceplot"])
with tab1:
st.write("Lineplot")
plot_chart(data, "line", x_var, y_var)
with tab2:
st.write("Barplot")
plot_chart(data, "bar", x_var, y_var)
with tab3:
st.write("Scatterplot")
show_regression_line = st.checkbox("Show regression line for scatterplot")
plot_chart(data, "scatter", x_var, y_var, show_regression_line=show_regression_line)
with tab4:
st.write("Heatmap")
plot_chart(data, "heatmap", x_var, y_var)
with tab5:
st.write("3D Scatterplot")
if z_var != "None":
plot_chart(data, "scatter_3d", x_var, y_var, z_var)
with tab6:
st.write("3D Lineplot")
if z_var != "None":
plot_chart(data, "line_3d", x_var, y_var, z_var)
with tab7:
st.write("3D Surfaceplot")
if z_var != "None":
plot_chart(data, "surface_3d", x_var, y_var, z_var)
elif selected_feature == "Total views":
data = data.sort_values(by='Total views', ascending=True)
fig = px.bar(data, x='Total views', y='Cleaned_title', title='Views of trending videos for the week',
color_discrete_sequence=px.colors.qualitative.Alphabet, hover_data={'Total views': ':.2f'})
st.plotly_chart(fig)
elif selected_feature == "Total shares":
data = data.sort_values(by='Total shares\xa0', ascending=True)
fig = px.bar(data, x='Total shares\xa0', y='Cleaned_title', title='Shares of trending videos for the week',
color_discrete_sequence=px.colors.qualitative.Set1, hover_data={'Total shares\xa0': ':.2f'})
st.plotly_chart(fig)
elif selected_feature == "Total likes":
data = data.sort_values(by='Total likes', ascending=True)
fig = px.bar(data, x='Total likes', y='Cleaned_title', title='Likes of trending videos for the week',
color_discrete_sequence=px.colors.qualitative.Antique, hover_data={'Total likes': ':.2f'})
st.plotly_chart(fig)
elif selected_feature == "Total comments":
data = data.sort_values(by='Total comments', ascending=True)
fig = px.bar(data, x='Total comments', y='Cleaned_title', title='Comments of trending videos for the week',
color_discrete_sequence=px.colors.qualitative.Vivid, hover_data={'Total comments': ':.2f'})
st.plotly_chart(fig)
elif selected_feature == "Number of Hashtags":
# Count the occurrences of each hashtag
hashtag_counts = Counter(hashtag for hashtags in data['Hashtags'] for hashtag in hashtags)
# Get the top N most common hashtags
N = 10
top_hashtags = hashtag_counts.most_common(N)
# Display the top hashtags
print(f"Top {N} hashtags:")
for hashtag, count in top_hashtags:
print(f"{hashtag}: {count}")
# Visualize the results with a Plotly bar chart
fig = go.Figure(go.Bar(
x=[t[0] for t in top_hashtags],
y=[t[1] for t in top_hashtags],
text=[t[1] for t in top_hashtags],
textposition='auto',
marker_color='rgba(58, 71, 80, 0.6)',
opacity=0.8
))
fig.update_layout(
title=f'Top {N} Hashtags',
xaxis_title='Hashtags',
yaxis_title='Count',
xaxis_tickangle=-45
)
st.plotly_chart(fig)
tab1, tab2, tab3, tab4 = st.tabs(["vs Views", "vs Shares", "vs Likes", "vs Comments"])
with tab1:
fig = px.scatter(data, x='Hashtag_count', y='Total views', hover_data=['Cleaned_title'])
fig.update_layout(
title='Hashtag Count vs. Views',
xaxis_title='Hashtag Count',
yaxis_title='Views'
)
st.plotly_chart(fig)
with tab2:
fig = px.scatter(data, x='Hashtag_count', y='Total shares\xa0', hover_data=['Cleaned_title'])
fig.update_layout(
title='Hashtag Count vs. Shares',
xaxis_title='Hashtag Count',
yaxis_title='Shares'
)
st.plotly_chart(fig)
with tab3:
fig = px.scatter(data, x='Hashtag_count', y='Total likes', hover_data=['Cleaned_title'])
fig.update_layout(
title='Hashtag Count vs. Likes',
xaxis_title='Hashtag Count',
yaxis_title='Likes'
)
st.plotly_chart(fig)
with tab4:
fig = px.scatter(data, x='Hashtag_count', y='Total comments', hover_data=['Cleaned_title'])
fig.update_layout(
title='Hashtag Count vs. Comments',
xaxis_title='Hashtag Count',
yaxis_title='Comments'
)
st.plotly_chart(fig)
elif selected_feature == "Hashtag Performance":
# Tokenize hashtags and create a list of unique hashtags
tokenized_hashtags = data["Hashtags"].tolist()
unique_hashtags = list(set([tag for tags in tokenized_hashtags for tag in tags]))
# Train a word2vec model
model = Word2Vec(tokenized_hashtags, size=50, window=5, min_count=1, workers=4)
# Create a hashtag vector dictionary
hashtag_vectors = {tag: model.wv[tag] for tag in unique_hashtags}
st.subheader("Explaining the concept of hashtag performance scores and cosine similarity scores")
st.markdown(
"""
So how are the **performance scores** calculated for each feature?
In each line, the code goes through each unique hashtag and selects all videos that use that hashtag. It then calculates the mean of the respective performance metric (views, shares, likes, or comments) for those videos.
This gives an average performance score for each hashtag, which can be used as an indication of how well videos with that hashtag tend to perform on average. However, this is a simplistic metric and there may be other factors influencing the performance of a video. It's also worth noting that the mean is sensitive to extreme values, so a few very popular or unpopular videos could skew the average performance for a given hashtag.
""")
st.markdown("""
How about **cosine similarity**?
Cosine similarity is a metric used to measure how similar two vectors are, irrespective of their size. In the context of Natural Language Processing (NLP), and in this case, it's used to measure the semantic similarity between two hashtags based on their embeddings (vectors) generated by the Word2Vec model.
In simple terms, it measures the cosine of the angle between two vectors. If the vectors are identical, the angle is 0, so the cosine is 1, indicating perfect similarity. If the vectors are orthogonal (i.e., the angle between them is 90 degrees), they're considered not similar, and the cosine similarity is 0. If the vectors point in opposite directions (i.e., the angle is 180 degrees), the cosine similarity is -1, indicating that they're diametrically dissimilar.
""")
# Calculate the average performance of each hashtag - views
hashtag_performance_views = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total views"].mean() for tag in unique_hashtags}
# Calculate the average performance of each hashtag - shares
hashtag_performance_shares = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total shares\xa0"].mean() for tag in unique_hashtags}
# Calculate the average performance of each hashtag - likes
hashtag_performance_likes = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total likes"].mean() for tag in unique_hashtags}
# Calculate the average performance of each hashtag - comments
hashtag_performance_comments = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total comments"].mean() for tag in unique_hashtags}
# Calculate the similarity between hashtags
similarity_matrix = cosine_similarity(list(hashtag_vectors.values()))
# Convert the similarity matrix into a DataFrame
similarity_df = pd.DataFrame(similarity_matrix, index=unique_hashtags, columns=unique_hashtags)
# Convert the performance dictionaries into DataFrames
perf_views_df = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "views"])
perf_shares_df = pd.DataFrame(list(hashtag_performance_shares.items()), columns=["hashtag", "shares"])
perf_likes_df = pd.DataFrame(list(hashtag_performance_likes.items()), columns=["hashtag", "likes"])
perf_comments_df = pd.DataFrame(list(hashtag_performance_comments.items()), columns=["hashtag", "comments"])
# Merge the performance DataFrames into a single DataFrame
perf_df = pd.merge(perf_views_df, perf_shares_df, on="hashtag")
perf_df = pd.merge(perf_df, perf_likes_df, on="hashtag")
perf_df = pd.merge(perf_df, perf_comments_df, on="hashtag")
# Convert the similarity matrix into a 1D series
similarity_series = similarity_df.unstack()
# Rename the series index
similarity_series.index.rename(["hashtag1", "hashtag2"], inplace=True)
# Convert the series into a DataFrame
similarity_df = similarity_series.to_frame("similarity").reset_index()
# Merge the similarity DataFrame with the performance DataFrame
merged_df = pd.merge(similarity_df, perf_df, left_on="hashtag1", right_on="hashtag")
# Calculate the correlation between hashtag similarity and performance
correlation = merged_df[["similarity", "views", "shares", "likes", "comments"]].corr()
st.subheader("Correlation matrix between hashtag cosine similarity values and performance values")
#st.write(correlation)
# Create a heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(correlation, annot=True, fmt=".2f", cmap='coolwarm', cbar=True)
# Show the plot in Streamlit
st.pyplot(plt)
# Rename the 'value' columns to make them unique
df1 = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "value"])
df2 = pd.DataFrame(list(hashtag_performance_shares.items()), columns=["hashtag", "value"])
df3 = pd.DataFrame(list(hashtag_performance_likes.items()), columns=["hashtag", "value"])
df4 = pd.DataFrame(list(hashtag_performance_comments.items()), columns=["hashtag", "value"])
df1.rename(columns={'value': 'value_views'}, inplace=True)
df2.rename(columns={'value': 'value_shares'}, inplace=True)
df3.rename(columns={'value': 'value_likes'}, inplace=True)
df4.rename(columns={'value': 'value_comments'}, inplace=True)
# Merge the DataFrames on the 'hashtag' column
merged_df = df1.merge(df2, on='hashtag').merge(df3, on='hashtag').merge(df4, on='hashtag')
st.subheader("Hashtag Performance Scores based on Views, Shares, Likes and Comments - Calculated using average of all videos' metrics containing a particular hashtag")
st.write(merged_df)
# Create a pair plot with regression lines
st.write("**Pair Plots**")
sns.pairplot(merged_df, kind="reg", diag_kind="kde")
st.pyplot(plt)
# Split the data into train and test sets
X = merged_df[["value_shares", "value_likes", "value_comments"]]
y = merged_df["value_views"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Fit the models on the training data
lr_model = LinearRegression()
lr_model.fit(X_train, y_train)
rf_model = RandomForestRegressor()
rf_model.fit(X_train, y_train)
xgb_model = XGBRegressor()
xgb_model.fit(X_train, y_train)
# Make predictions on the testing data using the trained models
lr_pred = lr_model.predict(X_test)
rf_pred = rf_model.predict(X_test)
xgb_pred = xgb_model.predict(X_test)
# Calculate the evaluation metrics for each model
st.write("**Training regression models to predict value_views using value_shares, value_likes and value_comments**")
models = ["Linear Regression", "Random Forest", "XGBoost"]
predictions = [lr_pred, rf_pred, xgb_pred]
# Initialize a list to hold the model metrics
model_metrics = []
for model, pred in zip(models, predictions):
mse = mean_squared_error(y_test, pred)
mae = mean_absolute_error(y_test, pred)
r2 = r2_score(y_test, pred)
# Append a dictionary of the metrics to the list
model_metrics.append({"Model": model, "Mean Squared Error": mse, "Mean Absolute Error": mae, "R^2 Score": r2})
# Convert the list of dictionaries into a DataFrame
metrics_df = pd.DataFrame(model_metrics)
# Display the DataFrame in Streamlit
st.write(metrics_df)
x_var = st.sidebar.selectbox("Select X variable - Content", merged_df.columns)
y_var = st.sidebar.selectbox("Select Y variable - Content", merged_df.columns)
show_regression_line = False
z_var_options = ["None"] + list(merged_df.columns)
z_var = st.sidebar.selectbox("Select Z variable for 3D charts (if applicable) - Content", z_var_options)
st.subheader("Various plots to represent performance scores for views, shares, likes and comments")
tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(["Line", "Bar", "Scatterplot", "Heatmap",
"3D Scatterplot", "3D Lineplot", "3D Surfaceplot"])
with tab1:
st.write("Lineplot")
plot_chart(merged_df, "line", x_var, y_var)
with tab2:
st.write("Barplot")
plot_chart(merged_df, "bar", x_var, y_var)
with tab3:
st.write("Scatterplot")
show_regression_line = st.checkbox("Show regression line")
plot_chart(merged_df, "scatter", x_var, y_var, show_regression_line=show_regression_line)
with tab4:
st.write("Heatmap")
plot_chart(merged_df, "heatmap", x_var, y_var)
with tab5:
st.write("3D Scatterplot")
if z_var != "None":
plot_chart(merged_df, "scatter_3d", x_var, y_var, z_var)
with tab6:
st.write("3D Lineplot")
if z_var != "None":
plot_chart(merged_df, "line_3d", x_var, y_var, z_var)
with tab7:
st.write("3D Surfaceplot")
if z_var != "None":
plot_chart(merged_df, "surface_3d", x_var, y_var, z_var)
tab1, tab2, tab3, tab4 = st.tabs(["vs Views", "vs Shares", "vs Likes", "vs Comments"])
with tab1:
st.subheader("Hashtag Performance - Views:")
# Sort the DataFrame by the 'value' column in descending order
sorted_df1 = df1.sort_values(by="value_views", ascending=False)
st.write(sorted_df1)
# Highlight specific bars (use 'rgba' values for transparency)
highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
sorted_df1['color'] = sorted_df1['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'red')
fig = px.bar(sorted_df1, x='hashtag', y='value_views', title='Hashtag performance for the week',
color='color', color_discrete_map='identity', hover_data={'value_views': ':.2f'})
fig.update_layout(title='Hashtag performance for the week - Views', xaxis_title='Hashtag', yaxis_title='Value')
st.plotly_chart(fig)
with tab2:
st.subheader("Hashtag Performance - Shares:")
# Sort the DataFrame by the 'value' column in descending order
sorted_df2 = df2.sort_values(by="value_shares", ascending=False)
st.write(sorted_df2)
# Highlight specific bars (use 'rgba' values for transparency)
highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
sorted_df2['color'] = sorted_df2['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'blue')
fig = px.bar(sorted_df2, x='hashtag', y='value_shares', title='Hashtag performance for the week',
color='color', color_discrete_map='identity', hover_data={'value_shares': ':.2f'})
fig.update_layout(title='Hashtag performance for the week - Shares', xaxis_title='Hashtag', yaxis_title='Value')
st.plotly_chart(fig)
with tab3:
st.subheader("Hashtag Performance - Likes:")
# Sort the DataFrame by the 'value' column in descending order
sorted_df3 = df3.sort_values(by="value_likes", ascending=False)
st.write(sorted_df3)
# Highlight specific bars (use 'rgba' values for transparency)
highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
sorted_df3['color'] = sorted_df3['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'green')
fig = px.bar(sorted_df3, x='hashtag', y='value_likes', title='Hashtag performance for the week',
color='color', color_discrete_map='identity', hover_data={'value_likes': ':.2f'})
fig.update_layout(title='Hashtag performance for the week - Likes', xaxis_title='Hashtag', yaxis_title='Value')
st.plotly_chart(fig)
with tab4:
st.subheader("Hashtag Performance - Comments:")
# Sort the DataFrame by the 'value' column in descending order
sorted_df4 = df4.sort_values(by="value_comments", ascending=False)
st.write(sorted_df4)
# Highlight specific bars (use 'rgba' values for transparency)
highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
sorted_df4['color'] = sorted_df4['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'orange')
fig = px.bar(sorted_df4, x='hashtag', y='value_comments', title='Hashtag performance for the week',
color='color', color_discrete_map='identity', hover_data={'value_comments': ':.2f'})
fig.update_layout(title='Hashtag performance for the week - Comments', xaxis_title='Hashtag', yaxis_title='Value')
st.plotly_chart(fig)
with tabc:
st.write("**Summary**")
st.markdown("""
- Jun 7 (Wed) had the most number of combined views, shares, likes - presumably due to the Black Ops 3: Gun Game at Nuketown video
- To optimise the number of views, posting a video of ~50s seems to be most ideal
- Although frankly speaking, there is close to negligible correlation (R-squared = 0.0049) between view count and video duration
- Having too many hashtags may not necessarily translate to more views. The best performing video only had 5 hashtags
- There doesn't seem to be a fixed time of the day to maximise the virality of the video. The top 4 videos which have similar view counts were spread out throughout the day
Next steps:
- Performing NLP and/or sentiment analysis to explore how on-screen text affects video viewership
- Investigating relationship between video title, on-screen text and views
""")