<jupyter_start><jupyter_text>The code provides answers for three data science questions about time series forecasting and relations discovering for an online retailer as follows:1- Time series forecasting based on ARIMA Model in file 1) OnlineRetailer_weekly_sales_prediction.ipynb,2- Decision Tree model in file 2) OnlineRetailer_estimating_for_customer.ipynb,3- Association rule learning in file 3) OnlineRetailer_basket_analysis.ipynb.<jupyter_code># Loading libraries
from datetime import datetime, timedelta,date
import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
import pydotplus

import warnings
warnings.filterwarnings("ignore")
# Loading online retailer data
# onlineRetailer_df = pd.read_csv('online_retail_II.csv', encoding= 'ISO-8859-1')
# Preview the original data
# onlineRetailer_df.head()
# UTILIZAR ESTA PARTE SI ESTAS EN GOOGLE COLAB
import sys
assert sys.version_info >= (3, 5)
import os
RETAIL_PATH = "https://github.com/hcgalvan/UNSAM-Machine-Learning-on-Economics/raw/main/data/"
if 'google.colab' in sys.modules:
  def load_datasets_h1(datasets_path=RETAIL_PATH):
    #retail_ol_h1 = pd.read_csv('./data/Year 2009-2010_train.csv',  encoding= 'unicode_escape')
    # retail_ol_h2 = pd.read_csv('./data/Year 2010-2011_train.csv',  encoding= 'unicode_escape')
    csv_path = os.path.join(datasets_path, "Year 2009-2010_train.csv")
    return pd.read_csv(csv_path, encoding= 'unicode_escape')

  # The function loads the data as a Pandas DataFrame instance.
  retail_ol_h1 = load_datasets_h1()
  def load_datasets_h2(datasets_path=RETAIL_PATH):
      #xlsx_path = os.path.join(datasets_path, "online_retail_II.xlsx")
      #return pd.read_excel(xlsx_path,1)
      csv_path = os.path.join(datasets_path, "Year 2010-2011_train.csv")
      return pd.read_csv(csv_path, encoding= 'unicode_escape')

  # The function loads the data as a Pandas DataFrame instance.
  retail_ol_h2 = load_datasets_h2()
# Loading online retailer data
# onlineRetailer_df = pd.read_csv('online_retail_II.csv', encoding= 'ISO-8859-1')
#retail_ol_h1= pd.read_excel(io='./data/online_retail_II.xlsx', sheet_name='Year 2010-2011')
#retail_ol_h2= pd.read_excel(io='./data/online_retail_II.xlsx', sheet_name='Year 2009-2010')
retail_ol_h1 = pd.read_csv('./data/Year 2009-2010_train.csv',  encoding= 'unicode_escape')
retail_ol_h2 = pd.read_csv('./data/Year 2010-2011_train.csv',  encoding= 'unicode_escape')

frames = [retail_ol_h1, retail_ol_h2]
results = pd.concat(frames)
onlineRetailer_df = results.copy()
# Check for missing values
onlineRetailer_df.isnull().sum()
# Check for missing values
onlineRetailer_df.isnull().sum()
# Remove missing values
onlineRetailer_df.dropna(inplace=True)
# Remove extra spaces from descriptions 
onlineRetailer_df['Description'] = onlineRetailer_df['Description'].str.strip()
# Remove rows which dont have invoice number
onlineRetailer_df.dropna(axis=0, subset=['Invoice'], inplace=True)
# Create the basket dataframe object, where the country is USA, and pivot the result
basket = (onlineRetailer_df[onlineRetailer_df['Country'] =="USA"]
          .groupby(['Invoice', 'Description'])['Quantity']
          .sum().unstack().reset_index().fillna(0)
          .set_index('Invoice'))
# Preview the basekt
basket
# Function to encoding the basket pivot table, it converts any value bigger than 0 to 1
def encodeingBasket(unit):
    if unit <= 0:
        return 0
    if unit >= 1:
        return 1

basket = basket.applymap(encodeingBasket)
# Generate the frequent item sets that have a support of at least 7%
frequent_products = apriori(basket, min_support=0.07, use_colnames=True)
# Create the association rules model
rules = association_rules(frequent_products, metric="lift", min_threshold=1)
rules.head()
# Preivew the result which have an expected confidence (lift) more than 6 and confidence of at least 80%
rules[ (rules['lift'] >= 6) &
       (rules['confidence'] >= 0.8)]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1068931068931069, 'fraction_numerical': 0.028971028971028972, 'mean_word_length': 5.554828150572831, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '544733', 'license': 'MIT', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1324, 'n_words': 334, 'path': '3) OnlineRetailer_basket_analysis.ipynb', 'repo_name': 'hcgalvan/UNSAM-Machine-Learning-on-Economics', 'size': '4004'}