I719 Fundamentals of Python/lecture6: Difference between revisions
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Lists and Arrays = | = Lists and Arrays = | ||
== | == Part 1: List manipulation == | ||
Please see, you should know how to do these things!<br /> | |||
https://wiki.itcollege.ee/index.php/I719_Fundamentals_of_Python/lists | |||
= Interlude: Packages and Requirements = | = Interlude: Packages and Requirements = | ||
Line 78: | Line 18: | ||
and setup.py include information on depedencies<br /> | and setup.py include information on depedencies<br /> | ||
see: https://github.com/pypa/sampleproject/blob/master/setup.py | see: https://github.com/pypa/sampleproject/blob/master/setup.py | ||
=== NOTE === | |||
Numpy and Pandas will not be tested on and are not required to know to continue in the class. These packages will only be used in this lecture | |||
= Numpy = | = Numpy = | ||
Line 89: | Line 33: | ||
<code>import numpy as np</code> | <code>import numpy as np</code> | ||
== Matrix multiplication == | |||
a 2x2 matrix by a 2x1 column vector | |||
<pre>[[1, 2] . [[1], = [[1 * 1 + 2 * 2], = [[5], | |||
[3, 4]] [2]] [3 * 1 + 4 * 2]] [11]]</pre> | |||
Results in a 2x1 column vector | |||
in numpy: | |||
<source lang="python">In [2]: A = np.array([[1,2],[3,4]]) | |||
In [3]: V = np.array([[1],[2]]) | |||
In [4]: A.dot(V) | |||
Out[4]: | |||
array([[ 5], | |||
[11]])</source> | |||
== Solve system of linear equations == | == Solve system of linear equations == | ||
<pre>x - y = 3 | <pre>x - y = 3 | ||
7x - y = -3</pre> | 7x - y = -3</pre> | ||
becomes in matrix | becomes in matrices<br /> | ||
(see how: https://www.khanacademy.org/math/precalculus/precalc-matrices/solving-equations-with-inverse-matrices/v/matrix-equations-systems) | |||
<pre>[[1, -1] . [[x], = [[3], | |||
[7, -1]] [y]] [-3]]</pre> | |||
This can go back to the original equations with variables: | |||
<pre>[[1, -1] . [[x], = [[1x + -1y], = [[3], | |||
[7, -1]] [y]] [7x + -1y]] [-3]]</pre> | |||
=== Solving in numpy === | |||
<pre>In [68]: a = np.array([[1, -1],[7, -1]]) | <pre>In [68]: a = np.array([[1, -1],[7, -1]]) | ||
Line 143: | Line 107: | ||
<source lang="python">import pandas as pd | <source lang="python">import pandas as pd | ||
btc_price = pd.read_csv('~/Downloads/market-price.csv', names=[' | btc_price = pd.read_csv('~/Downloads/market-price.csv', names=['date', 'usd'], parse_dates=[0])</source> | ||
== TASKS == | == TASKS == | ||
=== show only days where price is above 1000USD === | === show only days where price is above 1000USD === | ||
<source lang="python">In [4]: btc_price[btc_price.usd > 1000] | |||
</source> | |||
=== How many days was the price above 1000USD? === | === How many days was the price above 1000USD? === | ||
<source lang="python">len(btc_price[btc_price.usd > 1000])</source> | |||
=== What was the price 6 months ago? === | === What was the price 6 months ago? === | ||
<source lang="python">In [3]: import datetime as dt | |||
In [4]: dt.datetime.utcnow() - dt.timedelta(days=365/2) | |||
Out[4]: datetime.datetime(2016, 9, 8, 5, 12, 52, 204106) | |||
In [5]: datetime_to_check = dt.datetime.utcnow() - dt.timedelta(days=365/2) | |||
In [6]: date_to_check = datetime_to_check.date() | |||
In [7]: date_to_check | |||
Out[7]: datetime.date(2016, 9, 8) | |||
... | |||
In [9]: btc_price[btc_price.date == date_to_check] | |||
Out[9]: | |||
datetime usd | |||
184 2016-09-08 627.777875 | |||
</source> | |||
=== What was the average price last year? === | === What was the average price last year? === | ||
<source lang="python">[10]: btc_price.usd.mean()</source> | |||
=== What was the average price in august? === | === What was the average price in august? === | ||
<source lang="python">In [27]: august_end = dt.date(month=9, year=2016, day=1) | |||
In [28]: august_start = dt.date(month=8, year=2016, day=1) | |||
In [29]: btc_price[(august_start <= btc_price.date) & (btc_price.date < august_end)] | |||
Out[29]: | |||
date usd | |||
146 2016-08-01 606.322343 | |||
... | |||
176 2016-08-31 574.827937 | |||
In [30]: btc_price[(august_start <= btc_price.date) & (btc_price.date < august_end)].mean() | |||
Out[30]: | |||
usd 579.744405 | |||
dtype: float64</source> | |||
=== Plot the days on a line graph === | === Plot the days on a line graph === | ||
Line 161: | Line 161: | ||
import pandas as pd | import pandas as pd | ||
btc_price = pd.read_csv('~/Downloads/market-price.csv', names=[' | btc_price = pd.read_csv('~/Downloads/market-price.csv', names=['date', 'usd'], parse_dates=[0]) | ||
x = btc_price. | x = btc_price.date | ||
y = btc_price.usd | y = btc_price.usd | ||
plt.plot(x, y) | plt.plot(x, y) | ||
Line 174: | Line 174: | ||
plt.grid(True) | plt.grid(True) | ||
plt.show()</source> | plt.show()</source> | ||
== More information on Data Science in python == | |||
see https://github.com/jakevdp/PythonDataScienceHandbook/ |
Latest revision as of 19:33, 9 March 2017
Lists and Arrays
Part 1: List manipulation
Please see, you should know how to do these things!
https://wiki.itcollege.ee/index.php/I719_Fundamentals_of_Python/lists
Interlude: Packages and Requirements
python application often include a requirements.txt
that lists pip dependencies
python packages are formatted as
package_name/ package_name/ __init__.py setup.py
and setup.py include information on depedencies
see: https://github.com/pypa/sampleproject/blob/master/setup.py
NOTE
Numpy and Pandas will not be tested on and are not required to know to continue in the class. These packages will only be used in this lecture
Numpy
- it is faster
- it allows multidimensional arrays
- meant for math
Basics
import numpy as np
Matrix multiplication
a 2x2 matrix by a 2x1 column vector
[[1, 2] . [[1], = [[1 * 1 + 2 * 2], = [[5], [3, 4]] [2]] [3 * 1 + 4 * 2]] [11]]
Results in a 2x1 column vector
in numpy:
In [2]: A = np.array([[1,2],[3,4]])
In [3]: V = np.array([[1],[2]])
In [4]: A.dot(V)
Out[4]:
array([[ 5],
[11]])
Solve system of linear equations
x - y = 3 7x - y = -3
becomes in matrices
(see how: https://www.khanacademy.org/math/precalculus/precalc-matrices/solving-equations-with-inverse-matrices/v/matrix-equations-systems)
[[1, -1] . [[x], = [[3], [7, -1]] [y]] [-3]]
This can go back to the original equations with variables:
[[1, -1] . [[x], = [[1x + -1y], = [[3], [7, -1]] [y]] [7x + -1y]] [-3]]
Solving in numpy
In [68]: a = np.array([[1, -1],[7, -1]]) In [69]: a Out[69]: array([[ 1, -1], [ 7, -1]]) In [70]: b = np.array([[3],[-3]]) In [71]: b Out[71]: array([[ 3], [-3]]) In [72]: a_inverse = np.linalg.inv(a) In [73]: a_inverse Out[73]: array([[-0.16666667, 0.16666667], [-1.16666667, 0.16666667]]) In [74]: a_inverse.dot(b) Out[74]: array([[-1.], [-4.]])
Task 3
solve for x,y, and z
x + y + z = 6 2y + 5z = -4 2x + 5y - z = 27
Pandas
https://blockchain.info/charts/market-price?timespan=1year
download the csv
Open the the csv with pandas
import pandas as pd
btc_price = pd.read_csv('~/Downloads/market-price.csv', names=['date', 'usd'], parse_dates=[0])
TASKS
show only days where price is above 1000USD
In [4]: btc_price[btc_price.usd > 1000]
How many days was the price above 1000USD?
len(btc_price[btc_price.usd > 1000])
What was the price 6 months ago?
In [3]: import datetime as dt
In [4]: dt.datetime.utcnow() - dt.timedelta(days=365/2)
Out[4]: datetime.datetime(2016, 9, 8, 5, 12, 52, 204106)
In [5]: datetime_to_check = dt.datetime.utcnow() - dt.timedelta(days=365/2)
In [6]: date_to_check = datetime_to_check.date()
In [7]: date_to_check
Out[7]: datetime.date(2016, 9, 8)
...
In [9]: btc_price[btc_price.date == date_to_check]
Out[9]:
datetime usd
184 2016-09-08 627.777875
What was the average price last year?
[10]: btc_price.usd.mean()
What was the average price in august?
In [27]: august_end = dt.date(month=9, year=2016, day=1)
In [28]: august_start = dt.date(month=8, year=2016, day=1)
In [29]: btc_price[(august_start <= btc_price.date) & (btc_price.date < august_end)]
Out[29]:
date usd
146 2016-08-01 606.322343
...
176 2016-08-31 574.827937
In [30]: btc_price[(august_start <= btc_price.date) & (btc_price.date < august_end)].mean()
Out[30]:
usd 579.744405
dtype: float64
Plot the days on a line graph
import matplotlib.pyplot as plt
import pandas as pd
btc_price = pd.read_csv('~/Downloads/market-price.csv', names=['date', 'usd'], parse_dates=[0])
x = btc_price.date
y = btc_price.usd
plt.plot(x, y)
plt.xlabel('Date')
plt.ylabel('USD')
plt.title('BTC to USD')
plt.grid(True)
plt.show()