Spaces:
Sleeping
Sleeping
Download app.py from valinor61/digits-classification: direct link, hf CLI and curl.
- Browser
- Download file 4.75 kB
-
https://huggingface.co/spaces/valinor61/digits-classification/resolve/main/app.py
- Command line
-
hf download hf://spaces/valinor61/digits-classification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/valinor61/digits-classification/resolve/main/app.py
4.75 kB
| import streamlit as st | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.datasets import fetch_openml | |
| from sklearn.linear_model import SGDClassifier | |
| from sklearn.metrics import accuracy_score, confusion_matrix | |
| import pandas as pd | |
| import seaborn as sns | |
| from PIL import Image, ImageDraw | |
| import io | |
| # Sayfa yapılandırması | |
| st.set_page_config(page_title="MNIST Rakam Sınıflandırma", layout="wide") | |
| # Başlık ve açıklama | |
| st.title("MNIST Rakam Sınıflandırma") | |
| st.write("Bu uygulama, MNIST veri setini kullanarak el yazısı rakamları tanımak için eğitilmiş bir model sunar.") | |
| # Navigasyon | |
| st.sidebar.title("MNIST Rakam Sınıflandırma") | |
| page = st.sidebar.radio("Sayfa Seçin:", ["Veri Analizi", "Model Eğitimi"]) | |
| # Veri yükleme fonksiyonu | |
| def load_data(): | |
| try: | |
| with st.spinner('MNIST veri seti yükleniyor...'): | |
| data = fetch_openml("mnist_784", version=1) | |
| x, y = data["data"], data["target"] | |
| return x, y | |
| except Exception as e: | |
| st.error(f"Veri yüklenirken hata oluştu: {e}") | |
| return None, None | |
| # Model eğitim fonksiyonu | |
| def train_model(xtrain, ytrain): | |
| with st.spinner('Model eğitiliyor...'): | |
| model = SGDClassifier(random_state=42) | |
| model.fit(xtrain, ytrain) | |
| return model | |
| # Veriyi yükle | |
| x, y = load_data() | |
| if x is not None and y is not None: | |
| if page == "Veri Analizi": | |
| st.header("Veri Seti Analizi") | |
| # Veri seti hakkında bilgi | |
| st.subheader("Veri Seti Hakkında") | |
| st.write(f"Toplam örnek sayısı: {x.shape[0]}") | |
| st.write(f"Özellik sayısı: {x.shape[1]}") | |
| st.write("Her bir görüntü 28x28 piksel boyutundadır ve gri tonlamalıdır.") | |
| # Sınıf dağılımı | |
| st.subheader("Sınıf Dağılımı") | |
| class_dist = pd.Series(y).value_counts().sort_index() | |
| st.bar_chart(class_dist) | |
| # Örnek görüntüler | |
| st.subheader("Örnek Görüntüler") | |
| cols = st.columns(5) | |
| for i, col in enumerate(cols): | |
| with col: | |
| idx = np.random.randint(0, len(x)) | |
| image = np.array(x.iloc[idx]).reshape(28, 28) | |
| fig, ax = plt.subplots() | |
| ax.imshow(image, cmap='gray') | |
| ax.set_title(f"Rakam: {y.iloc[idx]}") | |
| ax.axis('off') | |
| st.pyplot(fig) | |
| elif page == "Model Eğitimi": | |
| st.header("Model Eğitimi ve Değerlendirme") | |
| # Eğitim ve test verilerini ayırma | |
| test_size = st.slider("Test veri seti oranı:", 0.1, 0.5, 0.2, 0.05) | |
| if st.button("Modeli Eğit"): | |
| with st.spinner('Veri hazırlanıyor...'): | |
| xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=test_size, random_state=42) | |
| st.success(f"Veri başarıyla ayrıldı: {xtrain.shape[0]} eğitim örneği, {xtest.shape[0]} test örneği") | |
| # Modeli eğit | |
| model = train_model(xtrain, ytrain) | |
| # Model performansını değerlendir | |
| with st.spinner('Model değerlendiriliyor...'): | |
| y_pred = model.predict(xtest) | |
| accuracy = accuracy_score(ytest, y_pred) | |
| st.success(f"Model doğruluğu: {accuracy:.4f}") | |
| # Karışıklık matrisi | |
| st.subheader("Karışıklık Matrisi") | |
| cm = confusion_matrix(ytest, y_pred) | |
| fig, ax = plt.subplots(figsize=(10, 8)) | |
| sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', ax=ax) | |
| ax.set_xlabel('Tahmin Edilen Sınıf') | |
| ax.set_ylabel('Gerçek Sınıf') | |
| st.pyplot(fig) | |
| # Örnek tahminler | |
| st.subheader("Örnek Tahminler") | |
| cols = st.columns(5) | |
| for i, col in enumerate(cols): | |
| with col: | |
| idx = np.random.randint(0, len(xtest)) | |
| image = np.array(xtest.iloc[idx]).reshape(28, 28) | |
| prediction = model.predict([xtest.iloc[idx]])[0] | |
| actual = ytest.iloc[idx] | |
| fig, ax = plt.subplots() | |
| ax.imshow(image, cmap='gray') | |
| ax.set_title(f"Tahmin: {prediction}\nGerçek: {actual}") | |
| ax.axis('off') | |
| st.pyplot(fig) | |
| else: | |
| st.error("Veri yüklenemedi. Lütfen internet bağlantınızı kontrol edin veya daha sonra tekrar deneyin.") | |