<jupyter_start><jupyter_text>04_04: Math with NumPy Arrays<jupyter_code>import math
import collections

import numpy as np
import pandas as pd
import matplotlib.pyplot as pp

%matplotlib inline
x = np.linspace(0,5*math.pi,64)
x
math.sin(x)
sinx = np.sin(x)
sinx
pp.plot(x, sinx)
# multiple matplotlib.pyplot.plot() instructions superimpose lines
# in the same plot, cycling colors so we can distinguish them

pp.plot(x, sinx)
pp.plot(x, np.cos(x))
pp.plot(x, np.log(1 + x))
# plot labels can be shown with matplotlib.pyplot.legend()

pp.plot(x, sinx, label='sin(x)')
pp.plot(x, np.cos(x), label='cos(x)')
pp.plot(x, np.log(1 + x), label='log(1+x)')

pp.legend()
cosx = np.cos(x)
y = sinx * cosx
z = cosx**2 - sinx**2

pp.plot(x, y)
pp.plot(x, z)
x + y[16:32]
w = sinx + 1.5
sinx[:10]
w[:10]
pp.plot(x, sinx)
pp.plot(x, w)
monalisa_bw = np.loadtxt('monalisa.txt')
monalisa_bw.shape
xgrad = np.linspace(0, 1, 134)
monalisa_xgrad = monalisa_bw * xgrad
# set nominal figure size in inches; show images in two adjacent subplots

pp.figure(figsize=(8,5))
pp.subplot(1,2,1); pp.imshow(monalisa_bw, cmap='gray')
pp.subplot(1,2,2); pp.imshow(monalisa_xgrad, cmap='gray')
ygrad = np.linspace(0, 1, 200)
ygrad * monalisa_bw
ygrad = ygrad[:, np.newaxis]
ygrad.shape
monalisa_ygrad = monalisa_bw * ygrad
pp.figure(figsize=(8,5))
pp.subplot(1,2,1); pp.imshow(monalisa_bw, cmap='gray')
pp.subplot(1,2,2); pp.imshow(monalisa_ygrad, cmap='gray')
a = np.array([0,1,2])
b = np.array([-1,-2,-3])
a @ b
np.dot(a, b)
C = np.random.randn(3, 3)
C
C @ a<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17715019255455713, 'fraction_numerical': 0.036585365853658534, 'mean_word_length': 5.286290322580645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '530669', 'license': 'CC0-1.0', 'n_tokens_mistral': 733, 'n_tokens_neox': 713, 'n_words': 158, 'path': 'chapter4/04_04_math.ipynb', 'repo_name': 'yoji-toriumi/Python_Data_Analysis', 'size': '1558'}