PYTHON PRACTICAL
FILE
Experiment No.9
Date: 07/04/2026
Aim: To create and analyze a NumPy array representing
temperature data of multiple cities, and to perform
statistical operations such as mean, maximum, and
identification of extreme values, also create Simple Linear
Regression model from scratch.
1. Problem Statement: A data scientist is
analyzing temperature data recorded over 7
days in 3 different cities. The data stores in a
NumPy array are given.
(a) Create a NumPy array to store the
temperature data
(b) Calculate the average temperature of
each city
(c) Find the maximum temperature
recorded in each city
(d) Determine the overall highest
temperature among all the cities
1
(e) Identify the cities with the highest
average temperature
Algorithm:
1. Start
2. Import NumPy library
3. Create a 2D array temperature data
• Store temperatures of 3 cities (each
row = one city)
4. Display the temperature data
5. Calculate average temperature of each city
• Use mean() function with axis = 1
6. Display average temperature
• Use loop with enumerate()
• Print city number and average
(rounded to 2 decimal places)
7. Find maximum temperature of each city
• Use max() function with axis = 1
8. Display maximum temperature
• Use loop with enumerate()
• Print city number and maximum temp
9. Find overall highest temperature
• Use max() on entire array
2
10. Display overall highest temperature
11. Find city with highest average temperature
• Use argmax() on average array
• Add 1 to get correct city number
12. Display the city with highest average temp
13. Stop
3
CODE SCREENSHOT AND OUTPUT:
4
2. Problem Statement: Import required libraries
(NumPy, Matplotlib):
[Link] or input the dataset (independent and dependent
variables).
2. Reshape the input data if required.
3. Fit the Linear Regression model.
4. Predict output values using the value.
5. Plot the regression line along with actual data.
Algorithm:
Step 1: Start
Step 2: Import required libraries (numpy, matplotlib)
Step 3: Define function estcoefficients(x, y)
Find number of elements
n ← size of x
Calculate mean of x and y
meanx ← mean(x)
meany ← mean(y)
Compute sum for numerator
sy ← Σ(x*y) − n × meanx × meany
Compute sum for denominator
5
sx ← Σ(x²) − n × (meanx)²
Calculate slope
a ← sy / sx (note: logically this is slope; your
code reverses it)
Calculate intercept
b ← meany − a × meanx
Print a and b
Return (a, b)
Step 4: Define function plotregline(x, y, a, b)
Plot scatter graph of (x, y)
Calculate predicted values
ypred ← b + a × x
Plot regression line using (x, ypred)
Label X-axis as "SIZE"
Label Y-axis as "COST"
Display graph
Step 5: Create arrays
x ← [1, 2, 3, 4, 5]
y ← [7, 9, 11, 13, 15]
Step 6: Call estcoefficients(x, y)
Store returned values in a, b
Step 7: Call plotregline(x, y, a, b)
6
CODE SCREENSHOT AND OUTPUT:
7
3. Problem Statement: Import NumPy library. Create
arrays using different functions. Display the arrays.
Algorithm:
Start
Import numpy library.
Create array a with elements [1, 2, 3, 4].
Create array b of zeros with shape (2, 3).
Create array c of ones with shape (3, 2).
Create array d using arange from 1 to 10
with step 2.
Create array e using linspace from 0 to 1
with 5 equally spaced values.
Print array a.
Print array b.
Print array c.
Print array d.
Print array e.
Stop.
8
CODE SCREENSHOT AND OUTPUT:
9
4. Problem Statement: Perform slicing and
indexing operations on NumPy arrays.
Algorithm:
Start
Import numpy library.
Create 1-D array a with elements [10, 20, 30, 40, 50].
Print the original array a.
Slice the array from index 1 to index 3: a[1:4].
Print the sliced array.
Select every second element of array a using a[::2].
Print the elements obtained in step 7.
Create 2-D array b with values:
123
456
789
Print the 2-D array b.
Extract the first row of array b using b[0, :].
Print the first row.
Stop.
10
CODE SCREENSHOT AND OUTPUT:
11
5. Problem Statement: Import NumPy library, Create
sample arrays, Perform concatenation using different axes,
Perform stacking operations, Observe shape changes .
Algorithm:
1. Start
2. Import numpy library.
Concatenation of 1-D arrays
3. Create array a ← [1, 2, 3].
4. Create array b ← [4, 5, 6].
5. Print "Concatenation".
6. Concatenate arrays a and b using
[Link]((a,b)) and store in
concat.
7. Print concatenated array concat.
Concatenation of 2-D arrays
8. Create 2-D array A ← [[1,2],[3,4]].
9. Create 2-D array B ← [[5,6],[7,8]].
10. Concatenate A and B row-wise using
axis = 0 and store in concat_row.
11. Print row-wise concatenation.
12
12. Concatenate A and B column-wise
using axis = 1 and store in concat_col.
13. Print column-wise concatenation.
Stacking arrays
14. Print "Stacking".
15. Stack arrays a and b along axis = 0
and store in Stack0.
16. Stack arrays a and b along axis = 1
and store in Stack1.
17. Print Stack0.
18. Print Stack1.
Horizontal, Vertical and Depth stacking
19. Perform horizontal stack of a and b
using hstack and store in h_stack.
20. Print horizontal stack.
21. Perform vertical stack of a and b
using vstack and store in v_stack.
22. Print vertical stack.
23. Perform depth stack of a and b using
dstack and store in d_stack.
13
24. Print depth stack.
25. Stop.
14
CODE SCREENSHOT AND OUTPUT:
15
THANK YOU
16