Skip to content

Instantly share code, notes, and snippets.

View Laurae2's full-sized avatar

Laurae Laurae2

View GitHub Profile
@Laurae2
Laurae2 / Linear_regression_simple_GD.R
Created February 26, 2017 12:58
Linear Regression simple gradient descent (brute forced) in R
# Setting up random matrix
set.seed(11111)
x <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up the (perfect) linear relationship
y <- 2 + (x[, 1] * 2) + (x[, 2] * 3) + (x[, 3] * 4) + (x[, 3] ^ 2) + (x[, 1] * x[, 2])
# Setting up polynomial features
@Laurae2
Laurae2 / logistic_regression_EN.R
Created February 27, 2017 23:24
Logistic Regression + Elastic Net Regularizaion example in R
# Setting up random matrix
set.seed(11111)
x <- data.frame(a = rnorm(n = 15) * 5,
b = rnorm(n = 15) * 3 + 1,
c = rnorm(n = 15) * 2 + 2)
# Setting up an imperfect relationship (non-linear problem)
y <- as.integer((2 + (x[, 1] * 2) + (x[, 2] * 3) + (x[, 3] * 4) + (x[, 3] ^ 2) + (x[, 1] * x[, 2])) > 20)
# Setting up polynomial features
@Laurae2
Laurae2 / ssd_svm.R
Created March 1, 2017 11:33
Stochastic Subgradient Descent + Linear SVM example in R
# Setting up random matrix
set.seed(11111)
x <- data.frame(a = rnorm(n = 100) * 5,
b = rnorm(n = 100) * 3 + 1,
c = rnorm(n = 100) * 2 + 2)
# Setting up an imperfect relationship (non-linear problem) with +1 or -1 for classes
y <- 2 * as.integer((2 + (x[, 1] * 2) + (x[, 2] * 3) + (x[, 3] * 4) + (x[, 3] ^ 2) + (x[, 1] * x[, 2])) > 20) - 1
# Setting up polynomial features
@Laurae2
Laurae2 / plotting.R
Created April 26, 2017 09:33
Code used for Medium blog post "Destroying the Myth of number of threads = number of physical cores": https://medium.com/data-design/destroying-the-myth-of-number-of-threads-number-of-physical-cores-762ad3919880
library(plotly)
library(ggplot2)
plot1 <- function(data, title) {
ggplot(data = data, aes(x = Threading, y = Score, fill = Score)) + geom_bar(stat = "identity", color = "black") + geom_label(aes(label = Score, y = mean(Score) * 0.05 + Score, fill = Score), position = "identity", color = "white", fontface = "bold") + scale_fill_gradientn(colours = c("#ff9999", "#33cc33")) + labs(title = title, x = "Threading Mode", y = "Cinebench R15 Score", fill = "Score") + theme_bw()
}
plot2 <- function(data, title) {
ggplot(data = data, aes(x = Threading, y = ScoreScale, fill = ScoreScale)) + geom_bar(stat = "identity", color = "black") + geom_label(aes(label = round(ScoreScale, digits = 2), y = mean(ScoreScale) * 0.05 + ScoreScale, fill = ScoreScale), position = "identity", color = "white", fontface = "bold") + scale_fill_gradientn(colours = c("#ff9999", "#33cc33")) + labs(title = title, x = "Threading Mode", y = "Scaled Cinebench R15 Score (GHz)", fill = "Scale") + theme_bw()
}
@Laurae2
Laurae2 / much_packages.R
Last active July 1, 2017 15:48
Much R packages
packages <- c("abind", "acepack", "actuar", "ActuDistns", "ada",
"adabag", "addinplots", "ade4",
"ade4TkGUI", "adegraphics", "adehabitatLT", "adehabitatMA", "ADGofTest",
"AER", "AGD", "agricolae", "AICcmodavg", "akima", "alabama",
"AlgDesign", "alphahull", "alr3", "alr4", "amap", "Amelia", "anchors",
"animation", "aod", "aods3", "ape", "aplpack",
"argparse", "arm", "arules", "arulesViz", "ascii",
"assertthat", "AUC", "BaBooN", "backports", "barcode", "bartMachine",
"bartMachineJARs", "base64", "base64enc", "BatchJobs",
"BayesFactor", "bayesplot", "BayesX", "BayesXsrc", "BB", "BBmisc",
@Laurae2
Laurae2 / enable-all-advanced-power-settings.ps1
Created June 11, 2018 20:25 — forked from raspi/enable-all-advanced-power-settings.ps1
Enable all advanced power settings in Windows.
# List all possible power config GUIDs in Windows
# Run: this-script.ps1 | Out-File powercfg.ps1
# Then edit and run powercfg.ps1
# (c) Pekka "raspi" Järvinen 2017
$powerSettingTable = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerSetting
$powerSettingInSubgroubTable = Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerSettingInSubgroup
Get-WmiObject -Namespace root\cimv2\power -Class Win32_PowerSettingCapabilities | ForEach-Object {
$tmp = $_.ManagedElement
@Laurae2
Laurae2 / data.table_benchmark_2bn.R
Last active October 25, 2018 18:24
data.table benchmark 2 billions
# library(data.table)
# N=2e9; K=100
# set.seed(1)
# DT <- data.table(
# id1 = sample(sprintf("id%03d",1:K), N, TRUE), # large groups (char)
# id2 = sample(sprintf("id%03d",1:K), N, TRUE), # large groups (char)
# id3 = sample(sprintf("id%010d",1:(N/K)), N, TRUE), # small groups (char)
# id4 = sample(K, N, TRUE), # large groups (int)
# id5 = sample(K, N, TRUE), # large groups (int)
# id6 = sample(N/K, N, TRUE), # small groups (int)
@Laurae2
Laurae2 / stream.R
Created October 14, 2018 21:48
Stream in R (brute force & slow version)
library(Rcpp)
Sys.setenv(PKG_CXXFLAGS = "-fopenmp -std=c++11")
cppFunction(code = '#include <Rcpp.h>
#include <omp.h>
Rcpp::NumericVector streamer(Rcpp::NumericVector threads, int triad_size = 2147483647, int repeats = 10, int repeats_alloc = 3, bool simd = false) {
int i;
@Laurae2
Laurae2 / cplex.md
Created November 25, 2018 23:13
Windows + CPLEX + R + cplexAPI

Step 1: download cplexAPI : Step 2: edit src/Makevars.win like the following, make sure cplex1280 is equal to your version you must change to the corresponding number you can find in C:/Program Files/IBM/ILOG/CPLEX_Studio128/cplex/bin/x64_win64:

PKG_CPPFLAGS=-g -D_R_=1 -DUSE_R=1 -I"C:/Program Files/IBM/ILOG/CPLEX_Studio128/cplex/include"
PKG_LIBS=-L"C:/Program Files/IBM/ILOG/CPLEX_Studio128/cplex/bin/x64_win64" -lm -lcplex1280

Step 3: in a R console, on the right folder:

@Laurae2
Laurae2 / xgboost_cpu_vs_gpu.R
Last active December 24, 2018 14:38
xgboost CPU vs GPU
library(xgboost)
set.seed(1)
N <- 10000000
p <- 100
pp <- 25
X <- matrix(runif(N * p), ncol = p)
betas <- 2 * runif(pp) - 1
sel <- sort(sample(p, pp))
m <- X[, sel] %*% betas - 1 + rnorm(N)