Modelni yaratamiz va o‘rgatamiz




Download 5,69 Mb.
bet167/182
Sana19.05.2024
Hajmi5,69 Mb.
#244351
1   ...   163   164   165   166   167   168   169   170   ...   182
Bog'liq
Python sun\'iy intellekt texnologiyasi Dasrlik 2024

Modelni yaratamiz va o‘rgatamiz:
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import matplotlib.pyplot as plt # Ko‘krak bezi saratoni ma’lumotlarini yuklash
data = load_breast_cancer()
X = data.data
y = data.target # Ma’lumotlarni o‘quv va test to‘plamlariga bo‘lish
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Tasodifiy o‘rmon modelini yaratish va o‘qitish
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train) # Sinov to‘plamida bashorat qilish
y_pred = model.predict(X_test) # Modelning aniqligini baholash
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy:.2f}") # Xato matritsasini vizualizatsiya qilish
conf_matrix = confusion_matrix(y_test, y_pred)
plt.imshow(conf_matrix, interpolation='nearest’, cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.colorbar()
plt.xlabel('Predicted')
plt.ylabel('True’)
plt.show() # Tasniflash hisobotining xulosasi
class_report = classification_report(y_test, y_pred)
print("Classification Report:")
print(class_report)
Ushbu kod scikit-learn kutubxonasidagi ko‘krak bezi saratoni ma’lumotlar to‘plamidan foydalanadi, ma’lumotlarni o‘quv va sinov to‘plamlariga ajratadi, tasodifiy o‘rmon modelini yaratadi va o‘rgatadi, so‘ngra aniqlik, xato matritsasi va tasniflash hisoboti orqali uning ishlashini baholaydi.
Tensorflow va Keras kutubxonalaridan foydalangan holda tibbiy tasvirga asoslangan tasniflash modelini o‘rgatish uchun Python va sun’iy intellektdan foydalanish misolini ko‘rib chiqaylik. Bunday holda, biz sklearn kutubxonasidagi Mammography ma’lumotlar to‘plamidan foydalanamiz.
pip install tensorflow scikit-learn matplotlib
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
import matplotlib.pyplot as plt # Ma’lumotlarni yuklash
data = load_breast_cancer()
X = data.data
y = data.target # Ma’lumotlarni o‘quv va test to‘plamlariga bo‘lish
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Ma’lumotlarni normallashtirish
X_train = tf.keras.utils.normalize(X_train, axis=1)
X_test = tf.keras.utils.normalize(X_test, axis=1)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1, 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1, 1)
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(X_train.shape[1], 1, 1)),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(1, activation='sigmoid')])# Modelni kompilyatsiya qilish
model.compile(optimizer='adam',loss='binary_crossentropy', metrics=['accuracy']) # Modelni o‘qitish
history = model.fit(X_train, y_train, epochs=10, validation_data=(X_test, y_test)) # O‘quv natijalarini vizualizatsiya qilish
plt.plot(history.history['accuracy'], label="O‘quv to‘plamidagi aniqlik")
plt.plot(history.history['val_accuracy'], label='Sinov to‘plamidagi aniqlik ')
plt.xlabel(' Davrlar ')
plt.ylabel(' Aniqlik ')
plt.legend()
plt.show()
Ushbu misol tibbiy tasvirlarni ikki sinfga (yaxshi va xavfli o‘smalar) tasniflash uchun Mammography ma’lumotlar to‘plamidan foydalanadi. Biz oddiy konvolyutsion neyron tarmoq modelidan (CNN) foydalanamiz, uni o‘quv to‘plamida o‘rgatamiz va uning ishlashini test to‘plamida baholaymiz.

Download 5,69 Mb.
1   ...   163   164   165   166   167   168   169   170   ...   182




Download 5,69 Mb.