What is a Library?
Essentially a list of pre-written code that you can use to streamline and clean up your program.
Libraries can help simplify complex programs
APIS are specifications for how the procedures in a library behave, and how they can be used
Documentations for an API/library is necessary in understanding the behaviors provided by the API/library and how to use them
Libraries that we will go over: Requests, Pillow, Pandas, Numpy, Scikit-Learn, TensorFlow, matplotlib.
Required Installations
Please run the following commands in your vscode terminal in order to continue the lesson
- pip install numpy
- pip install matplotlib
- pip install scikit-learn
- pip install pillow
- pip install pandas
- pip install tensorflow
- pip install requests
Images using requests and pillow libraries
‘Requests’ is focused on handling HTTP requests and web data while ‘Pillow’ is designed for data manipulation and analysis It’s common to see them used together in data-related assignments where data is fetched by HTTP requests using Requests and then processed and analyzed with Pandas.
Here’s an example:
import requests
from PIL import Image
from io import BytesIO
# Step 1: Download an image using Requests
image_url = "https://example.com/path/to/your/image.jpg" # Replace with the actual URL of the image you want to download
response = requests.get(image_url)
if response.status_code == 200:
# Step 2: Process the downloaded image using Pillow
image_data = BytesIO(response.content) # Create an in-memory binary stream from the response content
img = Image.open(image_data) # Open the image using Pillow
# Perform image processing tasks here, like resizing or applying filters
img = img.resize((x, y)) # Resize the image and replace x,y with desired amounts
# Step 3: Save the processed image using Pillow
img.save("processed_image.jpg") # Save the processed image to a file
print("Image downloaded, processed, and saved.")
else:
print(f"Failed to download image. Status code: {response.status_code}")
In this code, we use the Requests library to download an image from a URL and then if the download is successful the HTTP status code 200 will pop up, and from there we create an in-memory binary stream (BytesIO) from the response content. We then use the Pillow library to open the image, make any necessary changes, and save the processed image to a file.
Here’s a step by step tutorial on how we wrote this code: 1)We started by importing the necessary libraries, which were Requests, Pillow, and io.
2)Download the Image
3)Use the Requests library to send an HTTP GET request to the URL to download the image. Check the response status code to make sure the download goes through(status code 200).
4)If the download is successful, create an in-memory binary stream (BytesIO) from the response content. Process the Image:
5)Utilize the Pillow library to open the image from the binary stream. Change photo to desired preference(ie: size) Save the Processed Image:
6)Save the processed image to a file using Pillow. Choose a filename and file format for the saved image.
Hack 1
Write a Python code that accomplishes the following tasks:
Downloads an image from a specified URL using the Requests library. Processes the downloaded image (like resizing) using the Pillow library. Save the processed image to a file.
#Code here
Math Operations With Python Libraries
Numpy(Numerical Python) is used for numerical and scientific computing. It provides tools for handling large sets of numbers, such as data tables and arrays. Numpy makes it easier and more efficient to do mathematical tasks.
The Matplotlib library lets you create a visual representation of your data (graphs, charts, and etc.)
Example Sine Graph
Uses numpy and matplotlib libaries
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data with NumPy
x = np.linspace(0, 2 * np.pi, 100)
# Create an array of values from 0 to 2*pi
# 100 is included to have 100 points distributed between 0 and 2π to make graph smoother
y = np.sin(x)
# Compute the sine of each value
# Create a simple line plot using Matplotlib
plt.plot(x, y, label='Sine Function', color='blue', linestyle='-') # Create the plot
plt.title('Sine Function') # Set the title
plt.xlabel('x') # Label for the x-axis
plt.ylabel('sin(x)') # Label for the y-axis
plt.grid(True) # Display a grid
plt.legend() # Show the legend
plt.show() # Display the plot
Hack 2
Using the data from the numpy library, create a visual graph using different matplotlib functions.
import numpy as np
import matplotlib.pyplot as plt
# Generate data for two lines
x = np.linspace(0, 10, 50) # Create an array of values from 0 to 10
y1 = 2 * x + 1 # Set of data poits
# Create and display a plot using Matplotlib
# your code here
Tensor Flow is used in deep learning and neural networks, while scikit-learn is used for typical machine learning tasks. When used together, they can tackle machine learning projects. In the code below, Tensor Flow is used for model creation and training. Scikit-learn is used for data-processing and model evaluation.
Pip install tensorflow scikit-learn
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from tensorflow import keras
from tensorflow.keras import layers
# Generate synthetic data
np.random.seed(0)
X = np.random.rand(100, 1) # Feature
y = 2 * X + 1 + 0.1 * np.random.randn(100, 1) # Target variable with noise
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Create a simple linear regression model using TensorFlow and Keras
model = keras.Sequential([
layers.Input(shape=(1,)),
layers.Dense(1)
])
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
# Train the model
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=2)
# Make predictions on the test set
y_pred = model.predict(X_test)
# Calculate the Mean Squared Error on the test set
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse:.4f}")
A decrease in loss and time metrics (ms/epoch and ms/step) shows the efficiency increases as the training epochs increases
Hack
fill in the missing code to match the custom data set
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from tensorflow import keras
from tensorflow.keras import layers
# Generate a custom dataset (replace this with your data loading code)
# Synthetic data: House prices based on number of bedrooms and square footage
np.random.seed(0)
num_samples = 100
bedrooms = np.random.randint(1, 5, num_samples)
square_footage = np.random.randint(1000, 2500, num_samples)
house_prices = 100000 + 50000 * bedrooms + 100 * square_footage + 10000 * np.random.randn(num_samples)
# Combine features (bedrooms and square footage) into one array
X = np.column_stack((bedrooms, square_footage))
y = house_prices.reshape(-1, 1)
# Split the data into training and testing sets
# Standardize the features
# Create a regression model using TensorFlow and Keras
# Input shape adjusted to the number of features
# Output layer for regression
# Compile the model for regression
# Using MSE as the loss function
# Train the model
# Make predictions on the test set
# Calculate the Mean Squared Error on the test set
HOMEWORK 1
Create a GPA calculator using Pandas and Matplot libraries and make: 1) A dataframe 2) A specified dictionary 3) and a print function that outputs the final GPA
Extra points can be earned with creativity.
import pandas as pd
import matplotlib.pyplot as plt
# Function to calculate GPA
def calculate_gpa(grades):
grade_points = {'A+': 4.0, 'A': 4.0, 'A-': 3.7, 'B+': 3.3, 'B': 3.0, 'B-': 2.7, 'C+': 2.3, 'C': 2.0, 'C-': 1.7, 'D+': 1.3, 'D': 1.0, 'F': 0.0}
total_credits = 0
weighted_points = 0
for subject, credit, grade in grades:
if grade in grade_points:
total_credits += credit
weighted_points += credit * grade_points[grade]
if total_credits == 0:
return 0.0
else:
return weighted_points / total_credits
# Create a dataframe
data = {'Subject': ['Math', 'Science', 'History', 'English', 'Art'],
'Credits': [3, 4, 3, 3, 2],
'Grade': ['A', 'B+', 'A-', 'B', 'C+']}
df = pd.DataFrame(data)
# Print the dataframe
print("Course Information:")
print(df)
# Calculate GPA
grades = [(row.Subject, row.Credits, row.Grade) for index, row in df.iterrows()]
gpa = calculate_gpa(grades)
# Print GPA
print("\nCalculated GPA: {:.2f}".format(gpa))
# Bar chart to visualize grades
df['Grade Points'] = df['Grade'].map({'A+': 4.0, 'A': 4.0, 'A-': 3.7, 'B+': 3.3, 'B': 3.0, 'B-': 2.7, 'C+': 2.3, 'C': 2.0, 'C-': 1.7, 'D+': 1.3, 'D': 1.0, 'F': 0.0})
df.plot(x='Subject', y='Grade Points', kind='bar', color='teal', legend=False)
plt.ylabel('Grade Points')
plt.title('Grades')
plt.ylim(0, 4.5)
plt.xticks(rotation=45)
plt.show()
Fontconfig warning: ignoring UTF-8: not a valid region tag
Course Information:
Subject Credits Grade
0 Math 3 A
1 Science 4 B+
2 History 3 A-
3 English 3 B
4 Art 2 C+
Calculated GPA: 3.33
HOMEWORK 2
Import and use the “random” library to generate 50 different points from the range 0-100, then display the randomized data using a scatter plot.
Extra points can be earned with creativity.
# your code here
import random
import matplotlib.pyplot as plt
# Generate 50 random points in the range 0-100
x = [random.randint(0, 100) for _ in range(50)]
y = [random.randint(0, 100) for _ in range(50)]
# Create a scatter plot
plt.scatter(x, y, color='blue', label='Random Points')
# Add labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Random Scatter Plot')
# Display the plot
plt.legend()
plt.grid(True)
plt.show()