<jupyter_start><jupyter_text>Examples of differet boosting algorithms (new random forest)1. General usage2. Analysis3. Improvements<jupyter_code>import pandas as pd
import numpy as np
import lightgbm
import sklearn
import matplotlib.pyplot as plt
%matplotlib inline
# 
raw_df = pd.read_excel("http://archive.ics.uci.edu/ml/machine-learning-databases/00352/Online%20Retail.xlsx")

raw_df = raw_df.dropna()

# drop not needed stuff 
raw_df.drop(columns=['StockCode', 'Description'], inplace=True)

raw_df['Quantity'] = raw_df['Quantity'].apply(lambda x: int(x))
raw_df['CustomerID'] = raw_df['CustomerID'].apply(lambda x: int(x))
raw_df['UnitPrice'] = raw_df['UnitPrice'].apply(lambda x: float(x))
raw_df = raw_df.drop(raw_df[raw_df.Quantity <= 0].index)
raw_df['Transaction'] = raw_df["UnitPrice"] * raw_df["Quantity"]

lastDate = raw_df['InvoiceDate'].iloc[-1]
raw_df = raw_df.groupby('CustomerID').agg({'InvoiceDate': lambda x: (lastDate - x.max()).days, 
                                           'InvoiceNo': lambda x: len(x), 
                                           'Transaction': lambda x: x.sum()})
raw_df['InvoiceDate'] = raw_df['InvoiceDate'].astype(int)
raw_df.rename(columns={'InvoiceDate': 'recency', 
                         'InvoiceNo': 'frequency', 
                         'Transaction': 'monetary'}, inplace=True)

raw_df.head()
# Adaptive boosting -> combining stmps into one regressor/classifier
from sklearn.ensemble import AdaBoostRegressor
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split

# prepare data
train, test = train_test_split(raw_df)
train_target = train['monetary']
train.drop(columns=['monetary'], inplace=True)
test_target = test['monetary']
test.drop(columns=['monetary'], inplace=True)

# Fit regression model
simple = DecisionTreeRegressor(max_depth=4)

boosted = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), n_estimators=300)

simple.fit(train, train_target)
boosted.fit(train, train_target)

# Predict
simple_predictor = simple.predict(test)
boosted_predictor = boosted.predict(test)

from sklearn.metrics import mean_squared_error

simple_mse = mean_squared_error(list(test_target), simple_predictor)
boosted_mse = mean_squared_error(list(test_target), boosted_predictor)

print("simple: {} vs boosted: {}".format(simple_mse, boosted_mse))
print(simple_mse - boosted_mse)<jupyter_output>simple: 100790850.14594287 vs boosted: 204600581.69923657
-103809731.5532937
{'fraction_non_alphanumeric': 0.12834008097165991, 'fraction_numerical': 0.027125506072874495, 'mean_word_length': 5.255696202531645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '432457', 'license': 'MIT', 'n_tokens_mistral': 891, 'n_tokens_neox': 810, 'n_words': 155, 'path': 'notebooks/Boosting.ipynb', 'repo_name': 'Alisa-lisa/ml-showcase', 'size': '2470'}