Last active
October 5, 2025 16:36
-
-
Save aydinnyunus/7441460d940cada36c9aed6ac6158939 to your computer and use it in GitHub Desktop.
Logistic Regression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Kullanım: | |
| # python main.py dosya.png | |
| # | |
| # NOT: | |
| # - İlk çalıştırmada MNIST veri seti indirilecek ve model eğitilecek. | |
| # - Bu yüzden internet bağlantısı gereklidir. | |
| # - Gerekli kütüphaneler: | |
| # python -m venv venv | |
| # source venv/bin/activate # Linux/macOS | |
| # venv\Scripts\activate # Windows | |
| # pip install numpy pillow scikit-learn matplotlib pandas | |
| # Dataset: https://www.tensorflow.org/datasets/catalog/mnist | |
| import sys | |
| from sklearn.datasets import fetch_openml | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import accuracy_score | |
| import numpy as np | |
| import pandas as pd | |
| from PIL import Image, ImageEnhance, ImageOps | |
| import matplotlib.pyplot as plt | |
| def predict_handwritten_image(image_path, clf, show=False): | |
| img = Image.open(image_path).convert('L') | |
| img = img.resize((28, 28)) | |
| img = ImageOps.autocontrast(img) | |
| enhancer = ImageEnhance.Contrast(img) | |
| img = enhancer.enhance(2.0) | |
| img_array = np.array(img) / 255.0 | |
| if show: | |
| plt.imshow(img_array, cmap='gray') | |
| plt.title(image_path) | |
| plt.axis('off') | |
| plt.show() | |
| img_flatten = img_array.flatten().reshape(1, -1) | |
| # Convert to DataFrame to ensure feature names match training data | |
| feature_names = [f'pixel{i+1}' for i in range(784)] | |
| img_df = pd.DataFrame(img_flatten, columns=feature_names) | |
| prediction = clf.predict(img_df) | |
| return prediction[0] | |
| def main(): | |
| if len(sys.argv) != 2: | |
| print("Kullanım: python main.py dosya.png") | |
| sys.exit(1) | |
| image_path = sys.argv[1] | |
| print("MNIST verisi indiriliyor (ilk seferde internet gerekiyor)...") | |
| mnist = fetch_openml('mnist_784', version=1) | |
| X = mnist.data / 255.0 | |
| y = mnist.target.astype(int) | |
| # Veriyi eğitim ve test olarak ayır | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| print("Model eğitiliyor...") | |
| clf = LogisticRegression(max_iter=1000) | |
| clf.fit(X_train, y_train) | |
| # Test setindeki doğruluk oranı | |
| y_pred = clf.predict(X_test) | |
| accuracy = accuracy_score(y_test, y_pred) * 100 | |
| print(f"Test setindeki doğruluk oranı: %{accuracy:.2f}") | |
| # Kullanıcının verdiği dosyayı tahmin et | |
| pred = predict_handwritten_image(image_path, clf, show=True) | |
| print(f"{image_path} için tahmin edilen rakam: {pred}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment