3.14 Libraries A file that contains procedures that can be used in a program is called a library. An Application Program Interface, or API, contains specifications for how the procedures in a library behave and can be used. It allows the imported procedures from the library to interact with the rest of your code.

Types of Libraries There are many types of libraries that modern day companies use, such as Requests, Pillow, Pandas, NumPy, SciKit-Learn, TensorFlow, and Matplotlib. Requests: The Requests library simplifies making HTTP requests in Python, making it easy to interact with web services and retrieve data from websites.

Pandas: Pandas is a data manipulation and analysis library for efficiently working with structured data, including data import, cleaning, transformation, and analysis in tabular form.

Scikit-Learn: Scikit-Learn is a machine learning library that offers simple and efficient tools for data analysis and modeling, including classification, regression, and clustering.

TensorFlow: TensorFlow is an open-source machine learning framework that facilitates deep learning and neural network development, supporting tasks like image recognition and natural language processing.

Matplotlib: Matplotlib is a versatile plotting library for creating 2D and 3D visualizations and figures in Python, enabling a wide range of plots, charts, and graphs for data visualization and analysis.

Libraries are, at their heart, a collection of other people’s code, and it would be difficult to understand how the procedures should be used without documentation. APIs explain how two separate pieces of software interact with each other, and they also need documentation to keep this communication going.

Popcorn Hack 1 We have now hopefully all done or seen this type of question about getting a robot from point a to point b. But this can be very tedious because it may take a lot of code to do that. Luckily, there’s a saving grace called procedures, procedures essentially shorten the amount of code that is needed. So for a robot example we have displayed, we will use the procedure “moveBackwards” to shorten the amount of code we would normally need. This will rotate our triangle 180 degrees. Here is the example and solution

#Code goes here Pillow: Pillow is a powerful image processing library for opening, manipulating, and saving various image formats in Python.

from PIL import Image

Example usage:

image = Image.open(‘/home/lincolnc2008/vscode/student3/images/frog-game.jpg’)#This is the path of the image image.show() png

from PIL import Image:

This line imports the Image module from the PIL library. PIL stands for Python Imaging Library, which is used for opening, manipulating, and saving image files.

image = Image.open(‘/home/lincolnc2008/vscode/student3/images/frog-game.jpg’):

This line opens an image file named ‘/home/lincolnc2008/vscode/student3/images/frog-game.jpg’ using the Image.open() method. It creates an Image object and assigns it to the variable image.

image.show():

This line displays the image using the show() method. This will open the default image viewer on your system and display the image.

Popcorn Hack 2 Do this same thing but with a different image. Make it personallized!

Code goes here

NumPy: NumPy is a fundamental library for numerical computing in Python, providing support for multidimensional arrays and a wide range of mathematical functions.

import numpy as np

Example usage:

arr = np.array([1, 2, 3, 4, 5]) print(arr) [1 2 3 4 5] import numpy as np:

This line imports the Numpy library with the alias np. Numpy is a powerful library in Python for numerical computations, particularly with arrays and matrices.

arr = np.array([1, 2, 3, 4, 5]):

This line creates a Numpy array named arr using the np.array() function. The array is initialized with the values [1, 2, 3, 4, 5].

print(arr):

This line prints the array arr to the console

Popcorn Hack 3 You are given two 1-dimensional Numpy arrays, A and B, of the same length. Perform the following element-wise operations and return the results:

Add each element of array A to the corresponding element of array B. Multiply each element of array A by the corresponding element of array B. Square each element of array A.

Code goes here

import matplotlib.pyplot as plt

Example usage:

x = np.linspace(0, 10, 100) y = np.sin(x) plt.plot(x, y) plt.show() png

import matplotlib.pyplot as plt:

This line imports the pyplot module from the Matplotlib library with the alias plt. Matplotlib is a powerful library in Python for creating visualizations and plots.

x = np.linspace(0, 10, 100) y = np.sin(x):

These lines create two Numpy arrays x and y. x is generated using np.linspace() which creates an array of 100 equally spaced points between 0 and 10. y is generated by taking the sine of each element in x.

plt.plot(x, y):

This line creates a line plot using the plot() function from Matplotlib. It takes x and y as the data for the x and y coordinates of the plot.

plt.show():

This line displays the plot using the show() function. It opens a window with the generated plot.

Popcorn Hack 4 You have a dataset representing the monthly sales of a company over a year. The data is provided as two lists: months (containing the names of the months) and sales (containing the corresponding sales figures).

Your task is to create a bar chart to visualize the monthly sales.

Write a Python function that takes the lists months and sales as input and generates a bar chart using Matplotlib. The function should also label the x-axis with the months and the y-axis with “Sales (in thousands)”.

