0% found this document useful (0 votes)
3 views10 pages

Solution

The document contains a series of coding exercises involving linear equations, logistic regression, image processing, and data analysis using Python libraries such as NumPy, pandas, and Matplotlib. It includes implementations for solving systems of linear equations, applying logistic regression, manipulating images, and performing exploratory data analysis (EDA) on datasets. Additionally, it outlines the use of functions like loc, iloc, and groupby for data manipulation, along with basic statistical summaries of the data.
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)
3 views10 pages

Solution

The document contains a series of coding exercises involving linear equations, logistic regression, image processing, and data analysis using Python libraries such as NumPy, pandas, and Matplotlib. It includes implementations for solving systems of linear equations, applying logistic regression, manipulating images, and performing exploratory data analysis (EDA) on datasets. Additionally, it outlines the use of functions like loc, iloc, and groupby for data manipulation, along with basic statistical summaries of the data.
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

Question 1

Part A: Systems of Linear Equations


import numpy as np
# Data will be given in question paper
Time_Studied_x1 = [Link]([1, 2, 4, 5, 2])
CGPA_x2 = [Link]([2.1, 3.4, 2.8, 4.5, 3.9])

# you have to assume random array of y


y = [0, 1, 1, 0, 1]

# 1. Define the coefficient matrix (A)


A = [Link]([
Time_Studied_x1[0:2], # use slicing to select the first two
elements of Time_Studied_x1
CGPA_x2[0:2] # use slicing to select the first two elements of
CGPA_x2
])

# 2. Define the constant vector (b)


b = [Link]([1,2])

# 3. Solve for x and y


solution = [Link](A, b)

# Extract the individual variables


x_val, y_val = solution

print(f"x = {x_val:.4f}")
print(f"y = {y_val:.4f}")

x = 0.7500
y = 0.1250

Part B: Implement Logistic Regression


# Question 1 part 2 implement logistic regression from scratch using
numpy and sigmoid function.
w1 = x_val
w2 = y_val

# you can assume any value for b, as it is not given in the question.
its just to complete the formula of logistic regression.
b = 0

# y = wx + b
y = w1*Time_Studied_x1 + w2*CGPA_x2 + b
sigmoid = 1 / (1 + [Link](-y))

print(f"Before sigmoid function is applied (values are mostly greater


than 1 and 0): \n {y}")
print()
print(f"After sigmoid function is applied (values are shrinked between
0 and 1): \n {sigmoid}")

Before sigmoid function is applied (values are mostly greater than 1


and 0):
[1.0125 1.925 3.35 4.3125 1.9875]

After sigmoid function is applied (values are shrinked between 0 and


1):
[0.73350912 0.87269496 0.96610484 0.98677718 0.8794784 ]

Part B1.1: Build threshold logic to predict 1 or 0


# The model has trained! Now apply threshold to make predictions.
threshold = 0.75

# Declare new datapoints to make predictions on.


Time_Studied = 4
CGPA = 3.0

# y = wx + b
y = w1*Time_Studied + w2*CGPA + b # Step 1: Calculate the linear
combination of inputs and weights (y = wx + b)
sigmoid = 1 / (1 + [Link](-y)) # Step 2: Apply the sigmoid
function to get values between 0 and 1

if sigmoid >= threshold: # Step 3: Apply threshold to make


