Based on the great blog by Joel Grus, I implemented LogisticRegression, PCA, and LDA. I'd appreciate feedback as I'm not sure that the logistic classifier is good enough (as it supposed to achieve higher accuracy on training set).

    # This code was writen with inspiration from https://github.com/joelgrus/shirts/blob/master/visuals.py and with the help of my friend Jiaher.

from PIL import Image
from glob import glob
from random import shuffle, seed
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

STANDARD_SIZE = (138, 138)

def img_to_array(filename):
    takes a filename and turns it into a numpy array of RGB pixels
    img = Image.open(filename)
    img_i = img.resize(STANDARD_SIZE)
    img_i = list(img_i.getdata())
    img_i = map(list, img_i)
    img_i = np.array(img_i)
    s = img_i.shape[0] * img_i.shape[1]
    img_wide = img_i.reshape(1, s)
    return img_wide[0]

male_files = glob('./images/boys/*')
female_files = glob('./images/girls/*')

process_file = img_to_array

raw_data = []
for filename in male_files:
    print (filename)
    raw_data.append((process_file(filename), 'male', filename))
for filename in female_files:
    print (filename)
    raw_data.append((process_file(filename), 'female', filename))

# randomly order the data

# pull out the features and the labels
data = np.array([cd for (cd, _y, f) in raw_data])
labels = np.array([_y for (cd, _y, f) in raw_data])
labels = [1 if label == 'male' else 0 for label in labels]

X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.2)

        # Simple Linear Model:
    clf = LogisticRegression(penalty='l2')
    clf.fit(X_train, y_train)

    print("linear model test score", clf.score(X_test, y_test))

# find the principal components
pca = PCA(n_components=N_COMPONENTS, random_state=0, svd_solver='randomized')
X_train_pca = pca.fit_transform(X_train)
X_test_pca = pca.transform(X_test)

pcaclf = clf.fit(X_train_pca, y_train)
print("pca test score", pcaclf.score(X_test_pca, y_test))
print("pca train score", pcaclf.score(X_train_pca, y_train))

lda = LinearDiscriminantAnalysis(n_components=2)

X_train_lda = lda.fit_transform(X_train, y_train)
X_test_lda = lda.transform(X_test)

ldaclf = clf.fit(X_train_lda, y_train)
print("lda train score", ldaclf.score(X_train_lda, y_train))
print("lda test score", ldaclf.score(X_test_lda, y_test))

The data:

I'm using the data from ImageNet, nothing fancy.

  • \$\begingroup\$ got a link to the blog? \$\endgroup\$ – Simon Forsberg Dec 3 '17 at 21:15
  • \$\begingroup\$ @SimonForsberg Probably this one. He wrote some crazy stuff in Python. \$\endgroup\$ – Mast Dec 3 '17 at 21:19
  • \$\begingroup\$ Can you post an example of your labels/data? \$\endgroup\$ – mochi Dec 6 '17 at 4:04
  • \$\begingroup\$ @mochi I'm using the data from ImageNet (image-net.org) and labels are 0 for men and 1 for women. \$\endgroup\$ – oba2311 Dec 6 '17 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.