Skip to content

Instantly share code, notes, and snippets.

View mick001's full-sized avatar

Michy mick001

View GitHub Profile
@mick001
mick001 / example_pbx_bug.R
Last active September 11, 2018 15:50
Reproducible example for issue with GA package: position based crossover for permutation type problems not working as expected. Possible bug found.
# 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 August 16, 2018 11:53
Example of linear programming in 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 August 14, 2018 19:11
Support vector machine classifier example in R for Olivetti faces dataset.
#-------------------------------------------------------------------------------
# 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 August 13, 2018 15:33
Compute PCA in R with built-in prcomp function
#-------------------------------------------------------------------------------
# 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 August 13, 2018 14:58
Project the average face onto the eigenspace in 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 August 13, 2018 14:39
Find eigenfaces of the Olivetti faces dataset in 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 August 13, 2018 14:22
PCA on Olivetti faces dataset in 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 August 13, 2018 13:42
Calculate the average face on the Olivetti dataset in 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 August 12, 2018 22:54
Format Olivetti faces dataset image data in 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 August 12, 2018 22:49
Download Olivetti faces dataset in Python
# 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