การพยากรณ์ความน่าจะเป็นของการผิดนัดชำระหนี้

การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

Michael Crabtree

Data Scientist, Ford Motor Company

สัมประสิทธิ์ของ logistic regression

# Model Intercept
array([-3.30582292e-10])
# Coefficients for ['loan_int_rate','person_emp_length','person_income']
array([[ 1.28517496e-09, -2.27622202e-09, -2.17211991e-05]])

สูตรคำนวณความน่าจะเป็นของการผิดนัดชำระหนี้ด้วย logistic regression

# Calculating probability of default
int_coef_sum = -3.3e-10 +
    (1.29e-09 * loan_int_rate) + (-2.28e-09 * person_emp_length) + (-2.17e-05 * person_income)
prob_default = 1 / (1 + np.exp(-int_coef_sum))
prob_nondefault = 1 - (1 / (1 + np.exp(-int_coef_sum)))
การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

การตีความสัมประสิทธิ์

# Intercept
intercept = -1.02
# Coefficient for employment length
person_emp_length_coef = -0.056
  • ทุกๆ 1 ปีที่ person_emp_length เพิ่มขึ้น โอกาสผิดนัดชำระหนี้จะลดลง
การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

การตีความสัมประสิทธิ์

# Intercept
intercept = -1.02
# Coefficient for employment length
person_emp_length_coef = -0.056
  • ทุกๆ 1 ปีที่ person_emp_length เพิ่มขึ้น โอกาสผิดนัดชำระหนี้จะลดลง
intercept person_emp_length value * coef probability of default
-1.02 10 (10 * -0.06) .17
-1.02 11 (11 * -0.06) .16
-1.02 12 (12 * -0.06) .15
การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

การใช้คอลัมน์ที่ไม่ใช่ตัวเลข

  • ตัวเลข: loan_int_rate, person_emp_length, person_income

  • ไม่ใช่ตัวเลข:

    cr_loan_clean['loan_intent']
    
EDUCATION            
MEDICAL              
VENTURE              
PERSONAL             
DEBTCONSOLIDATION   
HOMEIMPROVEMENT
  • จะเกิดข้อผิดพลาดกับโมเดล machine learning ใน Python หากไม่ผ่านการประมวลผลก่อน
การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

One-hot encoding

  • แปลงข้อความให้เป็นตัวเลข

ตัวอย่างข้อมูล loan intent ในชุดข้อมูล

การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

One-hot encoding

  • แปลงข้อความให้เป็นตัวเลข
  • 0 หรือ 1 ในคอลัมน์ใหม่ชื่อ column_VALUE

ตัวอย่างการทำ one-hot encoding กับคอลัมน์ loan intent

การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

Get dummies

  • ใช้ฟังก์ชัน get_dummies() ใน pandas
# Separate the numeric columns
cred_num = cr_loan.select_dtypes(exclude=['object'])
# Separate non-numeric columns
cred_cat = cr_loan.select_dtypes(include=['object'])
# One-hot encode the non-numeric columns only
cred_cat_onehot = pd.get_dummies(cred_cat)
# Union the numeric columns with the one-hot encoded columns
cr_loan = pd.concat([cred_num, cred_cat_onehot], axis=1)
การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

การพยากรณ์ล่วงหน้าด้วยความน่าจะเป็น

  • ใช้เมธอด .predict_proba() ใน scikit-learn
# Train the model
clf_logistic.fit(X_train, np.ravel(y_train))
# Predict using the model
clf_logistic.predict_proba(X_test)
  • สร้าง array ของความน่าจะเป็นในการผิดนัดชำระหนี้
# Probabilities: [[non-default, default]]
array([[0.55, 0.45]])
การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

มาฝึกกันเถอะ!

การสร้างโมเดลความเสี่ยงด้านเครดิตด้วย Python

Preparing Video For Download...