# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/woodardmadeline/KeatonParkinsonResume/blob/master/starter_bank.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="mA0HPVmIBT4C"
import pandas as pd

campaign = pd.read_csv('https://raw.githubusercontent.com/byui-cse/cse450-course/master/data/bank.csv')

# + id="StiU5QcPPxqQ" colab={"base_uri": "https://localhost:8080/"} outputId="4209764e-b988-4cc4-fc8d-05eae6e39c15"
#campaign_na =(campaign
 #   .replace('unknown', np.nan, regex=True))
#campaign = campaign_na.dropna()
from imblearn.over_sampling import RandomOverSampler
# https://imbalanced-learn.readthedocs.io/en/stable/user_guide.html


# Let's over sample the minority class, which samples with replacement until the
# majority (died) and the minority (survived) are equal
ro = RandomOverSampler()

# + id="ALLMN63FPyEQ" colab={"base_uri": "https://localhost:8080/", "height": 456} outputId="bf748308-a8fc-4dcd-f74c-77e45726d974"
# new data with only pertaining data

#df.join(pd.get_dummies(df['type']))

campaignEncoded = pd.get_dummies(campaign, columns=['job','marital','education','default', 'housing', 'loan','contact','month','day_of_week','poutcome'])
campaignEncoded

# + id="RnGBwGVZPyyh" colab={"base_uri": "https://localhost:8080/"} outputId="26e038d4-ab5c-4227-dd87-8ed4d1c2269e"
y = campaignEncoded['y']
y

# + id="CVSfaqgKPzE2" colab={"base_uri": "https://localhost:8080/", "height": 456} outputId="31421a98-42f4-42b1-958d-1bf0469710fd"
X = campaignEncoded.drop(['y', 'poutcome_nonexistent',	'poutcome_success','previous','emp.var.rate','cons.price.idx','cons.conf.idx'], axis = 1)
X

# + colab={"base_uri": "https://localhost:8080/"} id="jG5JXtgYGQCB" outputId="2bbf2cb0-60f9-4878-dd1e-12a8d414afbc"
X_new, y_new = ro.fit_resample(X, y)

# Convert this to a dataframe and check the counts, now they're equal, because
# we have a bunch of duplicate survivors
campaignEncoded = pd.DataFrame(y_new)
campaignEncoded.value_counts()

# + id="samtskSpGRgP"
# Import and use the train_test_split() function to split the X and y
# dataframes into training and test sets.
#
# The training data should contain 80% of the samples and
# the test data should contain 20% of the samples.
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X_new, y_new, test_size=.2)

# + colab={"base_uri": "https://localhost:8080/"} id="O19z6BZUGUbE" outputId="113545c9-f253-46f3-d0a1-cac77ff5be38"
from sklearn import tree
from sklearn.naive_bayes import GaussianNB
from sklearn import metrics

tree_clf = tree.DecisionTreeClassifier()
tree_clf.fit(X_train, y_train)
y_pred = tree_clf.predict(X_test)


print("Accuracy:",metrics.accuracy_score(y_test, y_pred))

# + colab={"base_uri": "https://localhost:8080/", "height": 231} id="DBYZnJ8CGaRw" outputId="702d48c7-2044-420b-c5c1-21a6aa79f289"
import altair as alt

feature_dat = pd.DataFrame({
    "values": tree_clf.feature_importances_,
    "features": X_train.columns
    }) 
feature = alt.Chart(feature_dat.query('values > .02'), title='Most Valuable Features').encode(
    alt.X('values', title= 'Rank Value'), 
    alt.Y('features', sort = "-x", title= 'Features')).mark_bar()

# + id="13oNUu6bGeVr"
print(metrics.confusion_matrix(y_test, y_pred))



tree = metrics.plot_confusion_matrix(tree_clf, X_test, y_test)

print(metrics.classification_report(y_test, y_pred))


StarcoderdataJupyterScriptsDedupFiltered