<jupyter_start><jupyter_text>Load the dataset- Load the train data and using all your knowledge try to explore the different statistical properties of the dataset.<jupyter_code># Code starts here
train=pd.read_csv("train.csv")
# Code ends here
train.head()
train.drop(['Id','customer.id','inq.last.6mths','pub.rec'],axis=1,inplace=True)
train.head()
train['revol.bal'].max()
train['revol.bal'].min()
train.shape
train['revol.bal'].plot(kind='box')
train_1=train[train['revol.bal'] < 300000]
train_1 = train_1.reset_index(drop=True)

#new_dataframe = a_dataframe[a_dataframe.B <= 3]
#new_dataframe = new_dataframe.reset_index(drop=True)

train_1
train_1.shape 
train_1.isnull().sum()
train_1.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 7651 entries, 0 to 7650
Data columns (total 12 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   credit.policy      7651 non-null   object 
 1   purpose            7651 non-null   object 
 2   int.rate           7651 non-null   object 
 3   installment        7651 non-null   float64
 4   log.annual.inc     7651 non-null   float64
 5   dti                7651 non-null   float64
 6   fico               7651 non-null   int64  
 7   days.with.cr.line  7651 non-null   float64
 8   revol.bal          7651 non-null   int64  
 9   revol.util         7651 non-null   float64
 10  delinq.2yrs        7651 non-null   object 
 11  paid.back.loan     7651 non-null   object 
dtypes: float64(5), int64(2), object(5)
memory usage: 717.4+ KB<jupyter_text>Visualize the data- Check for the categorical & continuous features. - Check out the best plots for plotting between categorical target and continuous features and try making some inferences from these plots.- Clean the data, apply some data preprocessing and engineering techniques.<jupyter_code># Code starts here

fully_paid=train_1['paid.back.loan'].value_counts()
fully_paid_percentage=(train_1['paid.back.loan'].value_counts())/len(train_1['paid.back.loan'])
print(fully_paid)
print(fully_paid_percentage)
plt.bar(fully_paid.index,fully_paid)
# Code ends here.
train_1['int.rate']=train_1['int.rate'].map(lambda x: str(x)[:-1])
train_1['int.rate']=train_1['int.rate'].astype(float)/100
train.head()
num_df=train_1.select_dtypes(include=['number']).copy()
cat_df=train_1.select_dtypes(include=['object']).copy()
num_df.head()
cat_df.head()
plt.figure(figsize=(20,20))
cols=list(num_df)
print(cols)

fig,axes=plt.subplots(8,1,figsize=(10,20))
for i in range(8):
    sns.boxplot(x=train_1['paid.back.loan'],y=num_df[cols[i]],ax=axes[i])
    fig.tight_layout()
cols1=list(cat_df)
cols1
fig,axes=plt.subplots(2,2,figsize=(10,20))
for i in range(2):
    for j in range(2):
        sns.countplot(x=train_1[cols1[i*2+j]],hue=train_1['paid.back.loan'],ax=axes[i,j])
        fig.tight_layout()<jupyter_output><empty_output><jupyter_text>Model building- Separate the features and target.- Now let's come to the actual task, using Decision Tree, predict the `paid.back.loan`. Use different techniques you have learned to imporove the performance of the model.- Try improving upon the `accuracy_score` ([Accuracy Score](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html))<jupyter_code># Code Starts here
X=train_1.drop('paid.back.loan',axis=1)
y=train_1['paid.back.loan']
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=6)
le=LabelEncoder()
for col in cat_df.columns[:-1]:
    X_train[col].fillna('NA',inplace=True)
    X_test[col].fillna('NA',inplace=True)
    X_train[col]=le.fit_transform(X_train[col])
    X_test[col]=le.transform(X_test[col])
y_train.replace({'No':0,'Yes':1},inplace=True)  
y_test.replace({'No':0,'Yes':1},inplace=True)    
model=DecisionTreeClassifier(random_state=6)
model.fit(X_train,y_train)
score=model.score(X_test,y_test)
score
# Code ends here
parameter_grid={'max_depth':np.arange(3,10),'min_samples_leaf':range(10,50,10)}
model_2=DecisionTreeClassifier(random_state=6)
p_tree=GridSearchCV(model_2,parameter_grid,cv=5)
p_tree.fit(X_train,y_train)
score_2=p_tree.score(X_test,y_test)
score_2
print("Feature importances are:", dict(zip(train_1.columns, np.round(model.feature_importances_,3))))
dot_data = tree.export_graphviz(decision_tree=p_tree.best_estimator_, out_file=None,
                                feature_names=X.columns, filled = True,  
                                class_names=['paid.back.loan_yes','paid.back.loan_no'])

# Draw graph
graph_big = pydotplus.graph_from_dot_data(dot_data)  

# Show graph
Image(graph_big.create_png())<jupyter_output><empty_output><jupyter_text>Prediction on the test data and creating the sample submission file.- Load the test data and store the `Id` column in a separate variable.- Perform the same operations on the test data that you have performed on the train data.- Create the submission file as a `csv` file consisting of the `Id` column from the test data and your prediction as the second column.<jupyter_code># Code Starts here
test=pd.read_csv('test.csv')
Id=test['Id'].copy()
test.drop(['Id','customer.id','inq.last.6mths','pub.rec'],axis=1,inplace=True)


# Code ends here
test.head()
test['int.rate']=test['int.rate'].map(lambda x: str(x)[:-1])
test['int.rate']=test['int.rate'].astype(float)/100
test[col].fillna('NA',inplace=True)
test.info()
cat=test.select_dtypes(include=['object']).copy()
cat.head()
for col in cat.columns:
    test[col]=le.fit_transform(test[col])
y_pred = p_tree.predict(test)
y_pred

sample_submission = pd.DataFrame({'Id':Id,'paid.back.loan':y_pred})
sample_submission['paid.back.loan'].replace({0:'No',1:'Yes'},inplace=True) 

# Convert the sample submission file into a csv file
sample_submission.to_csv('3rd_submission.csv',index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1456544142906218, 'fraction_numerical': 0.03297835795259361, 'mean_word_length': 4.966188524590164, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '581760', 'license': 'MIT', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2058, 'n_words': 405, 'path': 'Loan_Defaulters_Using_Decision_Tree/Loan_Defaulters_Student_Template.ipynb', 'repo_name': 'Monika199211/ga-learner-dst-repo', 'size': '5822'}