harrychangjr commited on
Commit
d08c099
1 Parent(s): b399bc7

analysis content initial

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
gaming/Video Posts.xlsx CHANGED
Binary files a/gaming/Video Posts.xlsx and b/gaming/Video Posts.xlsx differ
 
gaming/~$Video Posts.xlsx ADDED
Binary file (165 Bytes). View file
 
pages/04_Case Study:_Gaming_Clips.py CHANGED
@@ -25,6 +25,102 @@ import seaborn as sns
25
  import matplotlib.pyplot as plt
26
  from mpl_toolkits.mplot3d import Axes3D
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # Set page title
30
  st.set_page_config(page_title="Case Study: Gaming Clips - Tiktok Analytics Dashboard", page_icon = "📊", layout = "centered", initial_sidebar_state = "auto")
@@ -124,106 +220,13 @@ elif selected == "Uploaded Datasets":
124
  mime='text/csv',
125
  )
126
  elif selected == "Analysis":
127
- def plot_chart(data, chart_type, x_var, y_var, z_var=None, show_regression_line=False, show_r_squared=False):
128
- scatter_marker_color = 'green'
129
- regression_line_color = 'red'
130
- if chart_type == "line":
131
- fig = px.line(data, x=x_var, y=y_var)
132
-
133
- elif chart_type == "bar":
134
- fig = px.bar(data, x=x_var, y=y_var)
135
-
136
- elif chart_type == "scatter":
137
- fig = px.scatter(data, x=x_var, y=y_var, color_discrete_sequence=[scatter_marker_color])
138
-
139
- if show_regression_line and x_var != 'Date':
140
- X = data[x_var].values.reshape(-1, 1)
141
- y = data[y_var].values.reshape(-1, 1)
142
- model = LinearRegression().fit(X, y)
143
- y_pred = model.predict(X)
144
- r_squared = r2_score(y, y_pred) # Calculate R-squared value
145
-
146
- fig.add_trace(
147
- go.Scatter(x=data[x_var], y=y_pred[:, 0], mode='lines', name='Regression Line', line=dict(color=regression_line_color))
148
- )
149
-
150
- # Add R-squared value as a text annotation
151
- fig.add_annotation(
152
- x=data[x_var].max(),
153
- y=y_pred[-1, 0],
154
- text=f"R-squared: {r_squared:.4f}",
155
- showarrow=False,
156
- font=dict(size=14),
157
- bgcolor='rgba(255, 255, 255, 0.8)',
158
- bordercolor='black',
159
- borderwidth=1,
160
- borderpad=4
161
- )
162
-
163
- elif chart_type == "heatmap":
164
- fig = px.imshow(data, color_continuous_scale='Inferno')
165
-
166
- elif chart_type == "scatter_3d":
167
- if z_var is not None:
168
- fig = px.scatter_3d(data, x=x_var, y=y_var, z=z_var, color=data.columns[0])
169
- else:
170
- st.warning("Please select Z variable for 3D line plot.")
171
- return
172
-
173
- elif chart_type == "line_3d":
174
- if z_var is not None:
175
- fig = go.Figure(data=[go.Scatter3d(x=data[x_var], y=data[y_var], z=data[z_var], mode='lines')])
176
- fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var)) # Set the axis name
177
- else:
178
- st.warning("Please select Z variable for 3D line plot.")
179
- return
180
-
181
- elif chart_type == "surface_3d":
182
- if z_var is not None:
183
- fig = go.Figure(data=[go.Surface(z=data.values)])
184
- fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var)) # Set the axis name
185
- else:
186
- st.warning("Please select Z variable for 3D line plot.")
187
- return
188
-
189
- elif chart_type == "radar":
190
- fig = go.Figure()
191
- for col in data.columns[1:]:
192
- fig.add_trace(go.Scatterpolar(r=data[col], theta=data[x_var], mode='lines', name=col))
193
- fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[data[data.columns[1:]].min().min(), data[data.columns[1:]].max().max()])))
194
-
195
- st.plotly_chart(fig)
196
-
197
- def plot_radar_chart(data, columns):
198
- df = data[columns]
199
- fig = go.Figure()
200
-
201
- for i in range(len(df)):
202
- date_label = data.loc[i, 'Date']
203
- fig.add_trace(go.Scatterpolar(
204
- r=df.loc[i].values,
205
- theta=df.columns,
206
- fill='toself',
207
- name=date_label
208
- ))
209
-
210
- fig.update_layout(
211
- polar=dict(
212
- radialaxis=dict(
213
- visible=True,
214
- range=[0, df.max().max()]
215
- )
216
- ),
217
- showlegend=True
218
- )
219
-
220
- st.plotly_chart(fig)
221
  st.subheader("Analysis")
