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 | |
def ols(x,y): | |
x_mean = x.mean() | |
y_mean = y.mean() | |
beta_1 = np.multiply((x - x_mean), (y - y_mean)).sum() / np.square((x - x_mean)).sum() | |
beta_0 = y_mean - (beta_1 * x_mean) | |
def lin_model(new_x, slope=beta_1, intercept=beta_0): | |
return new_x * slope + intercept |
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
def ols(x,y): | |
x_mean = x.mean() | |
y_mean = y.mean() | |
beta_1 = np.multiply((x - x_mean), (y - y_mean)).sum() / np.square((x - x_mean)).sum() | |
beta_0 = y_mean - (beta_1 * x_mean) | |
def lin_model(new_x, slope=beta_1, intercept=beta_0): | |
return new_x * slope + intercept | |
return lin_model |
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
def is_palindrome(sequence): | |
return sequence == sequence[::-1] | |
# test when sequence IS palindrome | |
def test_true(): | |
seq = 'bob' | |
assert is_palindrome(seq) == True | |
def test_true_list(): | |
seq = [1, 0, 1] |
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 pytest | |
@pytest.fixture | |
def default_list(): | |
return [1, 2] | |
def test_fixture_1(default_list): | |
del default_list[0] | |
assert default_list == [2] |
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 pytest | |
def is_int(age): | |
return isinstance(age, int) | |
@pytest.mark.parametrize("age", [ | |
"five", | |
1.5, | |
{2: 3}, | |
[2, 5], |
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 | |
def ols(x,y): | |
x_mean = x.mean() | |
y_mean = y.mean() | |
beta_1 = np.multiply((x - x_mean), (y - y_mean)).sum() / np.square((x - x_mean)).sum() | |
beta_0 = y_mean - (beta_1 * x_mean) | |
def lin_model(new_x, slope=beta_1, intercept=beta_0): | |
return new_x * slope + intercept |
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
from scipy import optimize | |
import matplotlib.pyplot as plt | |
import numpy as np | |
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15], dtype=float) | |
y = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59, 84.47, 90, 80, 70, 60]) | |
def piecewise_linear(x, y0, y1, b0, b1, b2): | |
x0 = 6 | |
x1 = 12 |
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 pandas as pd | |
import matplotlib.pyplot as plt | |
from datetime import datetime | |
def parser(x): | |
return datetime.strptime('190'+x, '%Y-%m') | |
series = pd.read_csv('data/sales-of-shampoo-over-a-three-ye.csv', \ | |
header=0, \ | |
parse_dates=[0], \ |
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
from pandas.tools.plotting import autocorrelation_plot | |
autocorrelation_plot(series) | |
plt.show() |
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
from statsmodels.tsa.arima_model import ARIMA | |
# fit model | |
model = ARIMA(series, order=(5,1,0)) | |
model_fit = model.fit(disp=0) | |
# print(model_fit.summary()) | |
# plot residual erros | |
residuals = pd.DataFrame(model_fit.resid) | |
residuals.plot() |
OlderNewer