Skip to content

Instantly share code, notes, and snippets.

View ranjiGT's full-sized avatar
🧿
Learning something new!

Ranji Raj ranjiGT

🧿
Learning something new!
View GitHub Profile
@ranjiGT
ranjiGT / kernel.py
Created May 12, 2021 11:56
A piepline for creating polynomial degree 2 kernel matrix for and its vector norm
import numpy as np
X = np.array([[-0.5, 1], [-1, -1.5], [-1.5, 1.5], [1.5, -0.5], [0.5, -0.5]])
y = np.array([-1, 1, 1, 1, -1])
res = np.empty((5, 5))
def k(X):
for i in range(X.shape[0]):
for j in range(X.shape[0]):
res[i][j] = np.dot(X[i], X[j]) ** 2
return res
print(k(X))
@ranjiGT
ranjiGT / pipe9.R
Created May 3, 2021 17:54
Pipeline for generating frequency barhplots
roll_calls_imp <-
un_roll_call_issues %>%
left_join(un_roll_calls) %>%
filter(importantvote == 1)
roll_calls_imp %>%
count(issue) %>%
ggplot(aes(reorder(issue, n) , n)) +
geom_col() +
coord_flip()+
@ranjiGT
ranjiGT / touchupSO.R
Created May 2, 2021 09:54
A touch up script in R for problem-solving
library(tidyverse)
category <- c("red", "blue", "green", "yellow")
tag <- c("yes", "no", "maybe", "idk")
value <- c(21, 1, 10, 9)
df <- data.frame(category, tag, value) %>%
mutate(prop = value/sum(value))
df %>%
ggplot(aes(tag, prop, fill = category))+
geom_col()+
@ranjiGT
ranjiGT / pipe5.R
Last active May 2, 2021 09:51
A basic pipeline on gapminder data
my_gapminder <- gapminder %>%
# filter observations were the country is either Africa, Asia or Europe
filter(continent %in% c("Africa", "Asia", "Europe")) %>%
# compute mean country population per continent and year
group_by(continent, year) %>%
summarize(mean_pop = mean(pop)) %>%
ungroup() %>%
# compute population growth as relative difference to continent population
# in 1958
group_by(continent) %>%
@ranjiGT
ranjiGT / KNN.R
Last active April 4, 2021 17:10
KNN classifier in action
library(caret)
# Create training and test sets
set.seed(123)
trainIndex <- sample(c(FALSE,TRUE), size = nrow(dat), prob = c(.25,.75), replace = TRUE)
train_set <- dat[trainIndex, ]
test_set <- dat[!trainIndex, ]
# Learn KNN classifier
fit <- knn3Train(train_set %>% select(-y), test_set %>% select(-y), cl = train_set$y,
k = 3, prob = F)