<jupyter_start><jupyter_text>Bin loanpurposecode and loanapprovalcode<jupyter_code># Loan Purpose Code
loanpurposecode_cts = loans_df["loanpurposecode"].value_counts()
loanpurposecode_cts
# Visualize the value counts of loanapprovalcode
loanpurposecode_cts.plot.density()
# Bin loans_df.loanpurposecode_cts < 100  (reduced from 275 in version 5.1)
replace_loanpurposecode = list(loanpurposecode_cts[loanpurposecode_cts < 100].index)
# replace_loanpurposecode

# Replace in dataframe
for loan in replace_loanpurposecode:
    loans_df.loanpurposecode = loans_df.loanpurposecode.replace(loan,9999)
    
# Verify binning was successful
loans_df.loanpurposecode.value_counts()
# Loan Approval Code
loanapprovalcode_cts = loans_df["loanapprovalcode"].value_counts()
loanapprovalcode_cts
# Visualize the value counts of loanapprovalcode
loanapprovalcode_cts.plot.density()
# Bin loans_df.loanapprovalcode < 100 (No change from version 5.1)
replace_loanapprovalcode = list(loanapprovalcode_cts[loanapprovalcode_cts < 100].index)

# Replace in dataframe
for loan in replace_loanapprovalcode:
    loans_df.loanapprovalcode = loans_df.loanapprovalcode.replace(loan,9999)
    
# Verify binning was successful
loans_df.loanapprovalcode.value_counts()
loans_df.dtypes<jupyter_output><empty_output><jupyter_text>Split the Data into Training and Testing<jupyter_code># Version 5.2 - eliminate "noisy" features

X = loans_df[[
#             'loantype',
#              'loanbranch',
#              'loanpurposecode',
#              'loanapprovalcode',
             'loaninterestrate',
             'loanoriginalbalance',
#              'loanpayment',
             'loanpaymentcount',
#              'loancreditscore',
             'debtratio',
#              'loantovalue'
             'rbl'
             ]]
y = loans_df["loanchargeoffdate"]
X
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.8, random_state=1)
X_train<jupyter_output><empty_output><jupyter_text>Scaler<jupyter_code># scaler = StandardScaler().fit(X_train)
# X_train_scaled = scaler.transform(X_train)
# X_test_scaled = scaler.transform(X_test)
# X_train_scaled
# # # export 
# X_train_df = pd.DataFrame(X_train)
# X_train_df 
# X_train_df.to_csv(r"c:./csv_exports/X_train_df.csv", header = True)
# DECIDED NOT TO SCALE IN ORDER TO RETAIN INPUTS FAMILIAR TO USER<jupyter_output><empty_output><jupyter_text>Balanced Random Forest Classifier<jupyter_code># Resample the training data with the BalancedRandomForestClassifier

# Create a random forest classifier.
loan_model = BalancedRandomForestClassifier(n_estimators=200, random_state=1) 

# Fitting the model
loan_model = loan_model.fit(X_train, y_train)

# Making predictions using the testing data.
predictions = loan_model.predict(X_test)

# print(f'Training Score: {loan_model.score(X_train_scaled, y_train)}')
# print(f'Testing Score: {loan_model.score(X_test_scaled, y_test)}')
print(f'Training Score: {loan_model.score(X_train, y_train)}')
print(f'Testing Score: {loan_model.score(X_test, y_test)}')
# loantype, loanpurposecode, loaninterestrate, loanoriginalbalance, loanpayment, loancreditscore, debtratio
# loan_model.predict([[10,10,10,10,10,10,10]])
# loaninterestrate, loanoriginalbalance, loanpaymentcount, debtratio, rbl
# loan_model.predict([[10,10000,60,1,0]])
loan_model.predict([[4,100,60,.1,5]])
# predictions
# Calculated the balanced accuracy score
# from sklearn.metrics import balanced_accuracy_score

balanced_accuracy_score(y_test, predictions)
# Display the confusion matrix
# from sklearn.metrics import confusion_matrix
# -----------------
# true good  | false CO
# false good | true CO

predictions = loan_model.predict(X_test)
confusion_matrix(y_test, predictions)
# Print the imbalanced classification report
# from imblearn.metrics import classification_report_imbalanced
print(classification_report_imbalanced(y_test, predictions))
# List the features sorted in descending order by feature importance
# loans_binary_encoded.columns.tolist()

sorted(zip(loan_model.feature_importances_, X.columns), reverse=True)
# # export 
# rank_features = sorted(zip(loan_model.feature_importances_, X.columns), reverse=True)
# rank_features_df = pd.DataFrame(rank_features)
# rank_features_df 
# # rank_features_df.to_csv(r"c:./csv_exports/rank_features_df.csv", header = True)<jupyter_output><empty_output><jupyter_text>User Input<jupyter_code># r=int(input("Enter Interest Rate: "))
# amt=int(input("Enter Loan Amount: "))
# tm=int(input("Enter Loan Term in Months: "))
# dr=float(input("Enter Debt Ratio (0-1): "))
# rbl=int(input("Enter Tier (1-5): "))
# if pr == 0:
#     print("Result:  Good Loan")
# else:
#     print("Result: Bad Loan")<jupyter_output>Enter Interest Rate: 14
Enter Loan Amount: 10000
Enter Loan Term in Months: 60
Enter Debt Ratio (0-1): .45
Enter Tier (1-5): 5
Result:  Good Loan<jupyter_text>Spare code<jupyter_code># # export 
# df.to_csv(r"c:./csv_exports/file_name.csv", header = True)
# # List the features sorted in descending order by feature importance
# sorted(zip(rf_model.feature_importances_, X.columns), reverse=True)
# # loaninterestrate, loanoriginalbalance, loanpaymentcount, debtratio, rbl
# # loan_model.predict([[10,10000,60,1,0]])
# loan_model.predict([[4,100,60,.1,5]])
# def bitcoin_to_usd(btc):
#     amount = btc * 527
#     print(amount)

# btc = float( input("Input your Bitcoins amount: ") )

# bitcoin_to_usd(btc
# x=int(input("Enter first number"))
# y=int(input("Enter second number"))

# def multiplication(x,y):
#  m=x*y
# print("Multiplication result: %d" % m)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11570100143061517, 'fraction_numerical': 0.020028612303290415, 'mean_word_length': 5.746682750301568, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '180575', 'license': 'MIT', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1898, 'n_words': 382, 'path': 'Segment_3/credit_risk6.0.ipynb', 'repo_name': 'honoruru/Final_Project', 'size': '5592'}