<jupyter_start><jupyter_text>Game Data Analytics<jupyter_code>import pandas as pd
import os

%matplotlib inline

dir_pth = '../../dataset'
file_pth = os.path.join(dir_pth, 'top_games_1605584163.csv')

df = pd.read_csv(file_pth)
print(df.shape)
df.head()
df.dtypes<jupyter_output><empty_output><jupyter_text>Delete useless data<jupyter_code>df = df.drop('box_art_url', axis=1)
print(df.shape)
df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Add labels for each gameDefine a function to get labels from tag files<jupyter_code>import glob

tag_pth = os.path.join(dir_pth, 'tags')
all_tag_files = glob.glob(tag_pth + '/*.csv')

def get_tags():
    tags = {}
    for tag_file in all_tag_files:
        try:
            df = pd.read_csv(tag_file)
        except:
            continue
        ids = df['id'].values
        tag = tag_file.split('/')[-1].split('.')[0]
        tags[tag] = ids
    return tags

tags = get_tags()

def find_label(game_id):
    labels = []
    
    for key, val in tags.items():
        if game_id in val:
            labels.append(key)
    return labels
            

print(find_label(509658))
ids = df['id'].values
labels = [find_label(id) for id in ids]
df['label'] = labels

df.head()<jupyter_output><empty_output><jupyter_text>Remove item with `IRL` label<jupyter_code>idxes = []
for idx, row in df.iterrows():
    label = row['label']
    if 'IRL' in label:
        idxes.append(idx)
df = df.drop(idxes)

print(df.shape)
df.head()<jupyter_output>(388, 12)<jupyter_text>Check if there is an item with empty label<jupyter_code>idxes = []

for idx, row in df.iterrows():
    label = row['label']
    if not label:
        print(row['name'])
        idxes.append(idx)

df = df.drop(idxes)

print(df.shape)
import plotly.offline as pyo
import plotly.graph_objs as go

pyo.init_notebook_mode(connected=True)

cat = {}
for _, row in df.iterrows():
    label = row['label']
    for tag in label:
        if tag in cat:
            cat[tag].append(row['id'])
        else:
            cat[tag] = [row['id']]

def draw_number_of_each_tag():
    data = {'tag': [], 'num': []}
    for key in cat.keys():
        data['tag'].append(key)
        data['num'].append(len(cat[key]))
    trace = go.Pie(
        labels=data['tag'],
        values=data['num'],
        name='Number of Games'
    )
    fig=go.Figure(data=[trace])
    fig.update_layout(
        title='Number of Games per Tag',
        yaxis_title='Number of Games',
    )
    pyo.iplot(fig, filename='stacked-bar')

draw_number_of_each_tag()
def draw_total_viewers():
    data = {'tag': [], 'total': []}
    for key in cat.keys():
        data['tag'].append(key)
        sdf = df[df['id'].isin(cat[key])]
        total = sdf['Avg. viewers, 7 days'].sum()
        data['total'].append(total)
    trace = go.Bar(
        x=data['tag'],
        y=data['total'],
        name='Number of Games'
    )
    fig=go.Figure(data=[trace])
    fig.update_layout(
        title='Total Viewers in 7 Days',
        yaxis_title='Total Viewers',
    )
    pyo.iplot(fig, filename='stacked-bar')

draw_total_viewers()
def draw_average_viewers():
    data = {'tag': [], 'aver': []}
    for key in cat.keys():
        data['tag'].append(key)
        sdf = df[df['id'].isin(cat[key])]
        aver = sdf['Avg. viewers, 7 days'].sum() *1.0 / len(cat[key])
        data['aver'].append(aver)
    trace = go.Bar(
        x=data['tag'],
        y=data['aver'],
        name='Number of Games'
    )
    fig=go.Figure(data=[trace])
    fig.update_layout(
        title='Average Viewers in 7 Days',
        yaxis_title='Average Viewers',
    )
    pyo.iplot(fig)

draw_average_viewers()
def draw_peak_viewers():
    data = {'tag': [], 'peak': []}
    for key in cat.keys():
        data['tag'].append(key)
        sdf = df[df['id'].isin(cat[key])]
        peak = sdf['Peak viewers '].str.split(' ').str[0].astype(int)
        data['peak'].append(peak.max())
    trace = go.Bar(
        x=data['tag'],
        y=data['peak'],
        name='Number of Games'
    )
    fig=go.Figure(data=[trace])
    fig.update_layout(
        title='Peak',
        yaxis_title='Peak Viewers',
    )
    pyo.iplot(fig)

draw_peak_viewers()
from sklearn.linear_model import LinearRegression
import numpy as np

print(df.columns)

def draw_viewer_channel():
    data = {'channel': [], 'viewer': []}
    
    for _, row in df.iterrows():
        data['channel'].append(row['Avg. viewers, 7 days'])
        data['viewer'].append(row['Avg. channels, 7 days'])
    model = LinearRegression()
    model.fit(np.array(data['channel']).reshape(-1, 1), np.array(data['viewer']))
    trace1 = go.Scatter(
        x=data['channel'],
        y=data['viewer'],
        name='Viewer VS. Channel',
        mode = 'markers'
    )
    x_range = np.linspace(min(data['channel']), max(data['channel']), 100)
    y_range = model.predict(x_range.reshape(-1, 1))
    trace2 = go.Scatter(
        x=x_range,
        y=y_range,
        name='Line',
    )
    fig=go.Figure(data=[trace1, trace2])
    fig.update_layout(
        title='Viewer VS. Channel',
        xaxis_title='Channels Number',
        yaxis_title='Viewers',
    )
    pyo.iplot(fig)

draw_viewer_channel()<jupyter_output>Index(['id', 'name', 'Live viewers', 'Live channels', 'Rank',
       'Avg. viewers, 7 days', 'Avg. channels, 7 days',
       'Hours watched, 7 days', 'Peak viewers ', 'Peak channels ',
       'Viewers per channel, 7 days', 'label'],
      dtype='object')
{'fraction_non_alphanumeric': 0.16715542521994134, 'fraction_numerical': 0.008797653958944282, 'mean_word_length': 2.8674698795180724, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '287428', 'license': 'MIT', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1945, 'n_words': 396, 'path': 'docs/notebook/game.ipynb', 'repo_name': 'yuukidach/eGaming-Analytics', 'size': '5456'}