This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |