Skip to content

Instantly share code, notes, and snippets.

@sizhky
sizhky / basic_charts.py
Last active July 26, 2017 11:23
Matplotlib simple charts
########################################
'''Simple Bar Chart'''
x_axis=['a','b','c']
y_axis=[10,12,11]
fig, ax = plt.subplots(figsize=(20, 8))
ax.set_xticks(range(len(x_axis)))
ax.bar(range(len(x_axis)), y_axis, align='center')
ax.set_xticklabels(x_axis, rotation='vertical')
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = 5, 10
%matplotlib inline
@sizhky
sizhky / .inputrc
Last active May 22, 2017 11:51
Conditional history
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
test_train_split <- function(data, train_frac = 0.66){
library(caret)
inTrain <- createDataPartition(1:nrow(data), p = train_frac, list = FALSE)
training <- data[inTrain,]
testing <- data[-inTrain,]
return(list(train = training, test = testing))
}
get_metrics <- function(model, test_data, truth, table = FALSE){
preds <- predict(model, test_data);
table_ <- table(truth, preds);
acc_ <- sum(diag(table_))/sum(table_);
rec_ <- table_[2,2]/sum(table_[,2]);
prec_ <- table_[2,2]/sum(table_[2,]);
f_ <- 2*rec_*prec_/(rec_+prec_)
if (table == FALSE){
cat('Accuracy:', acc_, '\nRecall:', rec_, '\nPrecision:', prec_, '\nF-Score:', f_);
} else {
datatypes <- function(dataframe){
types <- split(names(dataframe),sapply(dataframe, function(x) paste(class(x), collapse=" ")))
return(types)
}
factor_cols <- c()
raw_data[,factor_cols] <- lapply(raw_data[,factor_cols],
function(x) factor(x))