<jupyter_start><jupyter_text>IMPORTING LIBRARIES<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.preprocessing import StandardScaler

from sklearn.metrics import confusion_matrix
from sklearn import metrics
loans= pd.read_csv('loan_data.csv')
loans.head()<jupyter_output><empty_output><jupyter_text>HANDLING CATEGORICAL DATA<jupyter_code>import pandas as pd 
from sklearn.preprocessing import LabelEncoder

x = loans.iloc[:, :-1].values 
y = loans.iloc[:, -1].values 
labelencoder_X = LabelEncoder()
x[:,0] = labelencoder_X.fit_transform(x[:,0])
x[:,1] = labelencoder_X.fit_transform(x[:,1])
print(x)
#Here we are using LabelEncoder<jupyter_output>[[1 2 0.1189 ... 0 0 0]
 [1 1 0.1071 ... 0 0 0]
 [1 2 0.1357 ... 1 0 0]
 ...
 [0 2 0.1071 ... 8 0 0]
 [0 4 0.16 ... 5 0 0]
 [0 2 0.1392 ... 6 0 0]]<jupyter_text>GRAPHS<jupyter_code>plt.figure(figsize=(10,6))
loans[loans['credit.policy']==1]['fico'].hist(alpha=0.5,color='blue',
                                              bins=30,label='Credit.Policy=1')
loans[loans['credit.policy']==0]['fico'].hist(alpha=0.5,color='red',
                                              bins=30,label='Credit.Policy=0')
plt.legend()
plt.xlabel('FICO')<jupyter_output><empty_output><jupyter_text>SPLITTING OF DATASET INTO TRAIN AND TEST SET<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.30, random_state=101)<jupyter_output><empty_output><jupyter_text>NORMALIZING DATA<jupyter_code>sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)<jupyter_output><empty_output><jupyter_text>ALGORITHM<jupyter_code>dt = DecisionTreeRegressor()<jupyter_output><empty_output><jupyter_text>FITTING OF TRAINING DATA<jupyter_code>dt.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>PREDICTIONS FOR OUR PROBLEM STATEMENT WITH THE RELATED GRAPHS<jupyter_code>predictions = dt.predict(X_test)
plt.scatter(y_test,predictions)<jupyter_output><empty_output><jupyter_text>EVALUATION<jupyter_code>print('Mean abolute error is:', metrics.mean_absolute_error(y_test,predictions))
print('Mean squared error is:', metrics.mean_squared_error(y_test,predictions))
print('Root mean squared error is:', np.sqrt(metrics.mean_squared_error(y_test,predictions)))
print(metrics.accuracy_score(y_test, predictions))<jupyter_output>0.7334725121781489<jupyter_text>SAVING THE MODEL USING PICKLE LIBRARY<jupyter_code>import pickle 

# Save the trained model as a pickle string. 
saved_model = pickle.dumps(dt) 

# Load the pickled model 
model_from_pickle = pickle.loads(saved_model) 

# Use the loaded pickled model to make predictions 
model_from_pickle.predict(X_test)<jupyter_output><empty_output><jupyter_text>ACCURACY w.r.t TRAINED DATA Confusion Matrix<jupyter_code>from sklearn.metrics import classification_report,confusion_matrix
y_train = y_train[0:2874]
print(confusion_matrix(y_train,predictions))<jupyter_output>[[1943  474]
 [ 370   87]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>print(classification_report(y_train,predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.84      0.80      0.82      2417
           1       0.16      0.19      0.17       457

    accuracy                           0.71      2874
   macro avg       0.50      0.50      0.50      2874
weighted avg       0.73      0.71      0.72      2874<jupyter_text>ACCURACY w.r.t TEST DATA Confusion Matrix<jupyter_code>print(confusion_matrix(y_test,predictions))<jupyter_output>[[1989  442]
 [ 324  119]]<jupyter_text>Precision, Recall, Support, Fscore<jupyter_code>print(classification_report(y_test,predictions))<jupyter_output>precision    recall  f1-score   support

           0       0.86      0.82      0.84      2431
           1       0.21      0.27      0.24       443

    accuracy                           0.73      2874
   macro avg       0.54      0.54      0.54      2874
weighted avg       0.76      0.73      0.75      2874
{'fraction_non_alphanumeric': 0.10901162790697674, 'fraction_numerical': 0.06153100775193798, 'mean_word_length': 3.986714975845411, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '466236', 'license': 'MIT', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1502, 'n_words': 273, 'path': 'Decision Trees/ML-DT/ASSIGNMENT/ML-ASGN-DT-02/CODE/Loans___DT.ipynb', 'repo_name': 'RASHI3004/Project-Internship---ML-DL', 'size': '4128'}