where was this code i wonder

This commit is contained in:
ldlda committed 2025-06-14 09:37:24 +07:00
1 parent 8239b42a21
commit 4d11cb476f
5 files changed
+150

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
import pandas as pd
file_path = r"C:\Users\Student\Downloads\Practice_1\Advertising.csv"
required_columns = ['TV', 'radio', 'newspaper', 'sales']
def load_data(file_path = file_path, required_columns = required_columns):
"""
Load and validate the dataset.
Parameters:
file_path (str): Path to the CSV file.
required_columns (list): List of required column names.
Returns:
DataFrame: Loaded and validated dataset.
"""
try:
data = pd.read_csv(file_path)
# Verify the required columns exist
for col in required_columns:
if col not in data.columns:
raise ValueError(f"Column '{col}' is missing from the dataset.")
return data
except FileNotFoundError:
print("Error: File not found. Please check the file path.")
return None
except ValueError as e:
print(e)
return None
from itertools import repeat as _r
from functools import partial as _p
loop = _p(_r, None)
del _r
del _p
+57
View File
@@ -0,0 +1,57 @@
# import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import common
def normalize_features(X):
return (X - np.mean(X, axis=0)) / np.std(X, axis=0)
def gradient_descent(X, y, alpha, iterations):
m = len(y)
theta = np.zeros(X.shape[1])
cost_history = []
for _ in common.loop(iterations):
predictions = X.dot(theta)
errors = predictions - y
cost = (1/(2*m)) * np.sum(errors**2)
cost_history.append(cost)
gradients = (1/m) * X.T.dot(errors)
theta -= alpha * gradients
return theta, cost_history
# Main Script
data = common.load_data()
if data is None:
print("no data")
exit()
# Extract features and target
X = data[['TV', 'radio', 'newspaper']].values
y = data['sales'].values
# Normalize features
X = normalize_features(X)
# Add intercept term
X = np.c_[np.ones(X.shape[0]), X]
if __name__ == "__main__":
# Set hyperparameters
alpha = 0.01 # Learning rate
iterations = 1000 # Number of iterations
# Perform Gradient Descent
theta, cost_history = gradient_descent(X, y, alpha, iterations)
# Plot cost function
plt.plot(np.arange(iterations), cost_history)
plt.xlabel('Iteration')
plt.ylabel('Cost')
plt.title('Cost Function vs Iteration')
plt.show()
print(f"Final parameters: {theta}")
+21
View File
@@ -0,0 +1,21 @@
# import pandas as pd
import numpy as np
import common
# Load data
data = common.load_data()
if data is None:
print("no data")
exit()
# Prepare the data
X = data[['TV', 'radio', 'newspaper']].values # Update column names as per the dataset
y = data['sales'].values
# Add intercept term
X = np.c_[np.ones(X.shape[0]), X]
if __name__ == "__main__":
# Normal Equation
theta = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
print("Parameters calculated using Normal Equation:", theta)
+21
View File
@@ -0,0 +1,21 @@
import numpy as np
import common
from p1 import X, y
def gradient_descent_with_regularization(X, y, theta, alpha, lambda_, iterations):
m = len(y)
for _ in common.loop(iterations):
predictions = X @ theta
errors = predictions - y
gradient = (X.T @ errors + lambda_ * theta) / m
gradient[0] -= (lambda_/m) * theta[0] # Exclude bias term from regularization
theta -= alpha * gradient
return theta
if __name__ == "__main__":
theta = np.zeros(X.shape[1])
alpha = 0.01 # Learning rate
lambda_ = 1.0 # Regularization strength
iterations = 1000
theta = gradient_descent_with_regularization(X, y, theta, alpha, lambda_, iterations)
print("Regularized parameters using Gradient Descent:", theta)
+15
View File
@@ -0,0 +1,15 @@
from p2 import X, y
import numpy as np
def normal_equation_with_regularization(X, y, lambda_):
m, n = X.shape
I = np.eye(n) # Identity matrix
I[0, 0] = 0 # Don't regularize the bias term
theta = np.linalg.inv(X.T @ X + lambda_ * I) @ X.T @ y
return theta
if __name__ == "__main__":
theta = normal_equation_with_regularization(X, y, lambda_ = 1.)
print("Regularized parameters using Normal Equation:", theta)