# 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 + 2 * 2], = [,
[3, 4]]    ]     [3 * 1 + 4 * 2]]    ]```

Results in a 2x1 column vector

in numpy:

```In : A = np.array([[1,2],[3,4]])

In : V = np.array([,])

In : A.dot(V)
Out:
array([[ 5],
])
```

## Solve system of linear equations

```x - y = 3
7x - y = -3```
```[[1, -1]  . [[x],  = [,
[7, -1]]    [y]]     [-3]]```

This can go back to the original equations with variables:

```[[1, -1]  . [[x],  = [[1x + -1y], = [,
[7, -1]]    [y]]     [7x + -1y]]    [-3]]```

### Solving in numpy

```In : a = np.array([[1, -1],[7, -1]])

In : a
Out:
array([[ 1, -1],
[ 7, -1]])

In : b = np.array([,[-3]])

In : b
Out:
array([[ 3],
[-3]])

In : a_inverse = np.linalg.inv(a)

In : a_inverse
Out:
array([[-0.16666667,  0.16666667],
[-1.16666667,  0.16666667]])

In : a_inverse.dot(b)
Out:
array([[-1.],
[-4.]])```

solve for x,y, and z

```x + y + z = 6
2y + 5z = -4
2x + 5y - z = 27```

## Open the the csv with pandas

```import pandas as pd
```

### show only days where price is above 1000USD

```In : 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 : import datetime as dt

In : dt.datetime.utcnow() - dt.timedelta(days=365/2)
Out: datetime.datetime(2016, 9, 8, 5, 12, 52, 204106)

In : datetime_to_check = dt.datetime.utcnow() - dt.timedelta(days=365/2)

In : date_to_check = datetime_to_check.date()

In : date_to_check
Out: datetime.date(2016, 9, 8)
...
In : btc_price[btc_price.date == date_to_check]
Out:
datetime         usd
184 2016-09-08  627.777875
```

### What was the average price last year?

```: btc_price.usd.mean()
```

### What was the average price in august?

```In : august_end = dt.date(month=9, year=2016, day=1)

In : august_start = dt.date(month=8, year=2016, day=1)

In : btc_price[(august_start <= btc_price.date) & (btc_price.date < august_end)]
Out:
date         usd
146 2016-08-01  606.322343
...
176 2016-08-31  574.827937

In : btc_price[(august_start <= btc_price.date) & (btc_price.date < august_end)].mean()
Out:
usd    579.744405
dtype: float64
```

### Plot the days on a line graph

```import matplotlib.pyplot as plt
import pandas as pd

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()
```