binary predictions
print("Predicted class (1) The person is likely to pass the
exam.")
else:
print("Predicted class (0) The person is likely to fail the
exam.")

Predicted class (1) The person is likely to pass the exam.

Question No:2
part a) Image Processing using numpy and pillow library
from PIL import Image
# Orignal image
image = [Link]("[Link]")
image

# Resize the image


#
[Link]((200, 200)) # Resize the image to a smaller size for
faster processing
# 1. Convert image into numpy
img_to_numpy = [Link](image)
img_to_numpy[0] # Access the first row of the numpy array

array([[145, 184, 225],


[145, 184, 225],
[146, 185, 226],
...,
[170, 195, 226],
[169, 194, 225],
[168, 195, 225]], dtype=uint8)

print(img_to_numpy.shape) # image shape

(527, 497, 3)

# 2. 2d Image
image_2d = [Link]('L') # Convert the image to grayscale (2D)
image_2d.resize((200, 200)) # Resize the grayscale image to a smaller
size for faster processing
# 3. Binary Image
image_binary = [Link]('1') # Convert the image to binary
(black and white)
image_binary.resize((200, 200)) # Resize the binary image to a
smaller size for faster processing

# 4. RGB Image
image_RGB = [Link]('RGB') # Convert the image to RGB (3D)
image_RGB.resize((200, 200)) # Resize the RGB image to a smaller size
for faster processing
part b) Loc, iloc, and groupby on a builtin sklearn diabetes dataset
from [Link] import load_diabetes # Paper A Diabetes
from [Link] import load_breast_cancer # Paper B Breast
Cancer

data1 = load_diabetes() # Load the diabetes dataset Paper A


data2 = load_breast_cancer() # Load the diabetes dataset Paper B
df1 = [Link]([Link], columns=data1.feature_names) # Paper A

df2 = [Link]([Link], columns=data2.feature_names) # Paper B


[Link]()

age sex bmi bp s1 s2


s3 \
0 0.038076 0.050680 0.061696 0.021872 -0.044223 -0.034821 -
0.043401
1 -0.001882 -0.044642 -0.051474 -0.026328 -0.008449 -0.019163
0.074412
2 0.085299 0.050680 0.044451 -0.005670 -0.045599 -0.034194 -
0.032356
3 -0.089063 -0.044642 -0.011595 -0.036656 0.012191 0.024991 -
0.036038
4 0.005383 -0.044642 -0.036385 0.021872 0.003935 0.015596
0.008142

s4 s5 s6
0 -0.002592 0.019907 -0.017646
1 -0.039493 -0.068332 -0.092204
2 -0.002592 0.002861 -0.025930
3 0.034309 0.022688 -0.009362
4 -0.002592 -0.031988 -0.046641
# Implement loc function
loc_function = [Link][df['age'] > 0.05] # Use the loc function to
filter rows where the 'age' column is greater than 0.05
loc_function

age sex bmi bp s1 s2


s3 \
2 0.085299 0.050680 0.044451 -0.005670 -0.045599 -0.034194 -
0.032356
7 0.063504 0.050680 -0.001895 0.066629 0.090620 0.108914
0.022869
17 0.070769 0.050680 0.012117 0.056301 0.034206 0.049416 -
0.039719
28 0.052606 -0.044642 -0.021295 -0.074527 -0.040096 -0.037639 -
0.006584
29 0.067136 0.050680 -0.006206 0.063187 -0.042848 -0.095885
0.052322
.. ... ... ... ... ... ...
...
402 0.110727 0.050680 -0.033151 -0.022885 -0.004321 0.020293 -
0.061809
408 0.063504 -0.044642 -0.050396 0.107944 0.031454 0.019354 -
0.017629
412 0.074401 -0.044642 0.085408 0.063187 0.014942 0.013091
0.015505
414 0.081666 0.050680 0.006728 -0.004534 0.109883 0.117056 -
0.032356
431 0.070769 0.050680 -0.030996 0.021872 -0.037344 -0.047034
0.033914

s4 s5 s6
2 -0.002592 0.002861 -0.025930
7 0.017703 -0.035816 0.003064
17 0.034309 0.027364 -0.001078
28 -0.039493 -0.000612 -0.054925
29 -0.076395 0.059424 0.052770
.. ... ... ...
402 0.071210 0.015568 0.044485
408 0.023608 0.058038 0.040343
412 -0.002592 0.006207 0.085907
414 0.091875 0.054720 0.007207
431 -0.039493 -0.014960 -0.001078

[61 rows x 10 columns]

# implement iloc function


