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

Assignment 5 - Copy (4)

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

Assignment 5 - Copy (4)

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

K. J.

Somaiya College of Engineering, Mumbai-77


(Somaiya Vidyavihar University)

MODULE 5
5.1 Introduction to Packages, Installation, and Use
5.2 Introduction to Numpy, ndarray, data types, shape, reshape, iterating, join, split, search, sort,
filter, slice, Mathematical and string functions
5.3 Introduction to Python Matplotlib, Markers, line, labels, grid, subplot, scatterplot, histogram, bar
chart, pie charts
Q No Question

1 Consider two lists representing the x and y coordinates of 50 random data points. Write a Python
program for the following tasks:
1. Create a scatter plot of the data points.
2. Calculate and display the average value of the x-coordinates and the y-coordinates on the
scatter plot.

2 Analyze temperature data from a weather station for seven days by generating a NumPy array with
random temperature readings every minute (24 per day), calculate the average daily temperatures
and identify the maximum and minimum readings over the week, then use Matplotlib to visualize
the daily averages in a line chart with markers and grid, show the frequency distribution of all
readings in a histogram, create a pie chart categorizing the readings into Low (15-22°C), Moderate
(22-28°C), and High (28-35°C) ranges

3 Consider sales data for each month in a year, stored in two lists (months and sales). Write a Python
program that:
1. Creates a bar chart to represent monthly sales.
2. Highlights the month with the highest sales.
3. Adds labels, a title, and appropriate colors to make the chart visually appealing.

4 Implement a program that searches for a specific element in a NumPy array and returns its index
and Create a program that generates a random NumPy array and sorts it in ascending and
descending order

5 You are a data analyst working for a retail company. The company wants to visualize its sales data
to understand trends and patterns. You are tasked with creating visual representations of this data
using Matplotlib.
Write a Python program that
1. Create a line chart to show the trend of sales over the months,
2. Add markers, a grid, labels for the x and y axes, and a title to the chart.
['Jan', 'Feb', 'Mar',’Apr’, ‘May’,’Jun’,’Jul’,’Aug’,’Sep’, ‘Oct’,’Nov’,‘Dec’]
[12000, 15000, 13000, 17000, 14000, 17000, 18000, 17000, 17700, 18900, 20000, 21000]

6 Create an array in the range 1 to 20 with values 1.25 apart. Another array contains the log values of
the elements in the first array.
K. J. Somaiya College of Engineering, Mumbai-77
(Somaiya Vidyavihar University)

a) Create a plot of first vs second array: specify the x-axis(containg first array’s values) title as
‘Random values’ and y-axis title as ‘Logarithm Values’.
b) Create a third array tat stores the cos value of first array and then plot both the second and third
arrays vs first array. The cos value should be plotted with a dashdotted line.
c) Create scatterchart as this: second array data points as blue small diamonds, third array data
points as black circles.

7 Create a simple Python package named mypackage with a module math_operations that contains a
function add(a, b) that returns the sum of two numbers. Write a script that imports this function
and uses it to add two numbers.

8 Create a subplot with a bar chart on the left and a pie chart on the right. The bar chart should
display the number of sales of five products, and the pie chart should represent the market share of
those products as a percentage.
Products: ["A", "B", "C", "D", "E"]
Sales: [15, 30, 25, 10, 20]

9 Create a 2x2 grid of subplots in Matplotlib, with each subplot displaying a different type of chart
(line chart, scatter plot, histogram, and bar chart). Ensure each subplot has its own title.
Data Set:
Line chart and Scatter plot:
x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11], y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78]

Histogram Data: data = [22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27] (create a histogram
with bins set to 5).

Bar Chart Data: categories = ['A', 'B', 'C', 'D'], values = [5, 7, 3, 8]

10 Write a Python program that performs the following tasks:


1. Generate Data:
○ Use NumPy to create a range of values (x) from 0 to 2π (approximately 6.283) with
an interval of 0.1.
○ Compute the sine and cosine values for each x value and store them in separate
arrays.
2. Plotting:
○ Use Matplotlib to create a line plot that displays both the sine and cosine functions
on the same graph.
○ Label the x-axis as "X values (radians)", the y-axis as "Y values", and give the plot
a title "Sine and Cosine Waves"

