application_vnd.jupyter.stderr_output_1.png
from PIL import Image

from sklearn.linear_model import LogisticRegression

import numpy as np

import os

import pandas as pd

import random

def get_pixel_data(filepath):
    """
    Get the pixel data from an image as a pandas DataFrame.
    """
    image = Image.open(filepath)
    pixel_data = np.array(image.getdata())
    pixel_data = pixel_data.mean(axis=1)
    pixel_data = pixel_data.reshape(1, 32 * 32)
    pixel_data = pd.DataFrame(pixel_data, columns=np.arange(32 * 32))
    image.close()
    return pixel_data

path = '../input/train/train/'
train = pd.DataFrame()
for file in sorted(os.listdir(path)):
    image = get_pixel_data(path + file)
    train = train.append(image, ignore_index=True)
labels_train = pd.read_csv('../input/train.csv').sort_values('id')
path = '../input/test/test/'
test = pd.DataFrame()
test_id = []
for file in sorted(os.listdir(path)):
    image = get_pixel_data(path + file)
    test = test.append(image, ignore_index=True)
    test_id.append(file)

random.seed(0)
idx = random.choices(range(17500), k=10000)
X_train = train.iloc[idx]
X_test = train.drop(idx, axis=0)
y_train = labels_train.iloc[idx, 1]
y_test = labels_train.drop(idx, axis=0).iloc[:, 1]

model = LogisticRegression(solver='lbfgs', random_state=0)
model.fit(X_train, y_train)
model.score(X_test, y_test)