> For the complete documentation index, see [llms.txt](https://docs.datahungry.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.datahungry.dev/library/sklearn-and-shap.md).

# Sklearn & SHAP

##

## Pre-requirements

```
pip install shap, xgboost, scikit-learn
```

## Cleaning

```python
import pandas as pd

# label encoder
df['ocean_proximity_encoded'], labels = pd.factorize(df['ocean_proximity'])
df = df.drop("ocean_proximity", axis=1)

# onehot encoder
one_hot_encoded = pd.get_dummies(df['ocean_proximity'])
df = pd.concat([df, one_hot_encoded], axis=1).drop("ocean_proximity", axis=1)

# train_test_split
x_train, x_test, y_train, y_test = train_test_split(df, test_size=0.2, random_state=0)
```

## Modeling

### Classification

```python
import numpy as np
from xgboost import XGBClassifier
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

iris_data = load_iris()

x = iris_data.data
y = iris_data.target
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)

model = XGBClassifier(n_estimators=2, max_depth=2, learning_rate=1, objective='binary:logistic')
model.fit(X_train, y_train)

y_pred = model.predict(x_test)

print('The accuracy of the test set is:', model.score(x_test,y_test))

matrix_test = confusion_matrix(y_test, y_pred)
print('The confusion matrix for the training set is:\n',matrix_test)

report_test = classification_report(y_test, y_pred)
print(f'The classification report for the training set is: \n {report_test}')
```

### Regression

```python
import pandas as pd
from xgboost import XGBRegressor


url = "https://raw.githubusercontent.com/sonarsushant/California-House-Price-Prediction/master/housing.csv"
df = pd.read_csv(url)
df = df.drop("ocean_proximity", axis=1)

x = df.drop("median_house_value", axis=1)
y = df[['median_house_value']]
model = XGBRegressor()
model.fit(x, y)
```

## Evaluation

## SHAP

```python
import shap
shap.initjs()

explainer = shap.Explainer(model)
shap_values = explainer(x)

shap.plots.waterfall(shap_values[0])
shap.plots.force(shap_values[0], matplotlib=True)
shap.plots.force(shap_values[:500])
shap.plots.scatter(shap_values[:, "Latitude"], color=shap_values)
shap.plots.beeswarm(shap_values)
shap.plots.bar(shap_values)
```

<figure><img src="https://3744479106-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLJiDkWvKdN4dP8wNg15P%2Fuploads%2F67LYvGoSQdbwtYQJ2GGq%2Fimage.png?alt=media&amp;token=16e90127-7ff4-4123-a656-a9900ba3e9c6" alt=""><figcaption><p>waterfall</p></figcaption></figure>

<figure><img src="https://3744479106-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLJiDkWvKdN4dP8wNg15P%2Fuploads%2FAo1o5lAf3k1EyLi9cX3o%2Fimage.png?alt=media&amp;token=df07ec4b-448a-45f4-8b7e-79da95488d84" alt=""><figcaption><p>scatter</p></figcaption></figure>

<figure><img src="https://3744479106-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLJiDkWvKdN4dP8wNg15P%2Fuploads%2FZ1gCfnKTHEPBme6Sa0gW%2Fimage.png?alt=media&amp;token=b9613b7c-f435-4638-967b-bb41e0316b50" alt=""><figcaption><p>beeswarm</p></figcaption></figure>

<figure><img src="https://3744479106-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLJiDkWvKdN4dP8wNg15P%2Fuploads%2FtZttgYa8Cr24UBrs5bwU%2Fimage.png?alt=media&amp;token=4e2f255f-b647-4f3d-8434-b755c2f67fc2" alt=""><figcaption><p>bar</p></figcaption></figure>

## References

* <https://github.com/shap/shap>