11 Create two subplots that share the same x-axis. The first subplot should be a bar chart showing the
average monthly temperatures, and the second subplot should be a line chart showing monthly
rainfall. Label each subplot appropriately.
K. J. Somaiya College of Engineering, Mumbai-77
(Somaiya Vidyavihar University)

Months: [1, 2, 3, 4, 5, 6]
Average Temperatures (°C): [5, 7, 12, 15, 18, 20]
Rainfall (mm): [50, 45, 60, 55, 70, 65]

12 a)WAP to create a 2D NumPy array of shape (4, 5) containing integers from 1 to 20. Reshape this
array into a new shape of (5, 4). Print the original and reshaped arrays.
b)WAP to create a NumPy array with random integers between 1 and 100. Sort the array in
ascending order and then slice the array to get the top 5 largest elements. Print the sorted and sliced
array.

13 Write a Python program to create bar plots with error bars on the same figure.
Sample Date
Mean velocity: 0.2474, 0.1235, 0.1737, 0.1824
Standard deviation of velocity: 0.3314, 0.2278, 0.2836, 0.2645
The code snippet gives the output shown in the following screenshot:

14 300 children were asked to choose their favorite ice cream flavor.
WAP to show this data on a Pie chart with the percentage of children choosing different ice cream
flavors

Flavor Frequency
Strawberry 44
Vanilla 76
Chocolate 30
Butterscotch 78
Raspberry 39
Mint 11
Blueberry 22

15 Create an array of prime numbers between 2 and 1000. Create another array of prime numbers
between 2000 and 4000.
Truncate the larger array to make it the same size as the smaller array. Then find the correlation
K. J. Somaiya College of Engineering, Mumbai-77
(Somaiya Vidyavihar University)

between the two arrays.

16 Write a Python program to create a NumPy array that contains the names of five students.
1. Create another NumPy array for their corresponding scores in Mathematics. Print both
arrays.
2. Calculate the average score of the students in Mathematics. Print the average score.
3. Find out student who scored above a specified 75 in Mathematics using boolean indexing.
4. Sort the scores in descending order. Display the sorted scores along with the corresponding
student
5. Finds and prints the highest and lowest scores in the scores array along with the names of
the students

17 Write a Python program to display the grid and draw line charts of the closing value of Alphabet
Inc. between October 3, 2016 to October 7, 2016. Customize the grid lines with rendering with a
larger grid (major grid) and a smaller grid (minor grid).Turn on the grid but turn off ticks.

18 Write a Python program to add and subtract two numpy arrays.


For the given sample numpy array retrieve and display only those elements whose value is
between 4 to 9(excluding 4 and 9)
Sample array: [2, 4, 6, 8, 10], [1, 3, 5, 7, 9]

19 Write a program to create two NumPy arrays of shape (3, 3) with random integers between 1 and
10. Perform element-wise addition, subtraction, multiplication, and division between these arrays
and print the results for each operation.

20 Write one Python program to find the following from the given dataframe DF:

Rollno Name Age Marks

S1001 Arun 18 68

S1002 Mohit 14 47

S1003 Karan 13 78

S1004 Lalit 16 87

S1005 Ravi 14 60

a) Maximum marks and minimum marks


b) sum of all the marks
c) Mean and mode of age of the students
d) Count the no of rows present in the dataframe
K. J. Somaiya College of Engineering, Mumbai-77
(Somaiya Vidyavihar University)

21 Demonstrate your understanding of NumPy operations including array creation, statistical analysis,
reshaping, filtering, and mathematical operations.

22 Create a Python program using Matplotlib to generate a Bar chart displaying the heatwave during
the summer season (March to May) of a city for 5 years.

23 Read the company_sales_data file using Pandas or NumPy or using in-built matplotlib
function.Calculate total sale data for last year for each product and show it using a Pie chart.

24 Create two single dimensional NumPy arrays, one is height, and another is weight, calculate BMI
(weight/height**2) and keep all BMI values in another NumPy Array.
Create a scatter plot height and BMI, line plot of Height and weight, Bar plot of weight and BMI.
Use proper formatting of x-label, y-label, title, color, and grid.

25 Create a filter array that will return only even elements from the original array

