In [5]:
import csv
import numpy as np
import numpy.typing as npt

dictLst: dict = {}

with open("nino34.csv") as ifh:
    nino = csv.DictReader(ifh)
    for row in nino:
        for key in row:
            if not (key in dictLst):
                dictLst[key] = []
            dictLst[key].append(float(row[key]))
            
#print(dictLst)

ninoDict = {}
for key in dictLst:
    ninoDict[key] = np.array(dictLst[key])
    
#print(ninoDict)

fahrenheit = ninoDict["TOTAL"] * 9/5 + 32
print(fahrenheit)
[76.208 77.126 78.584 79.322 79.142 79.646 79.304 78.584 78.314 78.224
 77.828 77.522 77.468 78.296 80.438 81.662 82.274 81.914 81.662 80.618
 80.996 80.96  81.05  80.456 80.006 80.15  80.942 82.058 82.022 80.924
 79.736 79.34  79.43  79.25  78.656 79.178 80.132 80.6   81.626 82.454
 82.904 82.616 81.356 80.474 80.6   80.366 80.384 80.618 80.564 80.654
 80.438 79.97  80.798 80.222 78.98  77.756 77.198 77.396 78.008 77.468
 78.116 78.476 79.214 79.898 79.97  79.772 79.052 77.9   77.504 75.938
 75.65  76.226 77.612 78.386 79.646 80.348 80.834 80.258 79.196 78.206
 78.296 78.332 78.008 78.278 78.872 79.772 81.428 82.814 83.372 83.03
 82.67  81.824 81.356 81.338 81.716 82.202 82.976 82.832 82.886 82.904
 82.94  82.364 81.158 80.312 79.52  79.61  80.15  79.898 80.726 80.924
 81.446 82.184 81.842 81.248 79.574 78.962 78.656 79.232 78.872 79.124
 79.268 79.322 80.582 81.482 81.824 81.014 80.366 80.042 79.574 79.178
 79.25  79.196 79.214 79.808 80.492 81.266 81.95  81.788 80.366 79.124
 78.404 78.26  78.926 78.764 78.728 79.16  80.258 80.834 80.69  80.744
 80.15  79.376 78.674 78.728 78.35  78.206 78.386 79.214 80.924 82.022
 81.734 81.698 81.986 81.446 81.302 81.23  81.428 81.698 81.194 80.834
 80.636 80.51  80.276 79.844 79.394 78.062 77.576 77.648 77.468 77.396
 78.188 79.16  80.51  81.284 82.382 82.544 82.202 82.328 82.4   82.67
 82.598 82.31  81.788 81.59  82.778 82.688 81.59  81.734 81.176 79.664
 79.286 79.196 79.214 78.836 78.584 79.016 79.718 80.15  81.23  81.428
 80.528 79.574 78.53  78.728 78.926 78.71  78.242 78.242 79.412 80.78
 80.942 82.166 81.626 80.6   80.096 80.15  80.96  81.086 81.5   82.148
 82.094 82.652 82.922 81.842 80.744 80.636 80.87  81.212 80.798 80.564
 80.312 80.528 80.87  81.95  81.734 80.654 79.178 78.062 78.152 78.062
 78.026 77.486 76.676 77.36  78.674 79.952 80.51  79.88  79.016 78.332
 78.278 77.846 77.99  77.666 78.134 79.358 80.762 82.22  82.958 82.724
 82.652 82.292 82.292 82.85  83.48  83.624 82.994 82.31  81.608 81.05
 80.546 79.79  78.368 77.414 77.108 76.532 75.812 75.812 76.046 77.198
 78.548 79.664 79.97  79.754 79.502 79.394 78.926 78.386 78.098 78.134
 78.98  78.944 79.178 80.366 80.24  79.214 78.62  77.612 77.09  76.802
 77.09  76.406 76.172 77.882 79.628 80.402 80.96  81.23  80.816 80.564
 80.636 81.428 81.338 80.744 81.176 80.834 81.464 81.41  81.896 81.932
 81.284 80.312 80.798 81.212 80.924 81.122 80.906 80.618 80.78  80.816
 80.96  80.636 80.132 78.926 78.818 79.25  79.376 79.772 79.556 79.772
 81.104 82.112 81.842 81.374 80.276 80.15  80.582 80.294 80.582 80.798
 80.762 80.564 81.176 81.968 82.436 82.292 81.014 79.664 79.592 79.628
 79.88  79.97  79.142 79.016 79.988 81.14  81.248 81.086 79.97  79.376
 79.736 79.556 79.322 79.52  80.024 79.88  81.356 82.454 83.102 82.868
 81.788 81.644 82.76  83.642 83.498 83.822 83.984 83.642 83.606 83.786
 83.912 82.904 80.942 79.88  79.16  78.026 77.756 78.044 78.584 79.826
 80.204 80.87  80.96  80.294 80.204 79.88  79.484 78.872 77.936 77.468
 77.702 78.872 79.7   79.97  80.438 80.258 79.79  79.322 78.836 79.214
 79.394 79.142 78.602 78.908 80.384 81.482 81.338 81.356 80.924 80.906
 81.032 81.518 81.86  81.878 81.824 82.202 82.886 83.12  83.408 83.552
 83.444 83.138 83.048 82.31  81.986 81.572 81.41  80.672 81.302 81.284
 80.024 78.782 78.008 78.206 78.296 76.676 76.388 76.352 76.262 77.522
 78.962 80.15  80.726 80.852 80.078 79.376 79.538 79.376 79.25  79.628
 79.808 80.564 81.212 82.22  82.436 81.77  81.284 80.726 80.492 80.474
 80.258 80.528 80.672 80.744 81.194 82.436 82.76  82.85  82.49  81.554
 80.852 81.644 82.202 82.904 83.066 83.354 83.588 84.236 84.146 82.94
 81.518 80.438 79.988 79.556 79.556 79.61  80.06  80.906 81.824 83.138
 83.678 82.544 81.536 80.582 80.726 80.204 80.078 80.168 79.97  80.276
 81.302 82.544 82.832 82.472 81.572 81.284 80.942 81.446 82.058 82.13
 81.626 81.482 81.968 82.598 82.094 81.662 80.744 79.214 78.584 78.512
 78.098 78.188 78.26  78.638 80.042 81.248 81.59  81.122 80.33  79.952
 79.286 79.286 79.322 78.728 78.836 79.502 80.69  82.382 83.444 83.858
 83.93  83.732 83.912 84.326 84.398 84.002 84.074 83.822 83.534 83.498
 83.336 81.23  79.376 78.062 77.846 77.468 77.45  76.856 76.766 77.792
 79.412 80.078 80.222 79.772 79.034 78.152 78.134 77.864 77.234 76.766
 76.622 77.414 79.358 80.528 80.744 80.51  79.934 79.466 79.16  78.836
 78.656 78.242 78.476 79.052 80.276 81.212 81.68  81.572 81.05  80.24
 79.736 79.826 79.412 79.16  79.52  80.096 81.14  82.13  82.832 83.192
 82.454 81.896 82.058 82.328 82.688 82.346 80.888 81.32  81.788 81.842
 81.176 81.392 81.5   80.798 80.564 80.852 80.672 80.672 80.294 80.6
 81.338 82.202 82.382 82.13  81.986 81.68  81.536 81.392 81.248 81.212
 80.978 80.816 81.932 82.562 82.832 81.986 80.726 80.24  80.024 79.97
 78.998 78.224 78.152 78.962 79.844 81.518 82.13  81.914 81.14  80.888
 81.176 81.356 81.86  81.932 81.05  80.42  80.816 81.518 81.428 81.266
 80.06  79.034 78.098 77.594 77.306 77.036 76.766 77.18  78.962 80.312
 80.762 80.672 80.582 80.096 79.646 79.466 79.25  78.332 78.206 78.746
 79.88  81.464 82.382 82.472 81.986 81.356 81.32  81.698 82.67  82.994
 82.508 82.418 82.742 82.778 81.896 80.672 79.196 77.846 77.216 77.054
 77.108 77.    77.018 78.188 79.484 80.726 81.338 81.23  80.366 79.178
 78.656 78.206 77.936 77.99  78.224 78.98  80.042 81.194 81.716 81.95
 81.572 81.176 80.78  80.564 80.348 79.43  78.836 79.25  80.672 81.644
 81.554 81.14  80.42  79.646 79.754 79.736 79.808 79.574 78.908 79.07
 80.636 82.238 82.868 82.328 81.014 80.294 80.618 80.888 81.428 81.176
 80.708 80.924 81.986 83.354 83.93  84.02  83.75  83.804 84.056 84.326
 84.938 84.668 84.398 84.2   84.02  83.714 82.832 81.86  80.276 79.304
 79.07  78.764 78.71  78.98  79.016 80.024 81.194 82.472 82.94  82.508
 81.572 80.06  79.322 79.07  78.332 78.116 78.026 78.764 79.7   81.176
 81.932 81.986 81.356 80.51  80.942 81.716 81.698 81.482 80.96  81.464
 82.58  83.21  83.282 82.724 81.752 80.42  80.15  80.96  81.014 80.816
 80.888 80.834 81.986 82.742 81.788 81.302 80.582 79.286 78.602 77.828
 77.504 77.81  78.008 78.368 79.7   80.798 81.464 81.41  80.42  79.376
 79.088 78.404 78.368 77.972 78.098 78.584 79.394 80.096 80.294 80.564
 79.88  78.584 78.17  78.314 78.44  78.35  78.494 79.34  80.924 82.328
 83.102 83.408 82.958 82.76  82.976 83.192 83.696 83.534 83.048 82.904
 83.156 83.48  82.706 82.22  81.212 80.132 79.628 79.61  79.628 78.8
 78.476 79.412 81.158 81.842 82.058 81.788 80.87  79.682 79.268]
