Py.Cafe

sunilraj-B/

streamlit-pycafe-examples

Streamlit on Py.cafe: Interactive Examples

DocsPricing
  • app.py
  • requirements.txt
app.py
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
import streamlit as st
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

# Load dataset
@st.cache
def load_data():
    data = pd.DataFrame({
        'Gender': ['Male', 'Female', 'Male', 'Female'],
        'Married': ['Yes', 'No', 'Yes', 'No'],
        'Education': ['Graduate', 'Not Graduate', 'Graduate', 'Graduate'],
        'ApplicantIncome': [5000, 3000, 4000, 2500],
        'LoanAmount': [200, 100, 150, 120],
        'Loan_Status': [1, 0, 1, 0]
    })
    return data

# Prepare data for training
def preprocess_data(data):
    data['Gender'] = data['Gender'].map({'Male': 1, 'Female': 0})
    data['Married'] = data['Married'].map({'Yes': 1, 'No': 0})
    data['Education'] = data['Education'].map({'Graduate': 1, 'Not Graduate': 0})
    X = data[['Gender', 'Married', 'Education', 'ApplicantIncome', 'LoanAmount']]
    y = data['Loan_Status']
    return X, y

# Train model
def train_model(X, y):
    model = RandomForestClassifier(random_state=42)
    model.fit(X, y)
    return model

# Streamlit app
st.title("Loan Eligibility Prediction App")
st.write("Enter applicant details to check loan eligibility.")

# User inputs
gender = st.selectbox("Gender", ["Male", "Female"])
married = st.selectbox("Married", ["Yes", "No"])
education = st.selectbox("Education", ["Graduate", "Not Graduate"])
applicant_income = st.number_input("Applicant Income", min_value=0, value=3000)
loan_amount = st.number_input("Loan Amount", min_value=0, value=100)

# Load and preprocess data
data = load_data()
X, y = preprocess_data(data)
model = train_model(X, y)

# Predict eligibility
if st.button("Predict"):
    user_data = pd.DataFrame({
        'Gender': [1 if gender == "Male" else 0],
        'Married': [1 if married == "Yes" else 0],
        'Education': [1 if education == "Graduate" else 0],
        'ApplicantIncome': [applicant_income],
        'LoanAmount': [loan_amount]
    })
    prediction = model.predict(user_data)
    result = "Eligible" if prediction[0] == 1 else "Not Eligible"
    st.success(f"The applicant is {result} for the loan.")