Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.1 kB
1
Indexable
Never
import numpy as np
import matplotlib.pyplot as plt

# Generate random data
np.random.seed(0)
X = 2 * np.random.rand(100, 1)     # Generate 100 random values between 0 and 2
y = 3 + 4 * X + np.random.randn(100, 1)  # Generate y values with some noise

# Plot the random data
plt.scatter(X, y)
plt.xlabel('X')
plt.ylabel('y')
plt.title('Random Data')
plt.show()

def compute_cost(X, y, theta):
    """
    Compute the cost function J(theta) for linear regression.
    """
    m = len(y)
    h = np.dot(X, theta)
    J = 1/(2*m) * np.sum((h - y)**2)
    return J

def gradient_descent(X, y, theta, alpha, num_iters):
    """
    Perform gradient descent to minimize the cost function J(theta).
    """
    m = len(y)
    J_history = np.zeros(num_iters)
    for iter in range(num_iters):
        h = np.dot(X, theta)
        theta = theta - (alpha/m) * np.dot(X.T, (h - y))
        J_history[iter] = compute_cost(X, y, theta)
    return theta, J_history

def linear_regression(X, y, alpha, num_iters):
    """
    Perform linear regression using gradient descent.
    """
    X_b = np.c_[np.ones((len(X), 1)), X]  # Add a column of ones for the bias term
    theta = np.zeros((X_b.shape[1], 1))   # Initialize theta with zeros
    theta, J_history = gradient_descent(X_b, y, theta, alpha, num_iters)
    return theta, J_history

# Hyperparameters
alpha = 0.01       # Learning rate
num_iters = 1000   # Number of iterations

# Perform linear regression
theta, J_history = linear_regression(X, y, alpha, num_iters)

# Print the learned parameters theta
print("Theta:", theta)

# Plot the linear regression line
plt.scatter(X, y)
plt.plot(X, np.dot(np.c_[np.ones((len(X), 1)), X], theta), color='red')
plt.xlabel('X')
plt.ylabel('y')
plt.title('Linear Regression with Gradient Descent')
plt.show()

plt.plot(range(num_iters), J_history)
plt.xlabel('Number of Iterations')
plt.ylabel('Cost Function J(theta)')
plt.title('Parameter vs Loss Curve')
plt.show()

# Calculate the mean squared error (MSE)
X_b = np.c_[np.ones((len(X), 1)), X]  # Add a column of ones for the bias term
mse = compute_cost(X_b, y, theta)

print("Mean Squared Error (MSE):", mse)
Leave a Comment