iloc_function = [Link][0:5, 0:3] # Use the iloc function to select
the first 5 rows and the first 3 columns of the DataFrame
iloc_function
# Explanation
# [Rows, Columns]
# [0:5] means rows from 0 to 4
# [0:3] means columns from 0 to 2 (0 : age, 1: gender, 2:bmi)

age sex bmi


0 0.038076 0.050680 0.061696
1 -0.001882 -0.044642 -0.051474
2 0.085299 0.050680 0.044451
3 -0.089063 -0.044642 -0.011595
4 0.005383 -0.044642 -0.036385

# Groupby
groupby_function = [Link]('age').mean() # Use the groupby
function to group the DataFrame by the 'age' column and calculate the
mean of each group
# print(groupby_function)

Question 3: EDA and Matplotlib


Part a) Perform basic data information
# Print the shape of the DataFrame
print([Link])

(442, 10)

# data information
[Link]()

<class '[Link]'>
RangeIndex: 442 entries, 0 to 441
Data columns (total 10 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 age 442 non-null float64
1 sex 442 non-null float64
2 bmi 442 non-null float64
3 bp 442 non-null float64
4 s1 442 non-null float64
5 s2 442 non-null float64
6 s3 442 non-null float64
7 s4 442 non-null float64
8 s5 442 non-null float64
9 s6 442 non-null float64
dtypes: float64(10)
memory usage: 34.7 KB
# Check statistical summary of the dataset
[Link]()

age sex bmi bp


s1 \
count 4.420000e+02 4.420000e+02 4.420000e+02 4.420000e+02
4.420000e+02
mean -2.511817e-19 1.230790e-17 -2.245564e-16 -4.797570e-17 -
1.381499e-17
std 4.761905e-02 4.761905e-02 4.761905e-02 4.761905e-02
4.761905e-02
min -1.072256e-01 -4.464164e-02 -9.027530e-02 -1.123988e-01 -
1.267807e-01
25% -3.729927e-02 -4.464164e-02 -3.422907e-02 -3.665608e-02 -
3.424784e-02
50% 5.383060e-03 -4.464164e-02 -7.283766e-03 -5.670422e-03 -
4.320866e-03
75% 3.807591e-02 5.068012e-02 3.124802e-02 3.564379e-02
2.835801e-02
max 1.107267e-01 5.068012e-02 1.705552e-01 1.320436e-01
1.539137e-01

s2 s3 s4 s5
s6
count 4.420000e+02 4.420000e+02 4.420000e+02 4.420000e+02
4.420000e+02
mean 3.918434e-17 -5.777179e-18 -9.042540e-18 9.293722e-17
1.130318e-17
std 4.761905e-02 4.761905e-02 4.761905e-02 4.761905e-02
4.761905e-02
min -1.156131e-01 -1.023071e-01 -7.639450e-02 -1.260971e-01 -
1.377672e-01
25% -3.035840e-02 -3.511716e-02 -3.949338e-02 -3.324559e-02 -
3.317903e-02
50% -3.819065e-03 -6.584468e-03 -2.592262e-03 -1.947171e-03 -
1.077698e-03
75% 2.984439e-02 2.931150e-02 3.430886e-02 3.243232e-02
2.791705e-02
max 1.987880e-01 1.811791e-01 1.852344e-01 1.335973e-01
1.356118e-01

# Check for missing values in the dataset


[Link]().sum()

age 0
sex 0
bmi 0
bp 0
s1 0
s2 0
s3 0
s4 0
s5 0
s6 0
dtype: int64

# Data types
[Link]

age float64
sex float64
bmi float64
bp float64
s1 float64
s2 float64
s3 float64
s4 float64
s5 float64
s6 float64
dtype: object

# Features names
[Link]

Index(['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6'],
dtype='object')

Part b) Matplotlib: ~~~"Mid-Term" Question practice by your own

The questions that not implemented in this notebook


• Linear Regression (Mid-Term)
• Matplotlib (Mid-Term)

You might also like