Python Time Series Forecasting

Data Viz in Python – Color Palettes in Seaborn

Color Palettes in Seaborn Preliminaries import pandas as pd %matplotlib inline import matplotlib.pyplot as plt import seaborn as sns data = {‘date’: [‘2014-05-01 18:47:05.069722’, ‘2014-05-01 18:47:05.119994’, ‘2014-05-02 18:47:05.178768’, ‘2014-05-02 18:47:05.230071’, ‘2014-05-02 18:47:05.230071’, ‘2014-05-02 18:47:05.280592’, ‘2014-05-03 18:47:05.332662’, ‘2014-05-03 18:47:05.385109’, ‘2014-05-04 18:47:05.436523’, ‘2014-05-04 18:47:05.486877’], ‘deaths_regiment_1’: [34, 43, 14, 15, 15, 14, 31, 25, 62, 41], ‘deaths_regiment_2’: [52, …

Data Wrangling in Python – Pandas Time Series Basics

Pandas Time Series Basics Import modules from datetime import datetime import pandas as pd %matplotlib inline import matplotlib.pyplot as pyplot Create a dataframe data = {‘date’: [‘2014-05-01 18:47:05.069722’, ‘2014-05-01 18:47:05.119994’, ‘2014-05-02 18:47:05.178768’, ‘2014-05-02 18:47:05.230071’, ‘2014-05-02 18:47:05.230071’, ‘2014-05-02 18:47:05.280592’, ‘2014-05-03 18:47:05.332662’, ‘2014-05-03 18:47:05.385109’, ‘2014-05-04 18:47:05.436523’, ‘2014-05-04 18:47:05.486877’], ‘battle_deaths’: [34, 25, 26, 15, 15, 14, 26, 25, …

Data Wrangling in Python – How to Group A Time Series With pandas

Group A Time Series With pandas Import required modules import pandas as pd import numpy as np Create a dataframe df = pd.DataFrame() df[‘german_army’] = np.random.randint(low=20000, high=30000, size=100) df[‘allied_army’] = np.random.randint(low=20000, high=40000, size=100) df.index = pd.date_range(‘1/1/2014′, periods=100, freq=’H’) df.head() german_army allied_army 2014-01-01 00:00:00 21413 37604 2014-01-01 01:00:00 25913 21144 2014-01-01 02:00:00 22418 34201 2014-01-01 03:00:00 …

Data Wrangling in Python – How to Create A pandas Column With A For Loop

Create A pandas Column With A For Loop Preliminaries import pandas as pd import numpy as np Create an example dataframe raw_data = {‘student_name’: [‘Miller’, ‘Jacobson’, ‘Ali’, ‘Milner’, ‘Cooze’, ‘Jacon’, ‘Ryaner’, ‘Sone’, ‘Sloan’, ‘Piger’, ‘Riani’, ‘Ali’], ‘test_score’: [76, 88, 84, 67, 53, 96, 64, 91, 77, 73, 52, np.NaN]} df = pd.DataFrame(raw_data, columns = [‘student_name’, …

Data Wrangling in Python – How to Convert A Variable To A Time Variable In pandas

Convert A Variable To A Time Variable In pandas /* Import Preliminaries */ import pandas as pd /* Create a dataset with the index being a set of names */ raw_data = {‘date’: [‘2014-06-01T01:21:38.004053’, ‘2014-06-02T01:21:38.004053’, ‘2014-06-03T01:21:38.004053’], ‘score’: [25, 94, 57]} df = pd.DataFrame(raw_data, columns = [‘date’, ‘score’]) df date score 0 2014-06-01T01:21:38.004053 25 1 2014-06-02T01:21:38.004053 …

Learn Python By Example – Date And Time Basics

Date And Time Basics /* Import modules */ from datetime import datetime from datetime import timedelta /* Create a variable with the current time */ now = datetime.now() now datetime.datetime(2014, 5, 11, 20, 5, 11, 688051) /* The current year */ now.year 2014 /* The current month */ now.month 5 /* The current day */ …

Machine Learning for Beginners in Python: Dimensionality Reduction With PCA

Dimensionality Reduction With PCA Preliminaries /* Load libraries */ from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA from sklearn import datasets Load Data /* Load the data */ digits = datasets.load_digits() Standardize Feature Values /* Standardize the feature matrix */ X = StandardScaler().fit_transform(digits.data) Conduct Principal Component Analysis /* Create a PCA that will retain 99% …

Machine Learning for Beginners in Python: Dimensionality Reduction With Kernel PCA

Dimensionality Reduction With Kernel PCA Preliminaries /* Load libraries */ from sklearn.decomposition import PCA, KernelPCA from sklearn.datasets import make_circles Create Linearly Inseparable Data /* Create linearly inseparable data */ X, _ = make_circles(n_samples=1000, random_state=1, noise=0.1, factor=0.1) Conduct Kernel PCA /* Apply kernal PCA with radius basis function (RBF) kernel */ kpca = KernelPCA(kernel=”rbf”, gamma=15, n_components=1) …

Machine Learning for Beginners in Python: Dimensionality Reduction On Sparse Feature Matrix

Dimensionality Reduction On Sparse Feature Matrix Preliminaries /* Load libraries */ from sklearn.preprocessing import StandardScaler from sklearn.decomposition import TruncatedSVD from scipy.sparse import csr_matrix from sklearn import datasets import numpy as np Load Digits Data And Make Sparse /* Load the data */ digits = datasets.load_digits() /* Standardize the feature matrix */ X = StandardScaler().fit_transform(digits.data) /* …

Machine Learning for Beginners in Python: How to Select Date And Time Ranges

Select Date And Time Ranges Preliminaries /* Load library */ import pandas as pd Create pandas Series Time Data /* Create data frame */ df = pd.DataFrame() /* Create datetimes */ df[‘date’] = pd.date_range(‘1/1/2001′, periods=100000, freq=’H’) Select Time Range (Method 1) Use this method if your data frame is not indexed by time. /* Select …