File size: 4,300 Bytes
dcd714c |
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
"""i
mport streamlit as st
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from joblib import dump, load
# Load and process data
data = pd.read_csv("C:/Users/lenovo/Downloads/Create_score_model/clean_train.csv")
X = data.drop('Credit_Score', axis=1)
y = data['Credit_Score']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train.head()
# Train the model
rf = RandomForestClassifier(random_state=42)
rf.fit(X_train, y_train)
# Save the model
#dump(rf, 'credit_classifier.joblib', compress=('gzip', 9))
# Streamlit App
# %%writefile scoring_app.py
loaded_model = load(rf)
# Streamlit interface
st.title("Credit Score Prediction App")
# Input fields
feature1 = st.number_input("Outstanding Debt")
feature2 = st.number_input("Credit Mix")
feature3 = st.number_input("Credit History Age (in months)")
feature4 = st.number_input("Monthly Balance")
feature5 = st.number_input("Payment Behaviour")
feature6 = st.number_input("Annual Income")
feature7 = st.number_input("Number of Delayed Payments")
# Define target names
target_names = {0: "Good", 1: "Poor", 2: "Standard"}
if st.button("Predict"):
prediction = loaded_model.predict([[feature1, feature2, feature3, feature4, feature5, feature6, feature7]])
st.write(f"Predicted Class: {target_names[prediction[0]]}")
"""
import streamlit as st
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from joblib import dump, load
@st.cache_resource
def load_and_train_model():
try:
# Load and process data
data = pd.read_csv("clean_train.csv")
except FileNotFoundError:
st.error("Error: The file 'clean_train.csv' was not found. Please upload the file.")
st.stop()
# Splitting data into features and target
X = data.drop('Credit_Score', axis=1)
y = data['Credit_Score']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train the Random Forest model
rf = RandomForestClassifier(random_state=42)
rf.fit(X_train, y_train)
# Save the trained model to a file
dump(rf, 'credit_classifier.joblib')
return rf
@st.cache_resource
def load_model():
import os
if os.path.exists('credit_classifier.joblib'):
try:
return load('credit_classifier.joblib')
except Exception as e:
st.warning(f"Error loading model: {e}. Retraining the model...")
return load_and_train_model()
else:
st.warning("Model file not found. Training a new model...")
return load_and_train_model()
# Load the trained model
loaded_model = load_model()
# Streamlit App
st.title("Credit Score Prediction App")
st.markdown("Predict the credit score category based on user inputs.")
# Input fields
st.sidebar.header("Enter Feature Values:")
feature1 = st.sidebar.number_input("Outstanding Debt", min_value=0.0, step=100.0)
feature2 = st.sidebar.selectbox("Credit Mix (0=Bad, 1=Good, 2=Excellent)", [0, 1, 2])
feature3 = st.sidebar.number_input("Credit History Age (in months)", min_value=0.0, step=1.0)
feature4 = st.sidebar.number_input("Monthly Balance", min_value=0.0, step=100.0)
feature5 = st.sidebar.number_input("Payment Behaviour", min_value=0.0, step=1.0)
feature6 = st.sidebar.number_input("Annual Income", min_value=0.0, step=1000.0)
feature7 = st.sidebar.number_input("Number of Delayed Payments", min_value=0, step=1)
# Define target names
target_names = {0: "Good", 1: "Poor", 2: "Standard"}
# Predict Button
if st.button("Predict"):
try:
# Prepare the input as a 2D array
inputs = np.array([[feature1, feature2, feature3, feature4, feature5, feature6, feature7]])
# Make prediction
prediction = loaded_model.predict(inputs)
# Display result
st.success(f"Predicted Credit Score: {target_names[prediction[0]]}")
except Exception as e:
st.error(f"An error occurred during prediction: {e}")
|