File size: 1,974 Bytes
d01bcf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
import joblib


np.random.seed(42)

n_samples = 700

data = {
    "age": np.random.randint(18, 65, n_samples),
    "monthly_income": np.random.randint(200, 2000, n_samples),
    "debt_amount": np.random.randint(0, 5000, n_samples),
    "job_years": np.random.randint(0, 20, n_samples)
}

df = pd.DataFrame(data)

# Target (0 = to‘laydi, 1 = muammo bo‘lishi mumkin)
df["risk"] = (
    (df["monthly_income"] < 500).astype(int) +
    (df["debt_amount"] > 3000).astype(int) +
    (df["job_years"] < 2).astype(int)
)

df["risk"] = (df["risk"] >= 2).astype(int)

print(df.head())

feature_cols = ["age", "monthly_income", "debt_amount", "job_years"]

X = df[feature_cols].values
y = df["risk"].values



X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=200, random_state=42, stratify=y
)

print("Train:", len(X_train))
print("Test:", len(X_test))



model = LogisticRegression(max_iter=1000)
model.fit(X_train, y_train)

y_pred = model.predict(X_test)


acc = accuracy_score(y_test, y_pred)
print("Accuracy:", acc)

cm = confusion_matrix(y_test, y_pred)
print("Confusion matrix:\n", cm)


plt.hist(model.predict_proba(X_test)[:,1], bins=20)
plt.xlabel("Risk ehtimoli")
plt.ylabel("Odamlar soni")
plt.title("Risk ehtimollari taqsimoti")
plt.show()



def predict_person(age, income, debt, job_years):
    data = np.array([[age, income, debt, job_years]])
    prob = model.predict_proba(data)[0][1]
    pred = model.predict(data)[0]
    return pred, prob

result, probability = predict_person(30, 400, 3500, 1)

print("Natija:", result)
print("Ehtimol:", probability)


joblib.dump(model, "risk_model.pkl")
print("Model saqlandi")