0% found this document useful (0 votes)
1 views3 pages

Lab Activity-1 Python Rutul-Solution

The document outlines a series of laboratory activities focused on Python applications in edge AI and vision. It includes exercises on outlier detection in website analytics, filtering top earners from an employee dataset, comparing execution times of square root calculations, and binarizing a grayscale image using both for loops and numpy functions. Each question provides a dataset, solution, and explanation of the methods used.

Uploaded by

Dr. Vijay Savani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Lab Activity-1 Python Rutul-Solution

The document outlines a series of laboratory activities focused on Python applications in edge AI and vision. It includes exercises on outlier detection in website analytics, filtering top earners from an employee dataset, comparing execution times of square root calculations, and binarizing a grayscale image using both for loops and numpy functions. Each question provides a dataset, solution, and explanation of the methods used.

Uploaded by

Dr. Vijay Savani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

School of Technology, Nirma University

Department of Electronics and Communication Engineering


STTP on Edge AI and Vision
Laboratory Activity 1 — Python

Question 1: Website Analytics — Outlier Detection


A website analytics dataset contains day-wise records of visited users, bounce rate, and duration.
Identify the outlier row where all three values exceed one standard deviation from the mean.

Dataset
import numpy as np

# (row = day), (col = users, bounce, duration)


a = [Link]([[815, 70, 115],
[767, 80, 50],
[912, 74, 77],
[554, 88, 70],
[1008, 65, 128]])

Solution — Single Line Logic


outliers = a[[Link]([Link](a - [Link](axis=0)) > [Link](axis=0), axis=1)]
print(outliers)

Output
[[1008 65 128]]

Explanation
[Link](axis=0) computes the column-wise mean. [Link](axis=0) computes the column-wise
standard deviation. [Link](a - mean) > std checks each value against its column's std. [Link](...,
axis=1) keeps only rows where ALL three columns satisfy the condition.

Question 2: HR — Top Earners Filter


Find all employees earning at least $100,000 per year. Output a list of (name, salary) tuples using
(i) a for loop and (ii) a single line logic.

Dataset
employees = {
'Alice' : 100000,
'Bob' : 99817,
'Carol' : 122908,
'Frank' : 88123,
'Eve' : 9312
}
top_earners = []

(i) Using For Loop


for name, salary in [Link]():
if salary >= 100000:
top_earners.append((name, salary))

print(top_earners)

(ii) Single Line Logic


top_earners = [(name, sal) for name, sal in [Link]() if sal >= 100000]
print(top_earners)

Output
[('Alice', 100000), ('Carol', 122908)]

Question 3: Square Root — Timing Comparison


Compare execution time of operator-based square root (x ** 0.5) versus [Link]() using
Python's timeit() module.

Code
import timeit
import math

# Operator-based square root


t1 = [Link]('x ** 0.5', setup='x = 144', number=1000000)

# [Link]()
t2 = [Link]('[Link](x)', setup='import math; x = 144', number=1000000)

print(f'Operator (x**0.5) : {t1:.4f} seconds')


print(f'[Link](x) : {t2:.4f} seconds')
print(f'Faster: {"[Link]" if t2 < t1 else "** operator"}')

Sample Output
Operator (x**0.5) : 0.0521 seconds
[Link](x) : 0.0318 seconds
Faster: [Link]

Explanation
[Link]() is generally faster than the ** 0.5 operator because it calls the C standard library
function directly, avoiding Python's general-purpose exponentiation overhead. Results may vary
slightly by platform and Python version.

Question 4: Image Binarization


Simulate a 300x300 grayscale image and binarize it: assign 1 if pixel > 128, else 0. Implement
using (i) a for loop and (ii) [Link]() in a single line.

Setup
import numpy as np

frame = [Link](0, 255, (300, 300), dtype=np.uint8)

(i) Using For Loop


binary = [Link]((300, 300), dtype=np.uint8)

for i in range([Link][0]):
for j in range([Link][1]):
if frame[i, j] > 128:
binary[i, j] = 1
else:
binary[i, j] = 0

print('Binary frame shape:', [Link])


print('Unique values:', [Link](binary))

(ii) Single Line using [Link]()


binary = [Link](frame > 128, 1, 0)

print('Binary frame shape:', [Link])


print('Unique values:', [Link](binary))

Output
Binary frame shape: (300, 300)
Unique values: [0 1]

Explanation
The for loop iterates every pixel (90,000 iterations for 300x300) and conditionally assigns 0 or 1.
[Link](condition, x, y) achieves the same result in a single vectorized call — significantly
faster as it operates in compiled C internally, avoiding Python loop overhead.

You might also like