222
  taba, tabb, tabc = st.tabs(["Overview", "Content", "Followers"])
223
  with taba:
224
  st.write("**Overview**")
225
 
226
  data = pd.read_excel('gaming/Last 7 days.xlsx')
 
227
 
228
  x_var = st.sidebar.selectbox("Select X variable for Last 7 days", data.columns)
229
  y_var = st.sidebar.selectbox("Select Y variable for Last 7 days", data.columns)
@@ -274,6 +277,406 @@ elif selected == "Analysis":
274
 
275
  with tabb:
276
  st.write("**Content**")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
 
278
  with tabc:
279
  st.write("**Followers**")
 
25
  import matplotlib.pyplot as plt
26
  from mpl_toolkits.mplot3d import Axes3D
27
 
28
+ def plot_chart(data, chart_type, x_var, y_var, z_var=None, show_regression_line=False, show_r_squared=False):
29
+ scatter_marker_color = 'green'
30
+ regression_line_color = 'red'
31
+ if chart_type == "line":
32
+ fig = px.line(data, x=x_var, y=y_var)
33
+
34
+ elif chart_type == "bar":
35
+ fig = px.bar(data, x=x_var, y=y_var)
36
+
37
+ elif chart_type == "scatter":
38
+ fig = px.scatter(data, x=x_var, y=y_var, color_discrete_sequence=[scatter_marker_color])
39
+
40
+ if show_regression_line and x_var != 'Date':
41
+ X = data[x_var].values.reshape(-1, 1)
42
+ y = data[y_var].values.reshape(-1, 1)
43
+ model = LinearRegression().fit(X, y)
44
+ y_pred = model.predict(X)
45
+ r_squared = r2_score(y, y_pred) # Calculate R-squared value
46
+
47
+ fig.add_trace(
48
+ go.Scatter(x=data[x_var], y=y_pred[:, 0], mode='lines', name='Regression Line', line=dict(color=regression_line_color))
49
+ )
50
+
51
+ # Add R-squared value as a text annotation
52
+ fig.add_annotation(
53
+ x=data[x_var].max(),
54
+ y=y_pred[-1, 0],
55
+ text=f"R-squared: {r_squared:.4f}",
56
+ showarrow=False,
57
+ font=dict(size=14),
58
+ bgcolor='rgba(255, 255, 255, 0.8)',
59
+ bordercolor='black',
60
+ borderwidth=1,
61
+ borderpad=4
62
+ )
63
+
64
+ elif chart_type == "heatmap":
65
+ fig = px.imshow(data, color_continuous_scale='Inferno')
66
+
67
+ elif chart_type == "scatter_3d":
68
+ if z_var is not None:
69
+ fig = px.scatter_3d(data, x=x_var, y=y_var, z=z_var, color=data.columns[0])
70
+ else:
71
+ st.warning("Please select Z variable for 3D line plot.")
72
+ return
73
+
74
+ elif chart_type == "line_3d":
75
+ if z_var is not None:
76
+ fig = go.Figure(data=[go.Scatter3d(x=data[x_var], y=data[y_var], z=data[z_var], mode='lines')])
77
+ fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var)) # Set the axis name
78
+ else:
79
+ st.warning("Please select Z variable for 3D line plot.")
80
+ return
81
+
82
+ elif chart_type == "surface_3d":
83
+ if z_var is not None:
84
+ fig = go.Figure(data=[go.Surface(z=data.values)])
85
+ fig.update_layout(scene=dict(xaxis_title=x_var, yaxis_title=y_var, zaxis_title=z_var)) # Set the axis name
86
+ else:
87
+ st.warning("Please select Z variable for 3D line plot.")
88
+ return
89
+
90
+ elif chart_type == "radar":
91
+ fig = go.Figure()
92
+ for col in data.columns[1:]:
93
+ fig.add_trace(go.Scatterpolar(r=data[col], theta=data[x_var], mode='lines', name=col))
94
+ fig.update_layout(polar=dict(radialaxis=dict(visible=True, range=[data[data.columns[1:]].min().min(), data[data.columns[1:]].max().max()])))
95
+
96
+ st.plotly_chart(fig)
97
+
98
+ def plot_radar_chart(data, columns):
99
+ df = data[columns]
100
+ fig = go.Figure()
101
+
102
+ for i in range(len(df)):
103
+ date_label = data.loc[i, 'Date']
104
+ fig.add_trace(go.Scatterpolar(
105
+ r=df.loc[i].values,
106
+ theta=df.columns,
107
+ fill='toself',
108
+ name=date_label
109
+ ))
110
+
111
+ fig.update_layout(
112
+ polar=dict(
113
+ radialaxis=dict(
114
+ visible=True,
115
+ range=[0, df.max().max()]
116
+ )
117
+ ),
118
+ showlegend=True
119
+ )
120
+
121
+ st.plotly_chart(fig)
122
+
123
+
124
 
