Scikit-learn yordamida tasniflash modelini o‘qitish




Download 5,69 Mb.
bet30/182
Sana19.05.2024
Hajmi5,69 Mb.
#244351
1   ...   26   27   28   29   30   31   32   33   ...   182
Bog'liq
Python sun\'iy intellekt texnologiyasi Dasrlik 2024

Scikit-learn yordamida tasniflash modelini o‘qitish:
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
# Ma’lumotlarni yuklash(Iris ma’lumotlar bazasi bilan misol)
from sklearn.datasets import load_iris
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)
# Logistik regressiya modelini ishga tushirish
model = LogisticRegression()
# Modelni o‘qitish
model.fit(X_train, y_train)
# Sinov ma’lumotlar to‘plamida bashorat qilish
predictions = model.predict(X_test)
# Aniqlikni baholash
accuracy = accuracy_score(y_test, predictions)
print(f'Accuracy: {accuracy}')
# Tasniflash hisobotining xulosasi
print(classification_report(y_test, predictions))
SpaCy kutubxonasi yordamida tabiiy tilni qayta ishlash:
import spacy
# Matnni qayta ishlash uchun oldindan tayyorlangan modelni yuklash
nlp = spacy.load("en_core_web_sm")
# Matnni qayta ishlash
text = "Natural Language Processing with spaCy is awesome!"
doc = nlp(text)
# Matndan ma’lumot olish
entities = [(ent.text, ent.label_) for ent in doc.ents]
# Natijalarni chiqarish
print("Entities:", entities)
Klasterlash uchun scikit-learn kutubxonasidan foydalanish:
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
# Klasterlash uchun ma’lumotlar yaratish (ikki klasterli misol)
X, _ = make_blobs(n_samples=300, centers=2, random_state=42)
# K Means modelini ishga tushirish va o‘qitish
kmeans = KMeans(n_clusters=2, random_state=42)
kmeans.fit(X)
# Klasterlarni bashorat qilish
labels = kmeans.predict(X)
# Natijalarni vizualizatsiya qilish
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='viridis')
plt.title('KMeans Clustering’)
plt.show()
Ushbu misollar Python-da scikit-learn va spaCy kabi mashinalarni o‘qitish kutubxonalarining asosiy funktsiyalarini namoyish etadi. Biroq, haqiqiy ma’lumotlar bilan ishlashda, ehtimol siz ma’lumotlarni oldindan qayta ishlash, giperparametrlarni tanlash, modelni baholash va boshqalar kabi kengroq qadamlarni bajarishingiz kerak bo‘ladi.

Download 5,69 Mb.
1   ...   26   27   28   29   30   31   32   33   ...   182




Download 5,69 Mb.

Bosh sahifa
Aloqalar

    Bosh sahifa



Scikit-learn yordamida tasniflash modelini o‘qitish

Download 5,69 Mb.