In [9]:
import math

print(np.sin(1))
y = np.array([2.0, 4.0, -1.0])
print(np.sin(y))
print(math.sin(y))
0.8414709848078965
[ 0.90929743 -0.7568025  -0.84147098]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [9], in <cell line: 6>()
      4 y = np.array([2.0, 4.0, -1.0])
      5 print(np.sin(y))
----> 6 print(math.sin(y))

TypeError: only size-1 arrays can be converted to Python scalars
In [10]:
x = np.array([1.1, 2.2, 3.0])
assert np.all(
    np.abs(x - np.array([1.1, 2.2, 3.3])) < 0.001
)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [10], in <cell line: 2>()
      1 x = np.array([1.1, 2.2, 3.0])
----> 2 assert np.all(
      3     np.abs(x - np.array([1.1, 2.2, 3.3])) < 0.001
      4 )

AssertionError: 
In [ ]:
import numpy.typing as npt

def f(arr: npt.NDArray[np.float64]) -> None:
    ...
In [16]:
print(np.zeros(4))
print(np.ones(4))
print(np.zeros(4, dtype=int))
print(np.ones(4, dtype=bool))
[0. 0. 0. 0.]
[1. 1. 1. 1.]
[0 0 0 0]
[ True  True  True  True]
In [19]:
print(np.linspace(0, 4, 5))
print(np.linspace(1.1, 2.2, 3))
print(np.linspace(0, 4, 5, endpoint=False))
[0. 1. 2. 3. 4.]
[1.1  1.65 2.2 ]
[0.  0.8 1.6 2.4 3.2]
In [26]:
import typing

