Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cordon-thiago/766c2d25a6e6d483ba20062a1d2b23bc to your computer and use it in GitHub Desktop.
Save cordon-thiago/766c2d25a6e6d483ba20062a1d2b23bc to your computer and use it in GitHub Desktop.
def oversampleSMOTE(X, y):
'''
Resample a dataset using SMOTE oversample
Input:
X = dataframe with x variables (explanatory variables)
y = dataframe with y variable (variable to predict)
Output:
df[0] = X dataframe resampled
df[1] = y dataframe resampled
'''
from imblearn.over_sampling import SMOTE
import pandas as pd
sm = SMOTE(random_state=123)
X_resampled, y_resampled = sm.fit_resample(X, y.ravel())
# Get column names
X_cols = X.columns.values
y_cols = [y.name]
return pd.DataFrame(X_resampled, columns=X_cols) , pd.DataFrame(y_resampled, columns=y_cols)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Import libraries\n",
"from functions import aux_functions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Oversample dataset"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"x_vars = ['monthsSinceRegDate', 'age', 'percNumbersInEmailUser', 'emailUserCharQty', \n",
" 'emailDomain_cat_gmail.com', 'emailDomain_cat_hotmail.com',\n",
" 'emailDomainPiece1_com',\n",
" 'emailDomainPiece2_br', 'emailDomainPiece2_missing']\n",
"\n",
"X = hardbounce_dummy[x_vars]\n",
"\n",
"y = hardbounce_dummy['flgHardBounce_n']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"X_resampled, y_resampled = aux_functions.oversampleSMOTE(X, y)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"flgHardBounce_n\n",
"0 0.5\n",
"1 0.5\n",
"Name: __dummy__, dtype: float64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"aux_functions.freqTable(\n",
" y_resampled['flgHardBounce_n']\n",
" ,\"\"\n",
" ,False\n",
" ,\"columns\"\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment