Skip to content

Instantly share code, notes, and snippets.

Avatar

Michy mick001

View GitHub Profile
@mick001
mick001 / example_pbx_bug.R
Last active Sep 11, 2018
Reproducible example for issue with GA package: position based crossover for permutation type problems not working as expected. Possible bug found.
View example_pbx_bug.R
# Testing parents
test_parents <- matrix(c(1:8, c(2, 4, 6, 8, 7, 5, 3, 1)), nrow=2, byrow=T)
# Test 1
cxPoints_test1 <- c(1, 3, 5, 8)
# Test 2
cxPoints_test2 <- c(2, 3, 6)
##########
# gaperm_pbxCrossover_R has been *slightly* modified merely for pbx crossover testing purposes
##########
@mick001
mick001 / linear-programming-example.R
Created Aug 16, 2018
Example of linear programming in R
View linear-programming-example.R
# Load lpSolve
require(lpSolve)
## Set the coefficients of the decision variables -> C
C <- c(30, 40, 80)
# Create constraint martix B
A <- matrix(c(1, 1, -10,
4, 3, -20,
1, 0, -2,
@mick001
mick001 / SVC_model.R
Created Aug 14, 2018
Support vector machine classifier example in R for Olivetti faces dataset.
View SVC_model.R
#-------------------------------------------------------------------------------
# Setup
require(dplyr)
require(e1071)
load(file="images_formatted.Rdata")
D <- out; rm(out)
# Scale the data. Note that scale returns a matrix
@mick001
mick001 / compute_pca.R
Created Aug 13, 2018
Compute PCA in R with built-in prcomp function
View compute_pca.R
#-------------------------------------------------------------------------------
# With built-in prcomp function:
# Compute PCA
pc <- prcomp(D, center = T, scale. = T)
# Eigenvalues
(pc$sdev^2)[1:20]
# Eigenvectors (loadings)
@mick001
mick001 / project_avg_face_eigenspace.R
Created Aug 13, 2018
Project the average face onto the eigenspace in R.
View project_avg_face_eigenspace.R
#-------------------------------------------------------------------------------
# Projection of the average face onto the eigenvenctor space
# From a feature vector to a feature vector in the reduced feature space
proj_face_0 <- as.matrix(AverageFace[1, 2:4097]) %*% eigenvectors # for label 0
proj_face_1 <- as.matrix(AverageFace[2, 2:4097]) %*% eigenvectors # for label 1
proj_face_2 <- as.matrix(AverageFace[3, 2:4097]) %*% eigenvectors # for label 2
# Every face has different projection on eigenvector space.
# We can use these new fewer values for a classification task.
@mick001
mick001 / eigenfaces_olivetti.R
Created Aug 13, 2018
Find eigenfaces of the Olivetti faces dataset in R.
View eigenfaces_olivetti.R
#-------------------------------------------------------------------------------
# Plot of eigenfaces
# First 4 eigenfaces
plt_img(eigenvectors[, 1]) # Eigenface 1
plt_img(eigenvectors[, 2]) # Eigenface 2
plt_img(eigenvectors[, 3]) # Eigenface 3
plt_img(eigenvectors[, 4]) # Eigenface 4
plt_img(eigenvectors[, 20]) # Eigenface 20
# ...
@mick001
mick001 / PCA_on_olivetti.R
Last active Aug 13, 2018
PCA on Olivetti faces dataset in R
View PCA_on_olivetti.R
#-------------------------------------------------------------------------------
# Perform PCA on the data
# Step 1: scale data
# Scale as follows: mean equal to 0, stdv equal to 1
D <- scale(D)
# Step 2: calculate covariance matrix
A <- cov(D)
A_ <- t(D) %*% D / (nrow(D)-1)
@mick001
mick001 / avg_faces_olivetti_1.R
Created Aug 13, 2018
Calculate the average face on the Olivetti dataset in R
View avg_faces_olivetti_1.R
rm(list=ls())
require(dplyr)
load(file="images_formatted.Rdata")
D <- out; rm(out)
#-------------------------------------------------------------------------------
# Brief description of the data
# D is a 399x4096 matrix. It contains 399 images (rows) of 64x64 grayscale pixels (4096 columns)
@mick001
mick001 / format_image_data.R
Created Aug 12, 2018
Format Olivetti faces dataset image data in R
View format_image_data.R
################################################################################
# This script converts the images in the following format:
#
# X -> converted to a 399x4096 matrix. Each row represents an image of 64x64
# greyscale pixels
#
# y -> labels of the images
#
################################################################################
@mick001
mick001 / download_olivetty_faces.py
Created Aug 12, 2018
Download Olivetti faces dataset in Python
View download_olivetty_faces.py
# Imports
from sklearn.datasets import fetch_olivetti_faces
import numpy as np
# Download Olivetti faces dataset
olivetti = fetch_olivetti_faces()
x = olivetti.images
y = olivetti.target
# Print info on shapes and reshape where necessary