<jupyter_start><jupyter_text>Dataset page: https://datahack.analyticsvidhya.com/contest/practice-problem-loan-prediction-iii/  Preprocessing<jupyter_code>from sklearn import datasets
import pandas as pd
from matplotlib import cm
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv('loan_train.csv')
df.shape
df.isnull().sum()
df = df.dropna()
df = df.drop('Loan_ID', axis=1)
df.shape
df.head()
df['Dependents'] = df.Dependents.apply(lambda x: 3 if x=='3+' else x).astype(int)

for c,v in zip(['Gender', 'Married', 'Education','Self_Employed', 'Property_Area', 'Loan_Status'],
             ['Male',   'Yes',     'Yes',           'Urban',         'Y']): 
    df[c] = df[c].apply(lambda x: 1 if x== v else 0)
df.head()

from sklearn.model_selection import train_test_split
df_train, df_test = train_test_split(df, stratify=df.Loan_Status, test_size=0.2)
X_train, y_train = df_train.iloc[:,1:-1], df_train.iloc[:,-1]
X_test, y_test = df_test.iloc[:,1:-1], df_test.iloc[:,-1]<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>%%time
from sklearn import tree

clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, y_train)
y_hat = clf.predict(X_test)

ax = tree.plot_tree(clf);
ax;
from sklearn.metrics import roc_curve, roc_auc_score, accuracy_score
accuracy_score(y_test, y_hat)<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>from sklearn.ensemble import RandomForestClassifier

clf = RandomForestClassifier()
clf = clf.fit(X_train, y_train)
y_hat = clf.predict(X_test)
accuracy_score(y_test, y_hat)<jupyter_output>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/ensemble/forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.
  "10 in version 0.20 to 100 in 0.22.", FutureWarning)<jupyter_text>SVM<jupyter_code>%%time
from sklearn.svm import SVC

clf = SVC(kernel='linear', C=1,gamma='auto')

# Somente 2 features para poder plotar boundaries.
clf = clf.fit(X_train, y_train)
y_hat = clf.predict(X_test)
accuracy_score(y_test, y_hat)<jupyter_output>CPU times: user 2min 52s, sys: 770 ms, total: 2min 53s
Wall time: 3min 8s<jupyter_text>Naive Bayes<jupyter_code>%%time
from sklearn.naive_bayes import GaussianNB
clf = GaussianNB()
clf = clf.fit(X_train, y_train)
y_hat = clf.predict(X_test)
accuracy_score(y_hat, y_test)<jupyter_output>CPU times: user 4.33 ms, sys: 121 µs, total: 4.45 ms
Wall time: 4.48 ms<jupyter_text>KNN<jupyter_code>pd.np.empty()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1346456692913386, 'fraction_numerical': 0.02637795275590551, 'mean_word_length': 6.097765363128492, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '142428', 'license': 'Apache-2.0', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1008, 'n_words': 205, 'path': 'Loan Dataset.ipynb', 'repo_name': 'srodriguex/machine_learning_study', 'size': '2540'}