26 Total profit data provided for each month.

Month Total Profit


Number

1 INR 1200

4 INR 200

7 INR 800

10 INR 1500

12 INR 1700
Generate line plot with following properties:
● X label name = Month Number
● Y label name = Total profit in INR
● Add a circle marker
● Line marker color as red
27 Write a Python program to display a bar chart of the popularity of programming
Languages. Use a different color for each bar.
K. J. Somaiya College of Engineering, Mumbai-77
(Somaiya Vidyavihar University)

Sample data:
Programming languages: Java, Python, PHP, JavaScript, C#, C++
Popularity: 22.2, 17.6, 8.8, 8, 7.7, 6.7

28 Create a 6x6 matrix with random integers between 10 and 50.


Replace all even numbers in the matrix with -1.

29 Write a Python program using NumPy to create a 3x3 matrix filled with random integers between
1 and 100. Calculate the matrix's determinant, transpose, and inverse (if it exists). Display the
original matrix and the results of these operations. Handle any exceptions that may occur.

30 Create a Python program using Matplotlib to generate a line chart displaying a city's monthly
temperatures (in °C) for one year. Include markers, gridlines, labeled axes, a title, and a legend.
Additionally, create a subplot with a bar chart showing the average temperature for each season.

months = ["January", "February", "March", "April", "May", "June",


"July", "August", "September", "October", "November", "December"]

temperatures = [5, 7, 10, 15, 20, 25, 30, 29, 22, 16, 10, 6]
(Hint: Winter (Dec, Jan, Feb), Spring (Mar, Apr, May) Summer (Jun, Jul, Aug) Autumn
(Sep, Oct, Nov) )

31 Create a Python program using Matplotlib to generate a scatter plot showing the relationship
between study hours and exam scores for a group of students. Include labeled axes, a title, and a
trendline. Additionally, create a histogram displaying the distribution of exam scores.
study_hours = [2, 3, 4.5, 1, 6, 7.5, 8, 5, 9, 2.5, 3.5, 7, 6.5, 4, 8.5]
exam_scores = [55, 60, 65, 50, 75, 85, 90, 70, 95, 58, 63, 88, 80, 68, 92]

32 Write a Python program using Matplotlib to create a pie chart that shows the distribution of five
different categories with the following percentages: [15, 25, 35, 10, 15]. Label each segment with
its percentage.
Expected Output: A pie chart with labeled segments

33 Write a Python program using Matplotlib to create a line plot of the function y=x^2 for x values
ranging from -10 to 10. Label the x-axis as "X Values", the y-axis as "Y Values", and give the plot
a title.
Expected Output: A line plot of y = x^2 with appropriate labels and title.

34 Create a 2D NumPy array where each row represents a student and columns represent different
subjects (e.g., Mathematics, Science, and English). Populate the array with random scores and
print the complete dataset.
K. J. Somaiya College of Engineering, Mumbai-77
(Somaiya Vidyavihar University)

35 The data given below is the market share of different smartphone brands as of Q2 2024
across the world.
Brand Percentage /Share
Samsung: 18.4%
Apple: 15.6%
Xiaomi: 14.5%
vivo: 8.8%

OPPO: 8.8%
Others: 33.8%

Visualize the above data with Pie Chart using Python Programming Language.

36 An array representing the ages of a group of people:


ages = np.array([18, 22, 21, 19, 22, 24, 20, 25, 30, 32, 21, 20, 18, 19, 23])
1. Filter the array to find the ages that fall between 20 and 30, inclusive. Store this filtered
array as filtered_ages.
2. Calculate the mean and standard deviation of filtered_ages.
3. Create a new array adjusted_ages by subtracting the mean of filtered_ages from each
element of the original ages array.
4. Find the indices of the elements in adjusted_ages that are negative, indicating ages below
the mean of the filtered group.

37 Assume a 3*3 array of your choice. Write a program that sorts all the rows in the array and then
adds column wise. So you take a 3*3 array, sort all its rows and then add entries column wise.
Reference example:
Array [(3,1,2),(9,5,6),(4,8,7)]
Rows sorted : [[1 2 3] [5 6 9] [4 7 8]]
Added in column-wise fashion: [10 15 20]

You might also like