def approxArea(
    f: typing.Callable, lowerbound: float, upperbound: float, bins: int
) -> float:
    xs = np.linspace(lowerbound, upperbound, bins, endpoint=False)
    ys = f(xs)
    #ys = xs ** 2 + 1
    areas = ys * (upperbound - lowerbound) / bins
    return np.sum(areas)
    
def foo(x: npt.NDArray) -> npt.NDArray:
    return x**2 + 1

print(approxArea(foo, 2, 4, 1000000))
print(approxArea(np.sin, 2, 4, 4))
20.66665466666801
0.6490531756499428
In [30]:
def stepFunction(x):
    if x < 0:
        return -1
    elif x < 2:
        return 0
    else:
        return 1
    
stepFunctionVectorized = np.vectorize(stepFunction)
print(stepFunctionVectorized(np.array([-1.1, 1.1, 2.2])))

print(approxArea(
    np.vectorize(stepFunction),
    -5, 5, 1000
))
[-1  0  1]
-2.0
In [35]:
board = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
print(board)
print(board * 2)
print(board[0][2])
print(board[0, 2])

print(board[:, 1:2])
print(board[1:, :])
print(board[1:, 1:2])
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[ 2  4  6]
 [ 8 10 12]
 [14 16 18]]
3
3
[[2]
 [5]
 [8]]
[[4 5 6]
 [7 8 9]]
[[5]
 [8]]
In [40]:
a = np.zeros((2, 3))
print(a.shape[1])
3
In [41]:
import csv
import matplotlib.pyplot as plt

xs = []
ys = []
with open("nino34.csv") as ifh:
    nino = csv.DictReader(ifh)
    for row in nino:
        xs.append(
            (int(row["YR"])-1950)*12 +
            (int(row["MON"])-1)
        )
        ys.append(float(row["TOTAL"]))
        
plt.plot(xs, ys)
plt.xlabel("Months since January 1950")
plt.ylabel("Temperature (c)")
plt.show()