Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Lab Assignment 2

Uploaded by

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

Lab Assignment 2

Uploaded by

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

In [1]: # Q1. Write the python script to solve the following problem.

Find the MSE, RMSE, MAE, SSE and R2 Error.


import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
weeks = np.array([1, 2, 3, 4, 5])
sales = np.array([1.2, 1.8, 2.6, 3.2, 3.8])
weeks = weeks.reshape(-1, 1)
model = LinearRegression()
model.fit(weeks, sales)
week_7 = np.array([[7]])
week_12 = np.array([[12]])
sales_pred_7 = model.predict(week_7)
sales_pred_12 = model.predict(week_12)
print(f"Predicted sales for the 7th week: {sales_pred_7[0]:.2f} thousand")
print(f"Predicted sales for the 12th week: {sales_pred_12[0]:.2f} thousand")
mse = mean_squared_error(sales, model.predict(weeks))
rmse = np.sqrt(mse)
mae = mean_absolute_error(sales, model.predict(weeks))
sse = np.sum((sales - model.predict(weeks))**2)
r2 = r2_score(sales, model.predict(weeks))
print(f"\nMean Squared Error (MSE): {mse:.4f}")
print(f"Root Mean Squared Error (RMSE): {rmse:.4f}")
print(f"Mean Absolute Error (MAE): {mae:.4f}")
print(f"Sum of Squared Errors (SSE): {sse:.4f}")
print(f"R-squared (R2): {r2:.4f}")

Predicted sales for the 7th week: 5.16 thousand


Predicted sales for the 12th week: 8.46 thousand

Mean Squared Error (MSE): 0.0024


Root Mean Squared Error (RMSE): 0.0490
Mean Absolute Error (MAE): 0.0400
Sum of Squared Errors (SSE): 0.0120
R-squared (R2): 0.9973

In [2]: # Q2. Write Python script to find the statistics of the given data (take your own) such as Mean, Median, Standard deviation, Variance and mode.
import numpy as np
from scipy import stats
data = np.array([15, 22, 31, 35, 47, 51, 63, 74, 82, 92])
mean_value = np.mean(data)
median_value = np.median(data)
std_dev = np.std(data)
variance = np.var(data)
mode_result = stats.mode(data)
print(f"Data: {data}")
print(f"Mean: {mean_value:.2f}")
print(f"Median: {median_value}")
print(f"Standard Deviation: {std_dev:.2f}")
print(f"Variance: {variance:.2f}")
print(f"Mode: {mode_result.mode[0]} (occurs {mode_result.count[0]} times)")

Data: [15 22 31 35 47 51 63 74 82 92]


Mean: 51.20
Median: 49.0
Standard Deviation: 24.75
Variance: 612.36
Mode: 15 (occurs 1 times)
C:\Users\Yashu\AppData\Local\Temp\ipykernel_11676\180578166.py:9: FutureWarning: Unlike other reduction functions (e.g. `skew`, `kurtosis`), the default behav
ior of `mode` typically preserves the axis it acts along. In SciPy 1.11.0, this behavior will change: the default value of `keepdims` will become False, the `
axis` over which the statistic is taken will be eliminated, and the value None will no longer be accepted. Set `keepdims` to True or False to avoid this warni
ng.
mode_result = stats.mode(data)

In [3]: # Write a python script to implement the KNN Algorithm over given data set and find the class of (5.5. 5.5).
from sklearn.neighbors import KNeighborsClassifier
data = [
[2, 2, 'Car'],
[5, 3.5, 'Car'],
[3.5, 4.5, 'Car'],
[4.5, 2, 'Car'],
[4.5, 6.5, 'Car'],
[2.5, 4.5, 'Truck'],
[6.5, 5.5, 'Truck'],
[6.5, 2.5, 'Truck'],
[7.5, 3.5, 'Truck'],
[3, 2, 'Truck'],
[4.5, 7, 'Truck'],
[6, 7, 'Bus'],
[6.5, 4.5, 'Bus'],
[7.5, 6.5, 'Bus'],
[5, 3.5, 'Bus'],
[5.5, 5.5, 'Bus']
]
X = [row[:2] for row in data]
Y = [row[2] for row in data]
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X, Y)
point_to_classify = [[5.5, 5.5]]
predicted_class = knn.predict(point_to_classify)
print(f"The class of the point (5.5, 5.5) is: {predicted_class[0]}")

The class of the point (5.5, 5.5) is: Bus

In [ ]:

You might also like