O’ZBEKISTON RESPUBLIKASI RAQAMLI
TEXNOLOGIYALARVAZIRLIGI MUHAMMAD ALXORAZMIY NOMIDAGI TOSHKENT AXBOROT
TEXNOLOGIYALARI UNIVERSITETI
“SUN’IY INTELLEKT ASOSLARI” FANIDAN
AMALIY TOPSHIRIQ
TALABA: JUMAYEV JASURBEK
O’QITUVCHI: QOBILOV SIROJIDDIN SHERQULOVICH
TOSHKENT 2025
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, confusion_matrix,
ConfusionMatrixDisplay, roc_curve, auc, precision_recall_curve
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier, plot_tree
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
# Dataset yuklash
data = load_breast_cancer()
X = data.data
y = data.target
feature_names = data.feature_names
target_names = data.target_names
# Train/Test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# Scaling
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# --- Grafik uchun 2ta feature olish --X_train_2f = X_train_scaled[:, :2]
X_test_2f = X_test_scaled[:, :2]
# Logistic Regression (2 feature bilan)
logreg = LogisticRegression(max_iter=10000)
logreg.fit(X_train_2f, y_train)
# KNN (2 feature bilan)
k = 5
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train_2f, y_train)
y_pred_knn = knn.predict(X_test_2f)
accuracy_knn = accuracy_score(y_test, y_pred_knn)
# --- Logistic Regression chiziqli chiziq --plt.figure(figsize=(10, 6))
x_min, x_max = X_test_2f[:, 0].min() - 0.5, X_test_2f[:, 0].max() + 0.5
y_min, y_max = X_test_2f[:, 1].min() - 0.5, X_test_2f[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200), np.linspace(y_min,
y_max, 200))
Z = logreg.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.3, cmap='coolwarm')
plt.scatter(X_test_2f[:, 0], X_test_2f[:, 1], c=y_pred_knn,
cmap='coolwarm', edgecolors='k', s=100)
plt.title(f'K-NN (k={k}) + Logistic Regression Boundary\nAccuracy:
{accuracy_knn:.2f}')
plt.xlabel(f'{feature_names[0]} (scaled)')
plt.ylabel(f'{feature_names[1]} (scaled)')
plt.grid(True)
plt.show()
print(f'K-NN Accuracy (2 feature bilan): {accuracy_knn:.2f}')
# --- Full model (30 feature) bilan davom etamiz --# Decision Tree
dt_model = DecisionTreeClassifier(random_state=42)
dt_model.fit(X_train_scaled, y_train)
dt_pred = dt_model.predict(X_test_scaled)
dt_acc = accuracy_score(y_test, dt_pred)
plt.figure(figsize=(16, 10))
plot_tree(dt_model, filled=True, feature_names=feature_names,
class_names=target_names)
plt.title(f"Decision Tree (Accuracy: {dt_acc:.2f})")
plt.show()
# SVM
svm_model = SVC(kernel='linear', probability=True)
svm_model.fit(X_train_scaled, y_train)
svm_pred = svm_model.predict(X_test_scaled)
svm_acc = accuracy_score(y_test, svm_pred)
# Random Forest
rf_model = RandomForestClassifier(n_estimators=100, random_state=42)
rf_model.fit(X_train_scaled, y_train)
rf_pred = rf_model.predict(X_test_scaled)
rf_acc = accuracy_score(y_test, rf_pred)
# --- 1. Confusion Matrix (Random Forest uchun) --cm = confusion_matrix(y_test, rf_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm,
display_labels=target_names)
disp.plot(cmap='Blues')
plt.title(f"Random Forest Confusion Matrix (Accuracy: {rf_acc:.2f})")
plt.show()
# --- 2. ROC Curve (Random Forest) --rf_probs = rf_model.predict_proba(X_test_scaled)[:, 1]
fpr, tpr, thresholds = roc_curve(y_test, rf_probs)
roc_auc = auc(fpr, tpr)
plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, label=f'AUC = {roc_auc:.2f}')
plt.plot([0, 1], [0, 1], linestyle='--')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve - Random Forest')
plt.legend()
plt.grid(True)
plt.show()
# --- 3. Precision-Recall Curve --precision, recall, _ = precision_recall_curve(y_test, rf_probs)
plt.figure(figsize=(8, 6))
plt.plot(recall, precision, marker='.', label='Random Forest')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.title('Precision-Recall Curve - Random Forest')
plt.grid(True)
plt.legend()
plt.show()
# --- 4. Feature Importance (Random Forest) --importances = rf_model.feature_importances_
indices = np.argsort(importances)[::-1]
plt.figure(figsize=(12, 8))
plt.title('Feature Importances - Random Forest')
plt.bar(range(10), importances[indices][:10], align='center')
plt.xticks(range(10), feature_names[indices][:10], rotation=45,
ha='right')
plt.tight_layout()
plt.show()