<jupyter_start><jupyter_text>Data Science Blog Post Project<jupyter_code># Import the required python libraries
import numpy as np
import pandas as pd
import seaborn as sns

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_squared_error

import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Gather data<jupyter_code># Load the Video game dataset into a pandas dataframe
df = pd.read_csv('vgsales.csv')<jupyter_output><empty_output><jupyter_text>Assess data<jupyter_code># View a sample of the dataframe
df.head(5)
# Use Describe method to understand basic statistical details like percentile, mean, std, etc...
df.describe()
# Use the columns method to understand the columns in the dataframe
df.columns
# Use the shape method to count the number of rows and columns in the dataframe
#df.shape

count_row = df.shape[0]
count_col = df.shape[1]

print('Number of Row = {}'.format(count_row))
print('Number of Columns = {}'.format(count_col))
# Use the dtypes method to eetermine the types of data you are working with wihtin the dataframe
df.dtypes
# Use the isnull method to determine if there are any null items in the dataframe to consider
df.isnull().sum(axis = 0)
# Use the select_dbTypes method to identify the categorical and numerical fields in the dataframe

def cat_and_num_val(datasets):
    for col in datasets:
        print("Dataset:", [x for x in globals() if globals()[x] is col][0]) #Name of the dataset)
        print("Numerical columns: ", col.select_dtypes(include=['int', 'float']).columns)
        print("Categorical columns ", col.select_dtypes(include=['category', object]).columns)


datasets = [df]
cat_and_num_val(datasets)<jupyter_output>Dataset: df
Numerical columns:  Index(['Rank', 'Year', 'NA_Sales', 'EU_Sales', 'JP_Sales', 'Other_Sales',
       'Global_Sales'],
      dtype='object')
Categorical columns  Index(['Name', 'Platform', 'Genre', 'Publisher'], dtype='object')<jupyter_text>Clean the data<jupyter_code># Use the fillnamethod to replace empty values
df['Year'] = df['Year'].fillna(0.0)
df['Publisher'] = df['Publisher'].fillna('na')
# User the astype to convert Year from float to integer
df["Year"] = df["Year"].astype(int)<jupyter_output><empty_output><jupyter_text>Analyze the data (Understanding the Platform object)<jupyter_code># Use the describe method to understand the Platform column in the dataset
df['Platform'].describe()
# Use the unique method to provide a list the unique Platform objects within the dataset
df.Platform.unique()<jupyter_output><empty_output><jupyter_text>Model the data<jupyter_code># Use the value_counts method to provide the counts of the unique Platform objects within the dataset
df.Platform.value_counts()<jupyter_output><empty_output><jupyter_text>Visualize the data<jupyter_code># Use the plt to visualize the Platform counts

df_popular = df.Platform.value_counts()
df_popular.plot(kind="bar", figsize=(15,5));
plt.title("Most popular Platform");
# Use the groupby and agg methods to provide total sales by region for each Platform

df_sales= df.groupby(['Platform']).agg({'NA_Sales':np.sum,'JP_Sales':np.sum,'EU_Sales':np.sum,'Other_Sales':np.sum,'Global_Sales':np.sum})
df_sales.sort_values(by='Global_Sales', ascending=False, inplace=True)
df_sales.head(5)
df_sales['percentage']= df_sales['Global_Sales']/df_sales['Global_Sales'].sum()
df_sales.head(5)<jupyter_output><empty_output><jupyter_text>Question 1Understand how Video game Platform sales are performing by total sales in each region<jupyter_code>## 1. Understand how Video game Platform sales are performing by total sales in each region

fig = plt.figure(figsize=(15,5))
ax = fig.add_axes([0,0,1,1])
ax.plot(df_sales['NA_Sales'], label='North America')
ax.plot(df_sales['EU_Sales'], label='Japan')
ax.plot(df_sales['JP_Sales'], label='Japan')
ax.plot(df_sales['Other_Sales'], label='Other')
ax.plot(df_sales['Global_Sales'], label='Global')

plt.savefig("images/q1", bbox_inches="tight")

ax.legend()
ax.set_xlabel('Count')
ax.set_ylabel('Platform');<jupyter_output><empty_output><jupyter_text>Q1 Results : From the visual representation above we can see that PS2 has outsold all other platforms.  Understanding the Genre object<jupyter_code># Use the groupby and agg methods to provide the total games by name for each Genre

df_geners = df.groupby(['Genre']).agg({'Name':pd.Series.nunique})
df_geners.reset_index(inplace=True)
df_geners.set_index('Genre', inplace=True)

df_geners.rename(columns={'Name': 'count'}, inplace=True)
df_geners.sort_values(by='count', ascending=False, inplace=True)
df_geners
# Use the column method to confirm the column names
df_geners.columns<jupyter_output><empty_output><jupyter_text>Question 2Understand how video games are distributed across different Genre's<jupyter_code>## 2. Understand how video games are distributed across different Genre's

df_geners = df.groupby('Genre')['Name'].nunique().plot(kind='bar', figsize=(15,5))

df_geners.plot(kind='bar', figsize=(15,5))
plt.title("Genre's");

plt.savefig("images/q2", bbox_inches="tight")

plt.show()<jupyter_output><empty_output><jupyter_text>Q2 Results: From the visual represenation above we can determine that the Genre Action leads in popularity.  Question 3Predict the Platform sales based on sales reportings<jupyter_code># 3. Predict the Platform sales based on sales reportings

from sklearn.preprocessing import LabelEncoder
df['Name_encoded'] = LabelEncoder().fit_transform(df['Name'])
df['Platform_encoded'] = LabelEncoder().fit_transform(df['Platform'])
df['Genre_encoded'] = LabelEncoder().fit_transform(df['Genre'])
df['Publisher_encoded'] = LabelEncoder().fit_transform(df['Year'])

df.head(5)
#df['date_col'] =  pd.to_datetime(df['Year'])
#df.Year = pd.to_datetime(df.Year)
fig = plt.figure(figsize = (10,10))
df.hist();
# Use the heatmap visual to provide a look into corelation to use when predicting

sns.set(rc={'figure.figsize':(15,15)})
sns.heatmap(df.corr(), annot=True, fmt=".2f");
# Use the sklearn LinearRegression model to predict Platform sales

X = df[['Year', 'NA_Sales','EU_Sales','JP_Sales','Other_Sales','Global_Sales']]
y = df['Platform_encoded']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .30, random_state=42)

lm_model = LinearRegression(normalize=True)
lm_model.fit(X_train, y_train)

y_test_preds = lm_model.predict(X_test)
print('R2 Score', r2_score(y_test, y_test_preds))
print('Mean Squared', mean_squared_error(y_test, y_test_preds))
# Use the plot to display the prediction and actual data
y_pred = lm_model.predict(X_test) 
plt.plot(y_test, y_pred, '.')

x = np.linspace(0, 30, 30)
y = x
plt.plot(x, y)

plt.savefig("images/q3", bbox_inches="tight")

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12730195849166911, 'fraction_numerical': 0.008623209587839813, 'mean_word_length': 7.146428571428571, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '98876', 'license': 'FTL', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2251, 'n_words': 605, 'path': 'DataScienceBlogPost.ipynb', 'repo_name': 'gfonteno/DataScienceBlogPost', 'size': '6842'}