<jupyter_start><jupyter_text>Data from https://www.kaggle.com/debajyotipodder/co2-emission-by-vehicles<jupyter_code>import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

from sklearn.svm import OneClassSVM
from sklearn.neighbors import LocalOutlierFactor
from sklearn.ensemble import IsolationForest
from sklearn.model_selection import GridSearchCV, cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.datasets import load_digits
from sklearn.datasets import fetch_openml
from sklearn.kernel_approximation import Nystroem

from sklearn_extra.robust import RobustWeightedRegressor
    
pd.set_option('display.max_columns', 300) # Display up to 300 columns from pandas
df = pd.read_csv('data/co2/CO2 Emissions_Canada.csv')
X = df[df.columns[:-1]]
y = df['CO2 Emissions(g/km)']
X = X.drop(columns=['Make', 'Model'])
X.head()
X.shape
plt.figure(figsize=(15,5))
sns.histplot(data = df, x='CO2 Emissions(g/km)')
X = pd.get_dummies(X, drop_first=False)
X.columns.shape
from sklearn_extra.robust import RobustWeightedRegressor
reg = Pipeline(steps=[('scaler', RobustScaler()),
                ('reg',RobustWeightedRegressor(burn_in=0, k=20, weighting='mom'))])
print(np.mean(cross_val_score(reg, X, y, cv=5, scoring="neg_median_absolute_error")))
from sklearn.ensemble import RandomForestRegressor
reg = RandomForestRegressor()
print(np.mean(cross_val_score(reg, X, y, cv=5, scoring="neg_median_absolute_error")))
scaler = RobustScaler()
X_transformed = scaler.fit_transform(X)
reg = RobustWeightedRegressor(burn_in=0, k=20, weighting='mom', tol=-10, max_iter = 500) 
# set a negative tolerance to force the algorithm to stop late. 
# It is needed because the weights for mom need a lot of iterations to be computed.
reg.fit(X_transformed,y)
plt.bar(np.arange(len(reg.weights_))[:500], np.sort(reg.weights_)[:500])
outliers = np.argsort(reg.weights_)[:10]
df.iloc[outliers]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1156735102653981, 'fraction_numerical': 0.018027040560841263, 'mean_word_length': 6.8046875, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '300226', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 740, 'n_tokens_neox': 698, 'n_words': 160, 'path': "Outlier detection on CO2's vehicules.ipynb", 'repo_name': 'TimotheeMathieu/IllustrationRobustML', 'size': '1997'}