File size: 1,110 Bytes
067ea44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from sklearn.model_selection import train_test_split
import xgboost as xgb
from sklearn.preprocessing import OneHotEncoder, LabelEncoder, StandardScaler
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error, accuracy_score, classification_report, confusion_matrix
from utils.data import get_cleaned_data
   
def create_model():
    df = get_cleaned_data()
    #feature selection
    X = df.drop('credit_risk', axis=1)
    y = df['credit_risk']


    #split data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.transform(X_test)


    #train model
    model = xgb.XGBClassifier(objective='binary:logistic', random_state=42)
    model.fit(X_train, y_train)

    #evaluate model
    y_pred = model.predict(X_test)

    #print("classification report /n:", classification_report(y_test, y_pred)) 
    #print("R2", r2_score(y_test, y_pred))
   # print("MSE:", mean_squared_error(y_test, y_pred))
    return model, scaler