This file contains hidden or 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 | |
| import padasip as pa | |
| import matplotlib.pylab as plt | |
| # prep data | |
| N = 200 # the overall time series size | |
| n = 5 # size of sample we want to feed into the filter | |
| s = np.random.random(N) # generate the source input | |
| d = np.zeros(N) # initialize the target array | |
| for k in range((n-1), N): |
This file contains hidden or 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 numpy as np | |
| import padasip as pa | |
| SEED = 121 | |
| np.random.seed(SEED) | |
| n = 5 | |
| s = ts['Close'].values.flatten()[: 1000] # initial timeseries data | |
| x = pa.input_from_history(s, n) # input matrix |
This file contains hidden or 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
| s_future = ts['Close'].values.flatten()[1000: ] | |
| filter_min_error = pa_list[np.argmin(error_list)] | |
| print(f'Selected the filter with mu of {filter_min_error.mu}') | |
| print(f'with avg eror of {error_list[np.argmin(error_list)]}') | |
| x_future = pa.input_from_history(s_future, n) | |
| d_future = np.zeros(len(x_future)) | |
| N_future = len(x_future) | |
| for i, k in enumerate(range((n-1), N_future)): | |
| d_future[i] = s_future[k+1] |
This file contains hidden or 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 | |
| import statsmodels.api as sm | |
| from sklearn.linear_model import LinearRegression | |
| # generate sample data (single linear) | |
| X = 2 * np.random.rand(200, 1) | |
| y = 1.2 * X + 1 + 0.8 * np.random.randn(200, 1) | |
| X_ = sm.add_constant(X) # add constant for intercept computation | |
| print('Method 1: matrix formulation') |
This file contains hidden or 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 batch_gradient_descent(learning_rate, X, y, epochs: int, | |
| return_model_result: bool=True): | |
| # initial outputs | |
| mse_ = [] | |
| cost_ = [] | |
| theta_ = [] | |
| n = X.shape[0] | |
| theta = np.ones(X.shape[1]) # set default weights | |
| X_transpose = X.T | |
| for i in range(0, epochs): |
This file contains hidden or 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 _iter(X, y, | |
| batch_size: int=1): | |
| n_observations = X.shape[0] | |
| idx = list(range(n_observations)) | |
| random.shuffle(idx) | |
| for batch_id, i in enumerate(range(0, n_observations, batch_size)): | |
| _pos = np.array(idx[i: min(i + batch_size, n_observations)]) | |
| yield batch_id, X.take(_pos, axis=0), y.take(_pos) | |
This file contains hidden or 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 sklearn.linear_model import SGDRegressor | |
| SGD_rgs_normal = SGDRegressor(fit_intercept=True, random_state=SEED, eta0=learning_rate, | |
| learning_rate='constant', max_iter=n_epochs) | |
| SGD_rgs_normal.fit(X, y) | |
| print(SGD_rgs_normal) | |
| print(f'Intercept: {SGD_rgs_normal.intercept_}, weights: {SGD_rgs_normal.coef_}') | |
| y_pred = SGD_rgs_normal.predict(X) | |
| _ = print_regress_metric(y, y_pred) |
This file contains hidden or 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 _get_chunk(X, y, chunkrows): | |
| X_chunk, y_chunk = X[chunkrows], y[chunkrows] | |
| return X_chunk, y_chunk | |
| def _iter_minibatch(X, y, chunk_size): | |
| ''' | |
| Construct minibatch generator | |
| ''' | |
| _start = 0 |
This file contains hidden or 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 numpy as np | |
| import random | |
| from sklearn.datasets import make_regression | |
| import tensorflow as tf | |
| from tensorflow import keras | |
| from tensorflow.keras import layers | |
| # require for installation: !pip install -q git+https://github.com/tensorflow/docs |
This file contains hidden or 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 | |
| import tensorflow as tf | |
| from tensorflow import keras | |
| from tensorflow.keras import layers | |
| # require for installation: !pip install -q git+https://github.com/tensorflow/docs | |
| import tensorflow_docs as tfdocs | |
| import tensorflow_docs.plots | |
| import tensorflow_docs.modeling |