<jupyter_start><jupyter_text>StockX Data Contest 2019 <NAME>, <EMAIL> Import Data and Clean<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pytrends.request import TrendReq

plt.style.use('seaborn-darkgrid')
df = pd.read_excel('StockX-Data-Contest-2019-3.xlsx',error_bad_lines=False) 
df.head(10)
df.info()
df["Profit"] = df["Sale Price"] - df["Retail Price"]
df.head()<jupyter_output><empty_output><jupyter_text>Need to convert the object types into categorical types.<jupyter_code>df['Brand'] = pd.Categorical(df['Brand'])
df['Sneaker Name'] = pd.Categorical(df['Sneaker Name'])
df['Buyer Region'] = pd.Categorical(df['Buyer Region'])
df['Order Date'] = pd.to_datetime(df['Order Date'], format='%m/%d/%Y %I:%M:%S %p')
df.index = pd.DatetimeIndex(df['Order Date']) #make date the index of the data
df.info() #check to make sure columns are all the correct data types<jupyter_output><class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 99956 entries, 2017-09-01 to 2019-02-13
Data columns (total 9 columns):
Order Date      99956 non-null datetime64[ns]
Brand           99956 non-null category
Sneaker Name    99956 non-null category
Sale Price      99956 non-null float64
Retail Price    99956 non-null int64
Release Date    99956 non-null datetime64[ns]
Shoe Size       99956 non-null float64
Buyer Region    99956 non-null category
Profit          99956 non-null float64
dtypes: category(3), datetime64[ns](2), float64(3), int64(1)
memory usage: 5.6 MB<jupyter_text>Exploratory Data Analysis<jupyter_code>df2 = df.drop(['Order Date','Brand','Sneaker Name', 'Sale Price','Retail Price', 'Release Date', 'Buyer Region'], axis=1)
df2 = df2.groupby('Shoe Size', as_index=False)['Profit'].mean()
sns.set_style("darkgrid")
plt.title("Shoe Size vs. Average Profit")
plt.plot( 'Shoe Size', 'Profit', data=df2, linestyle='-', marker='o')
plt.show()
sns.set_style("whitegrid")
df3 = df.drop(['Order Date','Sneaker Name', 'Sale Price','Retail Price', 'Release Date', 'Buyer Region'], axis=1)
df3.head()

df3.info()
plt.figure(figsize=(16, 10))
sns.set_context("poster")
ax = sns.scatterplot(x="Shoe Size", y="Profit", palette="Greens_r",hue="Brand",data = df3,s=200)
ax.set_title('How Brand and Shoe Size Affect Profit on StockX')
ax.figure.savefig("StockXPLot.png")<jupyter_output><empty_output><jupyter_text>From this graph, we can see that there is clearly some sort of clustering when the brand was plotted based on shoe size and profit from the sale.  I will run with this idea and train several classification algorithms to see which one provides the most accurate result for classifying the brand of shoe based on only shoe size and profit.  Logistic Regression<jupyter_code>df.head()
df_log_reg = df.drop(['Sneaker Name','Release Date', 'Order Date', 'Buyer Region', 'Sale Price', 'Retail Price'], axis=1)
df_log_reg.head()
df_log_reg.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 99956 entries, 2017-09-01 to 2019-02-13
Data columns (total 3 columns):
Brand        99956 non-null category
Shoe Size    99956 non-null float64
Profit       99956 non-null float64
dtypes: category(1), float64(2)
memory usage: 2.4 MB<jupyter_text>Now we want to convert Yeezy and Off-White into 1 and 0 so we can use logistic regression to classify the data.  Yeezy's will now be a '1' and Off-White will now be '0'.<jupyter_code>df_log_reg['Brand'] = (df_log_reg['Brand'] !='Off-White').astype(int)
df_log_reg.head()
df_log_reg.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 99956 entries, 2017-09-01 to 2019-02-13
Data columns (total 3 columns):
Brand        99956 non-null int64
Shoe Size    99956 non-null float64
Profit       99956 non-null float64
dtypes: float64(2), int64(1)
memory usage: 3.1 MB<jupyter_text>Now all of our data should be in the correct format.  We will now proceed with training a logistic regression model to predict Brand based off of Sale Price, Retail Price, Shoe Size, Buyer Region, and Profit.<jupyter_code>y = df_log_reg['Brand']
X = df_log_reg.drop('Brand', axis=1)
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
y_pred = logreg.predict(X_test)
print('Accuracy of logistic regression classifier on training set: {:.2f}'.format(logreg.score(X_train, y_train)))
print('Accuracy of logistic regression classifier on test set: {:.2f}'.format(logreg.score(X_test, y_test)))<jupyter_output>Accuracy of logistic regression classifier on training set: 0.85
Accuracy of logistic regression classifier on test set: 0.85<jupyter_text>Naive Bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
bayes = GaussianNB()
bayes.fit(X_train, y_train)
y_pred = bayes.predict(X_test)
print('Accuracy of Naive Bayes on training set: {:.2f}'.format(bayes.score(X_train, y_train)))
print('Accuracy of Naive Bayes classifier on test set: {:.2f}'.format(bayes.score(X_test, y_test)))<jupyter_output>Accuracy of Naive Bayes on training set: 0.83
Accuracy of Naive Bayes classifier on test set: 0.84<jupyter_text>Decision Tree<jupyter_code>from sklearn import tree
dec_tree = tree.DecisionTreeClassifier()
dec_tree.fit(X_train, y_train)
y_pred = dec_tree.predict(X_test)
print('Accuracy of Decision Tree on training set: {:.2f}'.format(dec_tree.score(X_train, y_train)))
print('Accuracy of Decision Tree classifier on test set: {:.2f}'.format(dec_tree.score(X_test, y_test)))<jupyter_output>Accuracy of Decision Tree on training set: 0.90
Accuracy of Decision Tree classifier on test set: 0.86<jupyter_text>We can see that out of Logistic Regression, Naive Bayes, and a Decision Tree that using a Decision Tree was the best way to classify the sale as either a Yeezy shoe or an Off-White shoe.<jupyter_code>from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.74      0.73      0.74      8142
           1       0.90      0.90      0.90     21845

   micro avg       0.86      0.86      0.86     29987
   macro avg       0.82      0.82      0.82     29987
weighted avg       0.86      0.86      0.86     29987
{'fraction_non_alphanumeric': 0.11274894150854634, 'fraction_numerical': 0.050493962678375415, 'mean_word_length': 5.022662889518414, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '323460', 'license': 'MIT', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2158, 'n_words': 657, 'path': 'StockX.ipynb', 'repo_name': 'Kayleigh97/StockX_2019', 'size': '6395'}