Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Eligijus112 / read_hourly_ts
Created February 11, 2020 05:45
Reading and formating hourly time series
# Loading pandas
import pandas as pd
# Loading date wrangling package
from datetime import datetime
# Reading the input data
d = pd.read_csv('input/DAYTON_hourly.csv')
# Formating to datetime
@Eligijus112
Eligijus112 / create_X_Y
Last active February 11, 2020 06:12
A function to convert a time series to X and Y matrices for deep learning
import numpy as np
def create_X_Y(ts: list, lag: int) -> tuple:
"""
A method to create X and Y matrix from a time series list for the training of
deep learning models
"""
X, Y = [], []
if len(ts) - lag <= 0:
@Eligijus112
Eligijus112 / NN_LSTM_TS
Created February 11, 2020 06:22
An LSTM neural network for time series
# Deep learning packages
from keras.models import Sequential
from keras.layers import LSTM, Dense
# Defining the number of neurons in the LSTM layer
n_layer = 50
# Defining how many lags will be used in the time series
n_lag = 3
@Eligijus112
Eligijus112 / DeepTSClass
Last active December 28, 2022 16:30
A class to model time series data using LSTM deep learning model
# Data wrangling
import pandas as pd
import numpy as np
# Deep learning:
from keras.models import Sequential
from keras.layers import LSTM, Dense
class DeepModelTS():
@Eligijus112
Eligijus112 / text_preprocesing_embed
Last active April 22, 2022 19:58
Text preprocesing function
import re
def clean_text(
string: str,
punctuations=r'''!()-[]{};:'"\,<>./?@#$%^&*_~''',
stop_words=['the', 'a', 'and', 'is', 'be', 'will']) -> str:
"""
A method to clean text
"""
# Cleaning the urls
# Defining the window for context
window = 2
# Creating a placeholder for the scanning of the word list
word_lists = []
all_text = []
for text in texts:
# Cleaning the text
@Eligijus112
Eligijus112 / unique_word_dictionary
Created March 4, 2020 05:41
Creates a unique word dictionary using from a list of strings
def create_unique_word_dict(text:list) -> dict:
"""
A method that creates a dictionary where the keys are unique words
and key values are indices
"""
# Getting all the unique words from our text and sorting them alphabetically
words = list(set(text))
words.sort()
# Creating the dictionary for the unique words
@Eligijus112
Eligijus112 / X_Y_creation_word_embeding
Last active October 31, 2020 16:57
An example of how to create X and Y for word embedding training
from scipy import sparse
import numpy as np
# Defining the number of features (unique words)
n_words = len(unique_word_dict)
# Getting all the unique words
words = list(unique_word_dict.keys())
# Creating the X and Y matrices using one hot encoding
# Deep learning:
from keras.models import Input, Model
from keras.layers import Dense
# Defining the size of the embedding
embed_size = 2
# Defining the neural network
inp = Input(shape=(X.shape[1],))
x = Dense(units=embed_size, activation='linear')(inp)
# Deep learning:
from keras.models import Input, Model
from keras.layers import Dense
# Defining the size of the embedding
embed_size = 2
# Defining the neural network
inp = Input(shape=(X.shape[1],))
x = Dense(units=embed_size, activation='linear')(inp)