Skip to content

Instantly share code, notes, and snippets.

@sinhrks
sinhrks / mlogit.py
Last active August 29, 2015 14:10
Multinominal Logistic Regression
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# rpy2 経由で R から iris をロード
# import pandas.rpy.common as com
# iris = com.load_data('iris')
@sinhrks
sinhrks / logit.py
Last active August 29, 2015 14:10
Logistic Regression
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# rpy2 経由で R から iris をロード
# import pandas.rpy.common as com
# iris = com.load_data('iris')
@sinhrks
sinhrks / dtw.R
Created November 14, 2014 13:36
Dynamic Time Warping
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
library(animation)
plot_dtw_matrix <- function(ts_a, ts_b, i, j, cost, dist) {
.plot_matrix <- function(m, title, low, high) {
d <- dplyr::tbl_df(data.frame(m))
@sinhrks
sinhrks / KalmanFilter04.R
Last active August 29, 2015 14:08
KalmanFilter (multivariate) with ggplot2 animation Pt2
set.seed(1)
# 観測系列のサンプルサイズ
n <- 30
# 加速度
a <-rep(0.3, n)
v <- cumsum(0.5 * a)
x <- cumsum(v)
@sinhrks
sinhrks / KalmanFilter03.R
Created November 4, 2014 15:26
KalmanFilter (multivariate) with ggplot2 animation
set.seed(1)
# 観測系列のサンプルサイズ
n <- 100
# 真の値
x <- c(rep(0, n / 4), seq(0, 10, length.out = n / 4),
rep(10, n / 4), seq(10, 0, length.out = n / 4))
y <- c(seq(0, 10, length.out = n / 4), rep(10, n / 4),
seq(10, 0, length.out = n / 4), rep(0, n / 4))
@sinhrks
sinhrks / KalmanFilter02.R
Created November 1, 2014 22:13
KalmanFilter (univariate) with ggplot2 animation Pt2
set.seed(1)
# 観測系列のサンプルサイズ
n <- 200
# 真の値が時間変化する
actual <- 3.0 + cumsum(rnorm(n, sd = 0.2))
# 観測される値 (誤差は標準偏差2の正規分布とする)
observed <- actual + rnorm(n, mean = 0, sd = 2)
@sinhrks
sinhrks / KalmanFilter01.R
Last active August 29, 2015 14:08
KalmanFilter (univariate) with ggplot2 animation
set.seed(1)
# 観測系列のサンプルサイズ
n <- 120
# 真の値
actual <- rep(5, out.length = n)
# 観測される値 (誤差は標準偏差2の正規分布とする)
observed <- actual + rnorm(n, mean = 0, sd = 2)
@sinhrks
sinhrks / fortify_survfit.R
Created October 4, 2014 10:53
Allow ggplot2 to handle survival::survfit result
library(survival)
library(ggplot2)
library(scales)
d.survfit <- survival::survfit(survival::Surv(time, status) ~ sex,
data = lung)
fortify.survfit <- function(survfit.data) {
data.frame(time = survfit.data$time,
@sinhrks
sinhrks / fortify_forecast.R
Created October 4, 2014 05:11
Allow ggplot2 to handle forecast result
library(forecast)
library(ggplot2)
d <- AirPassengers
d.arima <- forecast::auto.arima(d)
d.forecast <- forecast(d.arima, level = c(95), h = 50)
fortify.forecast <- function(forecast.data) {
require(dplyr)
forecasted <- as.data.frame(forecast.data)
@sinhrks
sinhrks / df_plot_pie.py
Created April 27, 2014 00:45
Pandas plotting with pie
import pandas as pd
import numpy as np
import pandas.util.testing as tm
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
series = pd.Series(3 * np.random.rand(4), index=['a', 'b', 'c', 'd'], name='series')