0% found this document useful (0 votes)
14 views

Tutorial 1

Uploaded by

sgdenimhouse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Tutorial 1

Uploaded by

sgdenimhouse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Machine Learning

Tutorial 1

Exercise 1: Creating Arrays

• Create a NumPy array called arr1 with values from 1 to 10.


• Create another NumPy array called arr2 with 5 zeros.
• Create a 3x3 identity matrix called identity_matrix.

Exercise 2: Array Operations

• Perform element-wise multiplication of arr1 and arr2 and store the result in a
new array.
• Calculate the square root of all elements in arr1 and store the result in a new
array.
• Add 5 to each element in arr2 in place (modify arr2).

Exercise 3: Array Indexing and Slicing

• Create a NumPy array called arr3 with values from 0 to 15.


• Print the element at index 5 of arr3.
• Print elements from index 2 to 7 (inclusive) of arr3.
• Create a 2D array called matrix with the following values:

• Print the element at row 2, column 1 (indexing starts from 0).


• Slice the subarray consisting of the first two rows and the last two columns.

Exercise 4: Aggregation Functions

• Calculate the sum of all elements in arr1.


• Find the maximum value in arr2.
• Calculate the mean (average) of all elements in arr3.

Exercise 5: Array Manipulation

• Reshape arr3 into a 3x5 array.


• Stack arr1 and arr2 vertically to create a new array.
• Create a copy of arr1 and call it arr1_copy. Modify an element in arr1_copy and
check if it affects arr1.

1
Machine Learning
Exercise 6: Statistical Analysis

Create a NumPy array containing 100 random values sampled from a normal
distribution with mean 5 and standard deviation 2. Perform the following statistical
analysis:

• Calculate the mean, median, and variance of the array.


• Count how many values are greater than 7.
• Find the minimum and maximum values in the array.

Exercise 7: Broadcasting

• Create a NumPy array array_a of shape (3, 3) and another array array_b of
shape (3, 1). Use broadcasting to add array_b to each column of array_a.

*************************************************************************************

Exercise 1: Subplots

• Create a figure with two subplots: one for a line plot and another for a scatter
plot.
• Use different datasets for each subplot and customize their appearance.
• Add labels, titles, and legends for each subplot.

Exercise 2: Advanced Customization

• Choose any dataset or data of your choice and create an advanced


visualization. This could be a 3D plot, a stacked bar chart, or any other type of
plot that requires specific customization.
• Experiment with colors, styles, and annotations to make the plot informative
and visually appealing.

Exercise 3: Saving Plots

• After creating any of the above plots, save it as an image file (e.g., PNG or
PDF) using Matplotlib's savefig function.
• Specify the file name, resolution, and other options as needed.

2
Machine Learning
Exercice 4 :

Create this figure

*************************************************************************************

Problem: Visualizing Temperature Data

You are given a dataset containing daily temperature readings for a city over a
month. Your task is to create a line plot to visualize the temperature changes
throughout the month.

Dataset:

• Date (1st to 30th of the month)


• Maximum Temperature (in degrees Celsius)

dates = [1, 2, 3, ..., 30] # Dates from 1 to 30

temperatures = [28, 29, 30, ..., 32] # Maximum temperatures for each date

Requirements:

1. Create a line plot using Matplotlib to show the maximum temperatures over
the course of the month. The x-axis should represent the dates, and the y-axis
should represent the temperatures.
2. Customize the appearance of the plot:
• Use a blue line for the temperature curve.
• Add labels for the x-axis ("Date") and y-axis ("Temperature (°C)").
• Set a title for the plot ("Monthly Temperature Variation").

3
Machine Learning
• Add grid lines to the plot.
3. Highlight specific points on the plot:
• Mark the highest temperature point with a red circle marker.
• Mark the lowest temperature point with a green triangle marker.
4. Include a legend indicating the meaning of the markers.
5. Save the plot as an image file named "temperature_variation.png."

Optional :

Add a second line to the plot to represent the minimum daily temperatures for each
date in a different color (e.g., red). Adjust the legend accordingly.

Problem: Analyzing and Visualizing Sales Data

You are given a CSV file ("sales_data.csv") containing sales data for a retail store. The
data includes information about products, sales quantities, prices, and dates.

Tasks:

1. Use Pandas to read the CSV file into a DataFrame.


2. Perform basic data exploration:
• Display the first 5 rows of the DataFrame.
• Check for missing values in the dataset and handle them appropriately
(e.g., by filling or dropping).
3. Use NumPy to calculate the total sales revenue for each product by
multiplying the quantity sold by the price per unit.
4. Create a new column in the DataFrame to represent the month and year for
each sale (e.g., "Jan 2023").
5. Use Pandas to group the data by month and year and calculate the total sales
revenue for each month.
6. Create a line plot using Matplotlib to visualize the monthly sales trend over
time. The x-axis should represent the months, and the y-axis should represent
the total sales revenue.
7. Customize the appearance of the plot by adding labels, a title, and grid lines.
8. Save the plot as an image file (e.g., "sales_trend.png").

Optional :

9. Calculate additional insights, such as the best-selling product or the month


with the highest sales.
10. Create a bar chart or pie chart to visualize the distribution of sales by product
category.

Problem: Analyzing and Visualizing Stock Prices

4
Machine Learning
In this exercise, you will work with historical stock price data, perform data analysis
using Pandas and NumPy, and create visualizations using Matplotlib.

Dataset:

You are provided with a CSV file named "stock_prices.csv" containing historical
stock price data for a specific company. The dataset has the following columns:

• Date (in YYYY-MM-DD format)


• Open Price (opening price of the stock)
• Close Price (closing price of the stock)
• High Price (highest price during the trading day)
• Low Price (lowest price during the trading day)
• Volume (number of shares traded)

Tasks:

1. Read the "stock_prices.csv" file into a Pandas DataFrame.


2. Calculate the daily returns for the stock. Daily return is defined as:
Daily Return=Close Price−Open PriceOpen PriceDaily Return=Open PriceClo
se Price−Open Price
3. Calculate the 30-day rolling average of the stock's closing price and add it as a
new column in the DataFrame.
4. Calculate the 30-day rolling standard deviation of the stock's closing price and
add it as a new column.
5. Create a line plot using Matplotlib to visualize the stock's closing price over
time. Include labels and a title.
6. Create a histogram of the daily returns to visualize their distribution. Add
labels and a title.
7. Create a scatter plot to visualize the relationship between the stock's volume
and daily returns. Add labels and a title.
8. Export the DataFrame with the calculated values to a new CSV file named
"stock_analysis.csv."

Optional:

9. Perform additional analysis, such as calculating moving averages, trading


signals, or identifying days with significant price changes.

You might also like