Skip to content

Instantly share code, notes, and snippets.

View wlinInspire's full-sized avatar
🏠
Working from home

Wei Lin wlinInspire

🏠
Working from home
View GitHub Profile
import os
import pickle
import caffe
import cv2
import numpy as np
# ======================================================================================================================
# Off load params for each net in a pickle file
# ======================================================================================================================
@wlinInspire
wlinInspire / db_query.R
Last active August 25, 2019 20:29
Tricks in R to Boost Your Productivity (Part 2)
# Connect DB function
connect_db <- function() {
pgsql <- RJDBC::JDBC("com.amazon.redshift.jdbc42.Driver",
"./jdbc_driver/RedshiftJDBC42-1.2.16.1027.jar",
"`")
host <- "redshift-cluster-1.abc1234.us-east-2.redshift.amazonaws.com"
port <- '5439'
user <- 'abc1234'
@wlinInspire
wlinInspire / .Rprofile
Last active August 25, 2019 18:52
Tricks in R to Boost Your Productivity (Part 2)
.First <- function(){
# Set Java Heap Space
options(java.parameters = "-Xmx25g")
# Load common packages
library(ggplot2)
library(tidyverse)
library(data.table)
@wlinInspire
wlinInspire / churn_logistic_regression_w_special_cycle.R
Created August 19, 2019 04:31
Calculating Life Time Value for Subscription Products
library(survival)
library(data.table)
library(ggplot2)
data = read.csv('https://raw.githubusercontent.com/IBM/invoke-wml-using-cognos-custom-control/master/data/Telco-Customer-Churn.csv')
setDT(data)
churn_data <- data[, churn_flag := ifelse(Churn == 'Yes', 1, 0)]
km_curve <- survfit(Surv(tenure, churn_flag) ~ 1, data=churn_data)
@wlinInspire
wlinInspire / churn_logistic_regression.R
Last active August 19, 2019 04:21
Calculating Life Time Value for Subscription Products
library(survival)
library(data.table)
library(ggplot2)
data = read.csv('https://raw.githubusercontent.com/IBM/invoke-wml-using-cognos-custom-control/master/data/Telco-Customer-Churn.csv')
setDT(data)
churn_data <- data[, churn_flag := ifelse(Churn == 'Yes', 1, 0)]
km_curve <- survfit(Surv(tenure, churn_flag) ~ 1, data=churn_data)
@wlinInspire
wlinInspire / survival_rurve_fit.R
Last active August 19, 2019 03:32
Calculating Life Time Value for Subscription Products
library(survival)
library(data.table)
library(ggplot2)
data = fread('https://raw.githubusercontent.com/IBM/invoke-wml-using-cognos-custom-control/master/data/Telco-Customer-Churn.csv')
churn_data <- data[, .(churn_flag = ifelse(Churn == 'Yes', 1, 0), tenure)]
km_curve <- survfit(Surv(tenure, churn_flag) ~ 1, data=churn_data)
# Calculate Survival and Churn Rate
@wlinInspire
wlinInspire / km_curve.R
Last active August 19, 2019 03:28
Calculating Life Time Value for Subscription Products
library(survival)
library(data.table)
library(ggplot2)
data <- fread('https://raw.githubusercontent.com/IBM/invoke-wml-using-cognos-custom-control/master/data/Telco-Customer-Churn.csv')
churn_data <- data[, .(churn_flag = ifelse(Churn == 'Yes', 1, 0), tenure)]
km_curve <- survfit(Surv(tenure, churn_flag) ~ 1, data=churn_data)
# Calculate Survival and Churn Rate
@wlinInspire
wlinInspire / h2o.R
Created August 11, 2019 03:58
10 R Tricks to Boost Your Productivity
h2o_start <- function() {
h2o::h2o.init(nthreads = -1, enable_assertions = FALSE, min_mem_size = '8g',
strict_version_check = FALSE, port = 54321)
}
h2o_stop <- function() {
h2o::h2o.shutdown(prompt = FALSE)
}
@wlinInspire
wlinInspire / cc.R
Last active August 11, 2019 03:28
10 R Tricks to Boost Your Productivity
cc <- function(df) {
print(df %>% is.na() %>% colSums())
}
cd <- function(df) {
print(df %>% is.na() %>% colMeans())
}
@wlinInspire
wlinInspire / quick_date.R
Created August 11, 2019 03:08
10 R Tricks to Boost Your Productivity
quick_date <- function(x, …) {
if (anyDuplicated(x)) {
ux <- unique(x)
idx <- match(x, ux)
y <- as.Date.character(ux, …)
return(y[idx])
}
as.Date.character(x, …)
}