This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from flask_sqlalchemy import SQLAlchemy | |
from flask_marshmallow import Marshmallow | |
from flask_restful import Resource, Api | |
app = Flask(__name__) | |
api = Api(app) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db' | |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. Library imports | |
import pandas as pd | |
from sklearn.ensemble import RandomForestClassifier | |
from pydantic import BaseModel | |
import joblib | |
# 2. Class which describes a single flower measurements | |
class IrisSpecies(BaseModel): | |
sepal_length: float |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from missingpy import MissForest | |
# Make an instance and perform the imputation | |
imputer = MissForest() | |
X = iris.drop('species', axis=1) | |
X_imputed = imputer.fit_transform(X) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exp = explainer.explain_instance( | |
data_row=X_test.iloc[4], | |
predict_fn=model.predict_proba | |
) | |
exp.show_in_notebook(show_table=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: str = 'Bob' | |
age: int = 32 | |
rating: float = 7.9 | |
is_premium: bool = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Remap to integers | |
df['GENDER'] = [0 if x == 'M' else 1 for x in df['GENDER']] | |
df['CAR'] = [1 if x == 'Y' else 0 for x in df['CAR']] | |
df['REALITY'] = [1 if x == 'Y' else 0 for x in df['REALITY']] | |
# Create dummy variables | |
dummy_income_type = pd.get_dummies(df['INCOME_TYPE'], prefix='INC_TYPE', drop_first=True) | |
dummy_edu_type = pd.get_dummies(df['EDUCATION_TYPE'], prefix='EDU_TYPE', drop_first=True) | |
dummy_family_type = pd.get_dummies(df['FAMILY_TYPE'], prefix='FAM_TYPE', drop_first=True) | |
dummy_house_type = pd.get_dummies(df['HOUSE_TYPE'], prefix='HOUSE_TYPE', drop_first=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ax = df['TARGET'].value_counts().plot(kind='bar', figsize=(10, 6), fontsize=13, color='#087E8B') | |
ax.set_title('Credit card fraud (0 = normal, 1 = fraud)', size=20, pad=30) | |
ax.set_ylabel('Number of transactions', fontsize=14) | |
for i in ax.patches: | |
ax.text(i.get_x() + 0.19, i.get_height() + 700, str(round(i.get_height(), 2)), fontsize=15) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from imblearn.over_sampling import SMOTE | |
sm = SMOTE(random_state=42) | |
X_sm, y_sm = sm.fit_resample(X, y) | |
print(f'''Shape of X before SMOTE: {X.shape} | |
Shape of X after SMOTE: {X_sm.shape}''') | |
print('\nBalance of positive and negative classes (%):') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lime_exp.as_list() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shap.summary_plot(shap_values, X) |
NewerOlder