<jupyter_start><jupyter_text>Jupyter Notebook TutorialJupyter notebooks are great for writing Python interactively and visualizing results. In addition to Python, jupyter notebooks can render markdown (even with LaTeX! $f'(x) = 2x$).To execute Python, write something in a code "cell" and press shift-enter:<jupyter_code>2 + 2<jupyter_output><empty_output><jupyter_text>The notebook will automatically display the string representation of the last value entered:<jupyter_code>a = 2
b = a * 3
a**2 - b<jupyter_output><empty_output><jupyter_text>Creating PlotsJupyter notebooks are so cool because you can display plots created with code inline. The most popular library for plotting in library is matplotlib.To get started with matplotlib, you need to import it:<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>It's also useful to import numpy for working with arrays:<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Here's a basic example that graphs points along a sine wave:<jupyter_code>x = np.arange(0, 2*np.pi, 0.1) # create an array of numbers from 0 to 2pi, 0.1 interval
y = np.sin(x) # apply sin function to each x value

plt.title("Sine Wave")
plt.xlabel("x")
plt.ylabel("y")
plt.plot(x, y)<jupyter_output><empty_output><jupyter_text>More plottingHere's an example that shows the histograms of some random distributions:<jupyter_code># plot uniform (blue), triangular (green), and almost normal (red) distributions
for times in [1, 2, 12]:
    vals = np.random.rand(1000)
    for i in range(1, times):
        vals += np.random.rand(1000)
    vals /= times
    plt.hist(vals)<jupyter_output><empty_output><jupyter_text>Even more plottingYou can even do 3D stuff. This is the graph of $z = \sin \sqrt{x^2 + y^2}$<jupyter_code>from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')

X = np.linspace(-10, 10, 300)
Y = np.linspace(-10, 10, 300)
X_grid, Y_grid = np.meshgrid(X, Y)
Z = np.sin(np.sqrt(X_grid**2 + Y_grid**2))
ax.plot_surface(X_grid, Y_grid, Z, cmap='rainbow')
ax.set_zlim(-2, 2)
plt.title('z = sin(sqrt(x^2 + y^2))')<jupyter_output><empty_output><jupyter_text>OpenCV with jupyter notebooksUsing OpenCV in jupyter notebooks is a great way to quickly prototype and experiment with computer vision. Because OpenCV seamlessly integrates with matplotlib and numpy, it's simple to use in Python:<jupyter_code>import cv2 # import opencv library

# load image as a numpy array
img = cv2.imread("img/flower.jpg", cv2.IMREAD_COLOR)
img.shape<jupyter_output><empty_output><jupyter_text>The OpenCV Python bindings automatically load images as numpy arrays. The shape of the image loaded above is `(426, 640, 3)`, which means that the image is 640x426 and has 3 color channels.To display the image, use `plt.imshow`. (Note that OpenCV loads images in BGR format. Matplotlib expects RGB images so conversion is needed. If `cvtColor` is too much typing, try `img[:,:,::-1]` to reverse the channels)<jupyter_code>plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))<jupyter_output><empty_output><jupyter_text>It's easy to manipulate images now. Here's an example of copying a patch of the image to another location:<jupyter_code>manipulated = img.copy()
manipulated[200:400,300:500,:] = manipulated[50:250,100:300,:]
plt.imshow(cv2.cvtColor(manipulated, cv2.COLOR_BGR2RGB))<jupyter_output><empty_output><jupyter_text>Here's an example applying box blur to an image:<jupyter_code>kernel = np.ones((15, 15)) / 15**2 # create a 15x15 kernel whose elements are equal and sum to 1
blurred = cv2.filter2D(img, -1, kernel)
plt.imshow(cv2.cvtColor(blurred, cv2.COLOR_BGR2RGB))<jupyter_output><empty_output><jupyter_text>Here's how one could create a swirl effect. Use `alpha` to change the intensity and direction<jupyter_code>alpha = -0.018

y_axis, x_axis = np.arange(0, img.shape[0]), np.arange(0, img.shape[1])
# shift into coordinates where (0, 0) is center of image
y_axis, x_axis = y_axis - img.shape[0]/2, x_axis - img.shape[1]/2
grid_x, grid_y = np.meshgrid(x_axis, y_axis)
dist = np.sqrt(grid_x**2 + grid_y**2)
# don't swirl entire image
dist = np.clip(dist.max()/2 - dist, 0, 10000)
# apply rotation matrix where angle is dist*alpha
map_x = grid_x*np.cos(dist*alpha) - grid_y*np.sin(dist*alpha)
map_y = grid_x*np.sin(dist*alpha) + grid_y*np.cos(dist*alpha)

# shift back into opencv coordinates
map_y, map_x = map_y + img.shape[0]/2, map_x + img.shape[1]/2

swirl = cv2.remap(img, map_x.astype(np.float32), map_y.astype(np.float32), interpolation=cv2.INTER_CUBIC)
plt.imshow(cv2.cvtColor(swirl, cv2.COLOR_BGR2RGB))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11508786969567081, 'fraction_numerical': 0.033004714959279896, 'mean_word_length': 5.955290611028316, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '553384', 'license': 'MIT', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1667, 'n_words': 482, 'path': 'tutorial.ipynb', 'repo_name': 'techid2000/quiz1-aj.yam', 'size': '4666'}