Skip to content

Instantly share code, notes, and snippets.

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

Gray grayskripko

🏠
Working from home
  • Buenos Aires, Argentina
View GitHub Profile
@grayskripko
grayskripko / nn_memory.txt
Created February 16, 2018 16:58
Calculate memory for NN
https://github.com/pytorch/pytorch/issues/958#issuecomment-309752126
@grayskripko
grayskripko / flask_example.py
Created March 12, 2018 16:01
flask example
# -*- coding:utf8 -*-
# !/usr/bin/env python
from flask import Flask, request, session, make_response
from flask_pymongo import PyMongo
from utils.server import *
from utils.data import *
import pandas as pd
import numpy as np
@grayskripko
grayskripko / rowwise_dplyr.R
Last active July 19, 2019 18:58
Rowwise dplyr
# when you need to use tidyselect in a mutate function
# https://stackoverflow.com/questions/48353331/row-wise-operations-select-helpers-and-the-mutate-function-in-dplyr
tbl %>% mutate(a = select(., contains("abc")))
# based on https://rpubs.com/wch/200398
mtcars %>% purrrlyr::by_row(~any(is.na(.)), .collate = "rows", .to = "has_missing")
tibble(a=c(1, NA, 3), b=c(NA, 2, NA), c=c(NA, 5, 6)) %>%
mutate(pmap_int(., function(...) sum(lift_ld(is.na)(...))))
@grayskripko
grayskripko / char_dplyr_filter.R
Created July 18, 2018 10:52
Non-interactive dplyr filter
xx <- "b"
data.frame(a=1:3, b=c(2, NA, 5)) %>% filter(!is.na(get(xx)))
data.frame(a=1:3, b=c(2, NA, 5)) %>% filter(!is.na(!!as.name(xx)))
@grayskripko
grayskripko / dplyr_rename.R
Last active March 8, 2022 10:46
Dplyr rename all columns with character vector
x <- data.frame(a=1, b=2)
x %>% rename_at(vars(names(.)), ~letters[3:4])
x %>% rename_all(~letters[3:4])
x %>% rename_all(funs(str_c('prefix_', .)))
x %>% rename_at(vars(-starts_with("a")), ~str_c(., "_b")) # approx.
@grayskripko
grayskripko / nse_functions.R
Last active July 19, 2018 15:34
NSE functions
# https://www.enchufa2.es/archives/programming-with-dplyr-by-using-dplyr.html
# 1. reminder
starwars_mean <- function(var) {
var <- enquo(var)
starwars %>% group_by(!!var)
}
starwars_mean(homeworld)
# 2. parsing a string inside
@grayskripko
grayskripko / tally_count.R
Last active July 24, 2018 15:04
tally() and count()
``` r
library(dplyr)
x <- tibble(a=c(1, 2, 2, 3))
x %>% group_by(a) %>% summarise(n())
#> # A tibble: 3 x 2
#> a `n()`
#> <dbl> <int>
#> 1 1 1
@grayskripko
grayskripko / cb_best_model.R
Last active August 24, 2018 14:02
xgb.cv callback for model in best iteration
pacman::p_load(tidyverse, xgboost)
# in xgboost: "cb" for "callback", "bst" for "booster"
cb_best_model <- function(print_improvements, save_cv_models) {
# callback is the main function that will be executed on every iteration
callback <- function(env = parent.frame(), finalize = FALSE) {
if (is.null(env$basket) || is.null(env$bst_folds))
stop("'cb_best_model' callback requires 'basket' and 'bst_folds' lists
in its calling frame")
@grayskripko
grayskripko / nse_dot_not_found.R
Last active November 29, 2018 11:28
Functional sequence "." not found
library(tidyverse)
# case 1
forest %>% map(predictions) %>% map(assess) %>% transpose() %>%
map(~imap(., ~rename(.x, !!.y := value)))
# > ... works
forest %>% map(predictions) %>% map(assess) %>% transpose() %>%
map(~imap(~rename(.x, !!.y := value)))
# > Error in as_mapper(.f, ...) : argument ".f" is missing, with no default
```
... package ‘parsnip’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\Gray\AppData\Local\Temp\Rtmps7lBHE\downloaded_packages
parsnip installed
Failed to install/load:
parsnip
```