Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9e05bf

Browse files
authored
Merge pull request#106 from gauravtrivedi967/patch-2
Create CreditRisk.py
2 parents58790b0 +b2fd605 commitd9e05bf

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

‎CreditRisk.py‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#%%
2+
importwarnings
3+
warnings.filterwarnings('ignore')
4+
5+
importos
6+
importnumpyasnp
7+
importpandasaspd
8+
importscipy.statsasst
9+
importstatsmodels.apiassm
10+
importmatplotlib.pyplotasplt
11+
12+
fromsklearnimportlinear_model
13+
fromsklearn.metricsimportmean_squared_error,r2_score,confusion_matrix,accuracy_score
14+
fromsklearn.model_selectionimporttrain_test_split
15+
fromsklearn.metricsimportroc_curve
16+
fromsklearn.metricsimportroc_auc_score
17+
# %%
18+
os.chdir('D:/machine_learning/DATA_SET/credit_risk')
19+
df_a=pd.read_csv('credit_risk.csv')
20+
df_a.head()
21+
# %%
22+
df_a.isnull().sum()
23+
# %%
24+
df_a.dropna(axis=0,inplace=True)
25+
df_a.isnull().sum()
26+
# %%
27+
df_a.dtypes
28+
# %%
29+
df_a["CoapplicantIncome"]=df_a["CoapplicantIncome"].astype('float')
30+
df_a["ApplicantIncome"]=df_a["ApplicantIncome"].astype('float')
31+
print("AFTER CHANGING INT & OBJECT TO FLOAT")
32+
df_a.dtypes
33+
# %%
34+
df_a["Dependents"].value_counts()
35+
# %%
36+
cleanup_nums= {
37+
"Married": {"Yes":1,"No":0},
38+
"Self_Employed": {"Yes":1,"No":0},
39+
"Property_Area": {"Rural":0,"Urban":1,"Semiurban":2},
40+
"Education": {"Graduate":1,"Not Graduate":0},
41+
"Loan_Status": {"Y":1,"N":0},
42+
"Gender": {"Male":1,"Female":0},
43+
"Dependents": {"0":0,"1":1,"2":2,"3+":3},
44+
}
45+
46+
df_a.replace(cleanup_nums,inplace=True)
47+
df_a.head()
48+
# %%
49+
df_corr=df_a[['Gender','Married','Dependents','Education',
50+
'Self_Employed','ApplicantIncome','CoapplicantIncome','LoanAmount',
51+
'Loan_Amount_Term','Credit_History','Property_Area','Loan_Status']]
52+
53+
df_corr.head()
54+
# %%
55+
plt.imshow(df_corr.corr(),cmap=plt.cm.Blues ,interpolation='nearest')
56+
cmap='coolwarm'
57+
plt.colorbar()
58+
tick_marks= [iforiinrange(len(df_corr.columns))]
59+
plt.xticks(tick_marks,df_corr.columns,rotation='vertical')
60+
plt.yticks(tick_marks,df_corr.columns)
61+
plt.show()
62+
#%%
63+
boolean_col='Loan_Status'
64+
cols= ['Gender','Married','Dependents','Education','Self_Employed',
65+
'ApplicantIncome','CoapplicantIncome','Credit_History'
66+
]
67+
68+
xTrain=df_a[cols].values
69+
yTrain=df_a[boolean_col].values
70+
# %%
71+
st.chisqprob=lambdachisq,df:st.chi2.sf(chisq,df)
72+
73+
model=sm.Logit(yTrain,xTrain )
74+
result=model.fit()
75+
result.summary(xname=cols,yname=boolean_col,title='Logit Model',alpha=1)
76+
# %%
77+
defcalculate_accuracy(predictions,real):
78+
correct=0
79+
foriinrange(len(predictions)):
80+
ifround(predictions[i])==round(real[i]):
81+
correct+=1
82+
returncorrect*1.0/len(predictions)
83+
# %%
84+
train_predictions=result.predict(xTrain)
85+
86+
train_accuracy=calculate_accuracy(train_predictions,yTrain )
87+
print("Train Accuracy: ",train_accuracy*100 )
88+
# %%
89+
train_predictions= (train_predictions>0.5)
90+
91+
train_cm=confusion_matrix(yTrain,train_predictions,labels= [1.0,0.0])
92+
print(train_cm )
93+
# %%
94+
labels= ['0','1']
95+
cm=train_cm
96+
fig=plt.figure()
97+
ax=fig.add_subplot(111)
98+
cax=ax.matshow(cm,cmap='viridis')
99+
plt.title('Confusion matrix')
100+
fig.colorbar(cax)
101+
ax.set_xticklabels(['']+labels)
102+
ax.set_yticklabels(['']+labels)
103+
104+
r=0
105+
c=0
106+
forlistItemincm:
107+
forcellIteminlistItem:
108+
ax.text(c,r,cellItem,va='center',ha='center',color='r')
109+
c+=1
110+
c=0
111+
r+=1
112+
113+
plt.xlabel('Actual label')
114+
plt.ylabel('Predicted label')
115+
plt.show()
116+
# %%

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp