手写线性回归代码,并做实验评估算法性能
时间: 2024-01-16 17:14:40 浏览: 28
好的,我可以帮你手写线性回归代码并进行实验评估。以下是代码实现:
```python
import numpy as np
import matplotlib.pyplot as plt
class LinearRegression:
def __init__(self, learning_rate=0.01, num_iterations=1000):
self.learning_rate = learning_rate
self.num_iterations = num_iterations
self.weights = None
self.bias = None
def fit(self, X, y):
# Initialize weights and bias
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
# Gradient descent
for i in range(self.num_iterations):
y_pred = np.dot(X, self.weights) + self.bias
dw = (1 / n_samples) * np.dot(X.T, (y_pred - y))
db = (1 / n_samples) * np.sum(y_pred - y)
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
def predict(self, X):
y_pred = np.dot(X, self.weights) + self.bias
return y_pred
def mse(self, y_pred, y_true):
return np.mean((y_pred - y_true) ** 2)
```
这里我们使用梯度下降法来学习线性回归模型的参数。在训练过程中,我们计算预测值与真实值之间的误差,并通过计算梯度来更新权重和偏置。
下面我们来用一个简单的实例来测试我们的代码:
```python
# Generate some random data
np.random.seed(0)
X = np.random.rand(100, 1)
y = 2 + 3 * X + np.random.randn(100, 1)
# Split data into training and test sets
X_train, X_test = X[:80], X[80:]
y_train, y_test = y[:80], y[80:]
# Train the model
model = LinearRegression()
model.fit(X_train, y_train)
# Make predictions on test set
y_pred = model.predict(X_test)
# Evaluate the model
mse = model.mse(y_pred, y_test)
print("Mean Squared Error:", mse)
# Plot the results
plt.scatter(X_test, y_test, color='blue')
plt.plot(X_test, y_pred, color='red')
plt.show()
```
这里我们生成了一些随机数据,并将其分为训练集和测试集。然后我们训练模型并在测试集上进行预测。最后,我们计算模型的均方误差并绘制真实值和预测值之间的关系图。
你可以根据自己的需要更改参数,并使用不同的数据集来测试模型的性能。