Permalink
Browse files

week 8

  • Loading branch information...
sam-stack committed Apr 18, 2019
1 parent 298acdb commit ce0099e87e74a9f8d885a7990daa54122cc268fb
@@ -0,0 +1,211 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Use the datetime library to represent dates as objects.\n",
"- There is an important library called `datetime` which allows us to create very important variables to represent time and date."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Year 2019\n",
"Month 1\n",
"Day 4\n",
"Hour 9\n",
"Minute 21\n",
"Second 12\n",
"Micro-Second 844089\n"
]
}
],
"source": [
"from datetime import datetime\n",
"# year, month, day, hour, minute, second, micro-second\n",
"# not all positions are required\n",
"lesson_date = datetime(2019, 1, 4, 9, 21, 12, 844089)\n",
"print(\"Year\", lesson_date.year)\n",
"print(\"Month\",lesson_date.month)\n",
"print(\"Day\", lesson_date.day)\n",
"print(\"Hour\", lesson_date.hour)\n",
"print(\"Minute\", lesson_date.minute)\n",
"print(\"Second\", lesson_date.second)\n",
"print(\"Micro-Second\", lesson_date.microsecond)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Learn how to calculate time differences with `timedelta()`.\n",
"- Time deltas are another unique datatype that are used to represent quantities of time as opposed to actual dates. \n",
"\n",
"- Time Deltas can take data as any value of time; days, hours, weeks, seconds, however, it only has attributes for days, seconds and milliseconds. All other inputed values are represented in their terms of those amounts.\n",
" - A millisecond is converted to 1000 microseconds.\n",
" - A minute is converted to 60 seconds.\n",
" - An hour is converted to 3600 seconds.\n",
" - A week is converted to 7 days."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time days 0\n",
"Time seconds 9746\n",
"Time microseconds 0\n"
]
}
],
"source": [
"# Import timedelta() from the datetime library.\n",
"from datetime import timedelta\n",
"\n",
"# Timedeltas represent time as an amount rather than as a fixed position.\n",
"my_marathon_time = timedelta( minutes=42, seconds=26,hours = 2)\n",
"\n",
"# The timedelta() has attributes that allow us to extract values from it.\n",
"print('Time days', my_marathon_time.days)\n",
"#print('Time Hours', my_marathon_time.hours) # try uncommentin this.\n",
"print('Time seconds', my_marathon_time.seconds)\n",
"print('Time microseconds', my_marathon_time.microseconds)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.timedelta(seconds=344)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_second_marathon = timedelta(minutes = 36,seconds = 42, hours = 2)\n",
"\n",
"my_marathon_time - my_second_marathon"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### \"Rolling\" statistics through time, such as the rolling mean.\n",
"- Rolling implies the idea of taking a moving range or window of values that is used for the calculation. \n",
"- When applying a rolling statistic to say a time series dataset we may specify a time window, say 14 days, and perform the calculation for all possible window frames of 14 days.\n",
"- Because we are not using the entirety of the dataset and only these windows, that stat is representative of the window in time **and** we make several calculations.\n",
" - **Window** indicates the number of days to include in the average.\n",
" - **Center** indicates whether the window should be centered on the date or use data prior to that date.\n",
" - **Freq** indicates which level to roll the averages up to (as used in resample): D for day, M for month, A for year, etc.\n",
"- Lets think of an example. \"Weekly rain fall volume\". This implies that we are measuring the rain fall of a 7 day period. Additionally, this is a calculation that we're interested in measuring every day. Because it is weekly, as days progress we can still only use the past 7 days for a calculation. \n",
"\n",
"**Rolling Total/AVG. Example Table**\n",
" \n",
"| Date | Rainfall | Date | Rainfall | Date | Rainfall | \n",
"|:---------|----------|:---------|----------|:---------|----------|\n",
"|Sept. 7 | 0 |Sept. 8 | 0 |Sept. 9 | 8.5 |\n",
"|Sept. 6 | 1.5 |Sept. 7 | 0 |Sept. 8 | 0 |\n",
"|Sept. 5 | 4 |Sept. 6 | 1.5 |Sept. 7 | 0 |\n",
"|Sept. 4 | 0 |Sept. 5 | 4 |Sept. 6 | 1.5 |\n",
"|Sept. 3 | 12 |Sept. 4 | 0 |Sept. 5 | 4 |\n",
"|Sept. 2 | 0 |Sept. 3 | 12 |Sept. 4 | 0 |\n",
"|Sept. 1 | 2.5 |Sept. 2 | 0 |Sept. 3 | 12 |\n",
"|**Total** | **20** |**Total** | **17.5** |**Total** | **26** |\n",
"|**Avg. ** | **2.85** |**Avg. ** | **2.5** |**Avg. ** | **3.71** |\n",
"\n",
"### Exponentially weighted statistics.\n",
"- Weighted statistics utilize a similar principal of rolling statistics, however they apply weights to variables.\n",
"- Typically the further back an observation is from the current point, the smaller its assigned weight will be. This ,in general, makes values that happened longer ago less influential to the statistic being calculated. \n",
"- This concept is called \"Exponential Decay\".\n",
"\n",
"### Autocorrelation and partial autocorrelation\n",
"\n",
"**Autocorrelation**\n",
"- Correlation of an observation $t$ to a lagged observation $t-l$. \n",
"- Typically when we look at the autocorrelation we are looking at every lag period up to $l$, not just $l$.\n",
" - Having a lag of 365 may indicate a year (if the data is reported daily).\n",
"\n",
"**Partial Autocorrelation**\n",
"- Correlation of an observation $t$ to a lagged observation $t-l$ with removal of already identified correlation in previous lags.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Oops, something went wrong.

0 comments on commit ce0099e

Please sign in to comment.