125
  # Set page title
126
  st.set_page_config(page_title="Case Study: Gaming Clips - Tiktok Analytics Dashboard", page_icon = "📊", layout = "centered", initial_sidebar_state = "auto")
 
220
  mime='text/csv',
221
  )
222
  elif selected == "Analysis":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  st.subheader("Analysis")
224
  taba, tabb, tabc = st.tabs(["Overview", "Content", "Followers"])
225
  with taba:
226
  st.write("**Overview**")
227
 
228
  data = pd.read_excel('gaming/Last 7 days.xlsx')
229
+ st.write(data)
230
 
231
  x_var = st.sidebar.selectbox("Select X variable for Last 7 days", data.columns)
232
  y_var = st.sidebar.selectbox("Select Y variable for Last 7 days", data.columns)
 
277
 
278
  with tabb:
279
  st.write("**Content**")
280
+ def extract_hashtags(title):
281
+ if isinstance(title, (str, bytes)):
282
+ return re.findall(r'#\w+', title)
283
+ else:
284
+ return [] # or raise an exception or perform other error handling
285
+
286
+ # Remove emojis and hashtags from the title
287
+ def clean_title(title):
288
+ #title_without_emojis = emoji.get_emoji_regexp().sub(u'', title)
289
+ title_without_hashtags = re.sub(r'#\w+', '', title)
290
+ return title_without_hashtags.strip()
291
+
292
+ data = pd.read_excel('gaming/Video Posts.xlsx')
293
+
294
+ data['Video title'] = data['Video title'].fillna("") # Fill missing values with empty string
295
+ data['Video title'] = data['Video title'].astype(str) # Convert to string type
296
+
297
+ data['Hashtags'] = data['Video title'].apply(extract_hashtags)
298
+ #data['Emojis'] = data['Video title'].apply(extract_emojis)
299
+ data['Cleaned_title'] = data['Video title'].apply(clean_title)
300
+ # Add a new column to store the hashtag count
301
+ data['Hashtag_count'] = data['Hashtags'].apply(len)
302
+ # Convert the 'post time' column to datetime format
303
+ data['Post time'] = pd.to_datetime(data['Post time'])
304
+ # Create new columns for 'weekday', 'hour' and 'minute'
305
+ data['weekday_posted'] = data['Post time'].dt.day_name()
306
+ data['hour_posted'] = data['Post time'].dt.hour
307
+ data['min_posted'] = data['Post time'].dt.minute
308
+ st.write(data)
309
+ options = ["Summary", "Total views", "Total shares", "Total likes", "Total comments", "Number of Hashtags", "Hashtag Performance"]
310
+ selected_feature = st.selectbox(label="Select feature", options=options, index=0)
311
+
312
+ if selected_feature == "Summary":
313
+ x_var = st.sidebar.selectbox("Select X variable", data.columns)
314
+ y_var = st.sidebar.selectbox("Select Y variable", data.columns)
315
+ show_regression_line = False
316
+
317
+ z_var_options = ["None"] + list(data.columns)
318
+ z_var = st.sidebar.selectbox("Select Z variable for 3D charts (if applicable)", z_var_options)
319
+
320
+
321
+ tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(["Line", "Bar", "Scatterplot", "Heatmap",
322
+ "3D Scatterplot", "3D Lineplot", "3D Surfaceplot"])
323
+ with tab1:
324
+ st.write("Lineplot")
325
+ plot_chart(data, "line", x_var, y_var)
326
+
327
+ with tab2:
328
+ st.write("Barplot")
329
+ plot_chart(data, "bar", x_var, y_var)
330
+
331
+ with tab3:
332
+ st.write("Scatterplot")
333
+ show_regression_line = st.checkbox("Show regression line for scatterplot")
334
+ plot_chart(data, "scatter", x_var, y_var, show_regression_line=show_regression_line)
335
+
336
+ with tab4:
337
+ st.write("Heatmap")
338
+ plot_chart(data, "heatmap", x_var, y_var)
339
+
340
+ with tab5:
341
+ st.write("3D Scatterplot")
342
+ if z_var != "None":
343
+ plot_chart(data, "scatter_3d", x_var, y_var, z_var)
344
+
345
+ with tab6:
346
+ st.write("3D Lineplot")
347
+ if z_var != "None":
348
+ plot_chart(data, "line_3d", x_var, y_var, z_var)
349
+
350
+ with tab7:
351
+ st.write("3D Surfaceplot")
352
+ if z_var != "None":
353
+ plot_chart(data, "surface_3d", x_var, y_var, z_var)
354
+
355
+
356
+
357
+ elif selected_feature == "Total views":
358
+ data = data.sort_values(by='Total views', ascending=True)
359
+ fig = px.bar(data, x='Total views', y='Cleaned_title', title='Views of trending videos for the week',
360
+ color_discrete_sequence=px.colors.qualitative.Alphabet, hover_data={'Total views': ':.2f'})
361
+ st.plotly_chart(fig)
362
+ elif selected_feature == "Total shares":
363
+ data = data.sort_values(by='Total shares\xa0', ascending=True)
364
+ fig = px.bar(data, x='Total shares\xa0', y='Cleaned_title', title='Shares of trending videos for the week',
365
+ color_discrete_sequence=px.colors.qualitative.Set1, hover_data={'Total shares\xa0': ':.2f'})
366
+ st.plotly_chart(fig)
367
+ elif selected_feature == "Total likes":
368
+ data = data.sort_values(by='Total likes', ascending=True)
369
+ fig = px.bar(data, x='Total likes', y='Cleaned_title', title='Likes of trending videos for the week',
370
+ color_discrete_sequence=px.colors.qualitative.Antique, hover_data={'Total likes': ':.2f'})
371
+ st.plotly_chart(fig)
372
+ elif selected_feature == "Total comments":
373
+ data = data.sort_values(by='Total comments', ascending=True)
374
+ fig = px.bar(data, x='Total comments', y='Cleaned_title', title='Comments of trending videos for the week',
375
+ color_discrete_sequence=px.colors.qualitative.Vivid, hover_data={'Total comments': ':.2f'})
376
+ st.plotly_chart(fig)
377
+ elif selected_feature == "Number of Hashtags":
378
+ # Count the occurrences of each hashtag
379
+ hashtag_counts = Counter(hashtag for hashtags in data['Hashtags'] for hashtag in hashtags)
380
+
381
+ # Get the top N most common hashtags
382
+ N = 10
383
+ top_hashtags = hashtag_counts.most_common(N)
384
+
385
+ # Display the top hashtags
386
+ print(f"Top {N} hashtags:")
387
+ for hashtag, count in top_hashtags:
388
+ print(f"{hashtag}: {count}")
389
+
390
+ # Visualize the results with a Plotly bar chart
391
+ fig = go.Figure(go.Bar(
392
+ x=[t[0] for t in top_hashtags],
393
+ y=[t[1] for t in top_hashtags],
394
+ text=[t[1] for t in top_hashtags],
395
+ textposition='auto',
396
+ marker_color='rgba(58, 71, 80, 0.6)',
397
+ opacity=0.8
398
+ ))
399
+
400
+ fig.update_layout(
401
+ title=f'Top {N} Hashtags',
402
+ xaxis_title='Hashtags',
403
+ yaxis_title='Count',
404
+ xaxis_tickangle=-45
405
+ )
406
+
407
+ st.plotly_chart(fig)
408
+
409
+ tab1, tab2, tab3, tab4 = st.tabs(["vs Views", "vs Shares", "vs Likes", "vs Comments"])
410
+ with tab1:
411
+ fig = px.scatter(data, x='Hashtag_count', y='Total views', hover_data=['Cleaned_title'])
412
+ fig.update_layout(
413
+ title='Hashtag Count vs. Views',
414
+ xaxis_title='Hashtag Count',
415
+ yaxis_title='Views'
416
+ )
417
+ st.plotly_chart(fig)
418
+ with tab2:
419
+ fig = px.scatter(data, x='Hashtag_count', y='Total shares\xa0', hover_data=['Cleaned_title'])
420
+ fig.update_layout(
421
+ title='Hashtag Count vs. Shares',
422
+ xaxis_title='Hashtag Count',
423
+ yaxis_title='Shares'
424
+ )
425
+ st.plotly_chart(fig)
426
+ with tab3:
427
+ fig = px.scatter(data, x='Hashtag_count', y='Total likes', hover_data=['Cleaned_title'])
428
+ fig.update_layout(
429
+ title='Hashtag Count vs. Likes',
430
+ xaxis_title='Hashtag Count',
431
+ yaxis_title='Likes'
432
+ )
433
+ st.plotly_chart(fig)
434
+ with tab4:
435
+ fig = px.scatter(data, x='Hashtag_count', y='Total comments', hover_data=['Cleaned_title'])
436
+ fig.update_layout(
437
+ title='Hashtag Count vs. Comments',
438
+ xaxis_title='Hashtag Count',
439
+ yaxis_title='Comments'
440
+ )
441
+ st.plotly_chart(fig)
442
+ elif selected_feature == "Hashtag Performance":
443
+ # Tokenize hashtags and create a list of unique hashtags
444
+ tokenized_hashtags = data["Hashtags"].tolist()
445
+ unique_hashtags = list(set([tag for tags in tokenized_hashtags for tag in tags]))
446
+ # Train a word2vec model
447
+ model = Word2Vec(tokenized_hashtags, size=50, window=5, min_count=1, workers=4)
448
+
449
+ # Create a hashtag vector dictionary
450
+ hashtag_vectors = {tag: model.wv[tag] for tag in unique_hashtags}
451
+ st.subheader("Explaining the concept of hashtag performance scores and cosine similarity scores")
452
+ st.markdown(
453
+ """
454
+ So how are the **performance scores** calculated for each feature?
455
+
456
+ 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.
457
+
458
+ 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.
459
+ """)
460
+
461
+ st.markdown("""
462
+ How about **cosine similarity**?
463
+
464
+ 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.
465
+
466
+ 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.
467
+ """)
468
+ # Calculate the average performance of each hashtag - views
469
+ hashtag_performance_views = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total views"].mean() for tag in unique_hashtags}
470
+
471
+ # Calculate the average performance of each hashtag - shares
472
+ hashtag_performance_shares = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total shares\xa0"].mean() for tag in unique_hashtags}
473
+
474
+ # Calculate the average performance of each hashtag - likes
475
+ hashtag_performance_likes = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total likes"].mean() for tag in unique_hashtags}
476
+
477
+ # Calculate the average performance of each hashtag - comments
478
+ hashtag_performance_comments = {tag: data[data["Hashtags"].apply(lambda x: tag in x)]["Total comments"].mean() for tag in unique_hashtags}
479
+
480
+ # Calculate the similarity between hashtags
481
+ similarity_matrix = cosine_similarity(list(hashtag_vectors.values()))
482
+
483
+ # Convert the similarity matrix into a DataFrame
484
+ similarity_df = pd.DataFrame(similarity_matrix, index=unique_hashtags, columns=unique_hashtags)
485
+
486
+ # Convert the performance dictionaries into DataFrames
487
+ perf_views_df = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "views"])
488
+ perf_shares_df = pd.DataFrame(list(hashtag_performance_shares.items()), columns=["hashtag", "shares"])
489
+ perf_likes_df = pd.DataFrame(list(hashtag_performance_likes.items()), columns=["hashtag", "likes"])
490
+ perf_comments_df = pd.DataFrame(list(hashtag_performance_comments.items()), columns=["hashtag", "comments"])
491
+
492
+ # Merge the performance DataFrames into a single DataFrame
493
+ perf_df = pd.merge(perf_views_df, perf_shares_df, on="hashtag")
494
+ perf_df = pd.merge(perf_df, perf_likes_df, on="hashtag")
495
+ perf_df = pd.merge(perf_df, perf_comments_df, on="hashtag")
496
+
497
+ # Convert the similarity matrix into a 1D series
498
+ similarity_series = similarity_df.unstack()
499
+
500
+ # Rename the series index
501
+ similarity_series.index.rename(["hashtag1", "hashtag2"], inplace=True)
502
+
503
+ # Convert the series into a DataFrame
504
+ similarity_df = similarity_series.to_frame("similarity").reset_index()
505
+
506
+ # Merge the similarity DataFrame with the performance DataFrame
507
+ merged_df = pd.merge(similarity_df, perf_df, left_on="hashtag1", right_on="hashtag")
508
+
509
+ # Calculate the correlation between hashtag similarity and performance
510
+ correlation = merged_df[["similarity", "views", "shares", "likes", "comments"]].corr()
511
+ st.subheader("Correlation matrix between hashtag cosine similarity values and performance values")
512
+ #st.write(correlation)
513
+ # Create a heatmap
514
+ plt.figure(figsize=(10, 8))
515
+ sns.heatmap(correlation, annot=True, fmt=".2f", cmap='coolwarm', cbar=True)
516
+
517
+ # Show the plot in Streamlit
518
+ st.pyplot(plt)
519
+ # Rename the 'value' columns to make them unique
520
+ df1 = pd.DataFrame(list(hashtag_performance_views.items()), columns=["hashtag", "value"])
521
+ df2 = pd.DataFrame(list(hashtag_performance_shares.items()), columns=["hashtag", "value"])
522
+ df3 = pd.DataFrame(list(hashtag_performance_likes.items()), columns=["hashtag", "value"])
523
+ df4 = pd.DataFrame(list(hashtag_performance_comments.items()), columns=["hashtag", "value"])
524
+
525
+ df1.rename(columns={'value': 'value_views'}, inplace=True)
526
+ df2.rename(columns={'value': 'value_shares'}, inplace=True)
527
+ df3.rename(columns={'value': 'value_likes'}, inplace=True)
528
+ df4.rename(columns={'value': 'value_comments'}, inplace=True)
529
+
530
+ # Merge the DataFrames on the 'hashtag' column
531
+ merged_df = df1.merge(df2, on='hashtag').merge(df3, on='hashtag').merge(df4, on='hashtag')
532
+
533
+ st.subheader("Hashtag Performance Scores based on Views, Shares, Likes and Comments - Calculated using average of all videos' metrics containing a particular hashtag")
534
+ st.write(merged_df)
535
+
536
+ # Create a pair plot with regression lines
537
+ st.write("**Pair Plots**")
538
+ sns.pairplot(merged_df, kind="reg", diag_kind="kde")
539
+ st.pyplot(plt)
540
+
541
+ # Split the data into train and test sets
542
+ X = merged_df[["value_shares", "value_likes", "value_comments"]]
543
+ y = merged_df["value_views"]
544
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
545
+
546
+ # Fit the models on the training data
547
+ lr_model = LinearRegression()
548
+ lr_model.fit(X_train, y_train)
549
+
550
+ rf_model = RandomForestRegressor()
551
+ rf_model.fit(X_train, y_train)
552
+
553
+ xgb_model = XGBRegressor()
554
+ xgb_model.fit(X_train, y_train)
555
+
556
+ # Make predictions on the testing data using the trained models
557
+ lr_pred = lr_model.predict(X_test)
558
+ rf_pred = rf_model.predict(X_test)
559
+ xgb_pred = xgb_model.predict(X_test)
560
+
561
+ # Calculate the evaluation metrics for each model
562
+ st.write("**Training regression models to predict value_views using value_shares, value_likes and value_comments**")
563
+ models = ["Linear Regression", "Random Forest", "XGBoost"]
564
+ predictions = [lr_pred, rf_pred, xgb_pred]
565
+
566
+ # Initialize a list to hold the model metrics
567
+ model_metrics = []
568
+
569
+ for model, pred in zip(models, predictions):
570
+ mse = mean_squared_error(y_test, pred)
571
+ mae = mean_absolute_error(y_test, pred)
572
+ r2 = r2_score(y_test, pred)
573
+
574
+ # Append a dictionary of the metrics to the list
575
+ model_metrics.append({"Model": model, "Mean Squared Error": mse, "Mean Absolute Error": mae, "R^2 Score": r2})
576
+
577
+ # Convert the list of dictionaries into a DataFrame
578
+ metrics_df = pd.DataFrame(model_metrics)
579
+
580
+ # Display the DataFrame in Streamlit
581
+ st.write(metrics_df)
582
+
583
+
584
+ x_var = st.sidebar.selectbox("Select X variable", merged_df.columns)
585
+ y_var = st.sidebar.selectbox("Select Y variable", merged_df.columns)
586
+ show_regression_line = False
587
+
588
+ z_var_options = ["None"] + list(merged_df.columns)
589
+ z_var = st.sidebar.selectbox("Select Z variable for 3D charts (if applicable)", z_var_options)
590
+
591
+ st.subheader("Various plots to represent performance scores for views, shares, likes and comments")
592
+ tab1, tab2, tab3, tab4, tab5, tab6, tab7 = st.tabs(["Line", "Bar", "Scatterplot", "Heatmap",
593
+ "3D Scatterplot", "3D Lineplot", "3D Surfaceplot"])
594
+ with tab1:
595
+ st.write("Lineplot")
596
+ plot_chart(merged_df, "line", x_var, y_var)
597
+
598
+ with tab2:
599
+ st.write("Barplot")
600
+ plot_chart(merged_df, "bar", x_var, y_var)
601
+
602
+ with tab3:
603
+ st.write("Scatterplot")
604
+ show_regression_line = st.checkbox("Show regression line")
605
+ plot_chart(merged_df, "scatter", x_var, y_var, show_regression_line=show_regression_line)
606
+
607
+ with tab4:
608
+ st.write("Heatmap")
609
+ plot_chart(merged_df, "heatmap", x_var, y_var)
610
+
611
+ with tab5:
612
+ st.write("3D Scatterplot")
613
+ if z_var != "None":
614
+ plot_chart(merged_df, "scatter_3d", x_var, y_var, z_var)
615
+
616
+ with tab6:
617
+ st.write("3D Lineplot")
618
+ if z_var != "None":
619
+ plot_chart(merged_df, "line_3d", x_var, y_var, z_var)
620
+
621
+ with tab7:
622
+ st.write("3D Surfaceplot")
623
+ if z_var != "None":
624
+ plot_chart(merged_df, "surface_3d", x_var, y_var, z_var)
625
+
626
+ tab1, tab2, tab3, tab4 = st.tabs(["vs Views", "vs Shares", "vs Likes", "vs Comments"])
627
+ with tab1:
628
+ st.subheader("Hashtag Performance - Views:")
629
+ # Sort the DataFrame by the 'value' column in descending order
630
+ sorted_df1 = df1.sort_values(by="value_views", ascending=False)
631
+ st.write(sorted_df1)
632
+ # Highlight specific bars (use 'rgba' values for transparency)
633
+ highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
634
+ sorted_df1['color'] = sorted_df1['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'red')
635
+ fig = px.bar(sorted_df1, x='hashtag', y='value_views', title='Hashtag performance for the week',
636
+ color='color', color_discrete_map='identity', hover_data={'value_views': ':.2f'})
637
+
638
+ fig.update_layout(title='Hashtag performance for the week - Views', xaxis_title='Hashtag', yaxis_title='Value')
639
+ st.plotly_chart(fig)
640
+
641
+ with tab2:
642
+ st.subheader("Hashtag Performance - Shares:")
643
+ # Sort the DataFrame by the 'value' column in descending order
644
+ sorted_df2 = df2.sort_values(by="value_shares", ascending=False)
645
+ st.write(sorted_df2)
646
+ # Highlight specific bars (use 'rgba' values for transparency)
647
+ highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
648
+ sorted_df2['color'] = sorted_df2['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'blue')
649
+ fig = px.bar(sorted_df2, x='hashtag', y='value_shares', title='Hashtag performance for the week',
650
+ color='color', color_discrete_map='identity', hover_data={'value_shares': ':.2f'})
651
+
652
+ fig.update_layout(title='Hashtag performance for the week - Shares', xaxis_title='Hashtag', yaxis_title='Value')
653
+ st.plotly_chart(fig)
654
+ with tab3:
655
+ st.subheader("Hashtag Performance - Likes:")
656
+ # Sort the DataFrame by the 'value' column in descending order
657
+ sorted_df3 = df3.sort_values(by="value_likes", ascending=False)
658
+ st.write(sorted_df3)
659
+ # Highlight specific bars (use 'rgba' values for transparency)
660
+ highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
661
+ sorted_df3['color'] = sorted_df3['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'green')
662
+ fig = px.bar(sorted_df3, x='hashtag', y='value_likes', title='Hashtag performance for the week',
663
+ color='color', color_discrete_map='identity', hover_data={'value_likes': ':.2f'})
664
+
665
+ fig.update_layout(title='Hashtag performance for the week - Likes', xaxis_title='Hashtag', yaxis_title='Value')
666
+ st.plotly_chart(fig)
667
+ with tab4:
668
+ st.subheader("Hashtag Performance - Comments:")
669
+ # Sort the DataFrame by the 'value' column in descending order
670
+ sorted_df4 = df4.sort_values(by="value_comments", ascending=False)
671
+ st.write(sorted_df4)
672
+ # Highlight specific bars (use 'rgba' values for transparency)
673
+ highlighted_bars = ['#fyp', '#tiktok', '#foryou', '#trending', '#viral']
674
+ sorted_df4['color'] = sorted_df4['hashtag'].apply(lambda x: 'black' if x in highlighted_bars else 'orange')
675
+ fig = px.bar(sorted_df4, x='hashtag', y='value_comments', title='Hashtag performance for the week',
676
+ color='color', color_discrete_map='identity', hover_data={'value_comments': ':.2f'})
677
+
678
+ fig.update_layout(title='Hashtag performance for the week - Comments', xaxis_title='Hashtag', yaxis_title='Value')
679
+ st.plotly_chart(fig)
680
 
681
  with tabc:
682
  st.write("**Followers**")