Code goes here

months = [‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’, ‘May’, ‘Jun’, ‘Jul’, ‘Aug’, ‘Sep’, ‘Oct’, ‘Nov’, ‘Dec’] sales = [150, 170, 190, 200, 220, 250, 280, 300, 280, 250, 230, 190] Optional Popcorn Hack Now for extra credit, create a program(s) using all the libraries we provided (Pillow, NumPy, TensorFlow, etc). Try showing a basic understanding of how to use each one of these libraries. Try doing something fun or creative!

Code goes here

Homework! For our homework hack, Write a Python program that uses the NumPy library to generate an array of values for the sine and cosine functions over a specified range, and then use Matplotlib to create a plot that shows both functions on the same graph. The program should: Import the necessary libraries (NumPy and Matplotlib). Define a range of angles in degrees, e.g., from 0 to 360 degrees. Use NumPy to calculate the sine and cosine values for each angle in the range. Create a Matplotlib plot with the angles on the x-axis and the sine and cosine values on the y-axis. Label the plot with appropriate titles, axis labels, and a legend. Display the plot on the screen.

import numpy as np

start_angle = 0
end_angle = 2 * np.pi  
step = 0.1  


angles = np.arange(start_angle, end_angle + step, step)

sine_values = np.sin(angles)
cosine_values = np.cos(angles)

print("Angles (radians):")
print(angles)
print("\nSine values:")
print(sine_values)
print("\nCosine values:")
print(cosine_values)

import matplotlib.pyplot as plt
import matplotlib.pyplot as plt

angles_degrees = np.arange(0, 361, 1)
angles_radians = np.radians(angles_degrees)
sine_values = np.sin(angles_radians)
cosine_values = np.cos(angles_radians)

plt.figure(figsize=(12, 7))
plt.plot(angles_degrees, sine_values, label='Sine', color='blue')
plt.plot(angles_degrees, cosine_values, label='Cosine', color='red')
plt.title('Sine and Cosine Functions')
plt.xlabel('Angle (degrees)')
plt.ylabel('Value')
plt.legend()
plt.grid()
plt.show()




Angles (radians):
[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7
 1.8 1.9 2.  2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.  3.1 3.2 3.3 3.4 3.5
 3.6 3.7 3.8 3.9 4.  4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.  5.1 5.2 5.3
 5.4 5.5 5.6 5.7 5.8 5.9 6.  6.1 6.2 6.3]

Sine values:
[ 0.          0.09983342  0.19866933  0.29552021  0.38941834  0.47942554
  0.56464247  0.64421769  0.71735609  0.78332691  0.84147098  0.89120736
  0.93203909  0.96355819  0.98544973  0.99749499  0.9995736   0.99166481
  0.97384763  0.94630009  0.90929743  0.86320937  0.8084964   0.74570521
  0.67546318  0.59847214  0.51550137  0.42737988  0.33498815  0.23924933
  0.14112001  0.04158066 -0.05837414 -0.15774569 -0.2555411  -0.35078323
 -0.44252044 -0.52983614 -0.61185789 -0.68776616 -0.7568025  -0.81827711
 -0.87157577 -0.91616594 -0.95160207 -0.97753012 -0.993691   -0.99992326
 -0.99616461 -0.98245261 -0.95892427 -0.92581468 -0.88345466 -0.83226744
 -0.77276449 -0.70554033 -0.63126664 -0.55068554 -0.46460218 -0.37387666
 -0.2794155  -0.1821625  -0.0830894   0.0168139 ]

Cosine values:
[ 1.          0.99500417  0.98006658  0.95533649  0.92106099  0.87758256
  0.82533561  0.76484219  0.69670671  0.62160997  0.54030231  0.45359612
  0.36235775  0.26749883  0.16996714  0.0707372  -0.02919952 -0.12884449
 -0.22720209 -0.32328957 -0.41614684 -0.5048461  -0.58850112 -0.66627602
 -0.73739372 -0.80114362 -0.85688875 -0.90407214 -0.94222234 -0.97095817
 -0.9899925  -0.99913515 -0.99829478 -0.98747977 -0.96679819 -0.93645669
 -0.89675842 -0.84810003 -0.79096771 -0.7259323  -0.65364362 -0.57482395
 -0.49026082 -0.40079917 -0.30733287 -0.2107958  -0.11215253 -0.01238866
  0.08749898  0.18651237  0.28366219  0.37797774  0.46851667  0.55437434
  0.63469288  0.70866977  0.77556588  0.83471278  0.88551952  0.92747843
  0.96017029  0.98326844  0.9965421   0.99985864]

png