在Python中实现逻辑回归的梯度下降方法通常涉及以下步骤:
### 1. 导入必要的库
首先,确保你的环境中已安装了NumPy和Matplotlib(或Scikit-learn)等必要的科学计算和绘图工具。
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
```
### 2. 定义逻辑回归模型
接下来,我们定义一个简单的逻辑回归模型类,包括初始化参数、sigmoid函数以及梯度下降训练方法。
```python
class LogisticRegressionGD:
def __init__(self, learning_rate=0.1, max_iter=1000):
self.learning_rate = learning_rate
self.max_iter = max_iter
@staticmethod
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def fit(self, X, y):
# 初始化权重和偏置
n_features = X.shape[1]
self.weights = np.zeros(n_features)
self.bias = 0
for _ in range(self.max_iter):
# 计算预测值
linear_output = np.dot(X, self.weights) + self.bias
predictions = self.sigmoid(linear_output)
# 计算梯度
dw = (1 / X.shape[0]) * np.dot(X.T, (predictions - y))
db = (1 / X.shape[0]) * np.sum(predictions - y)
# 更新权重和偏置
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
predictions = self.sigmoid(linear_output)
return (predictions > 0.5).astype(int)
```
### 3. 数据预处理与划分
然后,你需要准备你的数据并进行适当的预处理(如归一化、添加常数项等),最后将数据集划分为训练集和测试集。
```python
def prepare_data():
# 加载鸢尾花数据集为例
iris = datasets.load_iris()
X = iris.data[:, :2] # 只取前两个特征
y = (iris.target == 2).astype(int) # 将目标变量转换为二分类问题
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 预处理(这里简化不进行具体操作)
return X_train, X_test, y_train, y_test
```
### 4. 使用模型训练和测试
最后,你可以使用训练好的模型对数据进行预测并评估其性能。
```python
def main():
X_train, X_test, y_train, y_test = prepare_data()
model = LogisticRegressionGD(learning_rate=0.1, max_iter=1000)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
# 计算准确率等评估指标
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
print("Test Accuracy: {:.2f}%".format(accuracy * 100))
if __name__ == "__main__":
main()
```
### 应用场景与示例
在人工智能大模型方面,逻辑回归可以广泛应用于多个领域,例如:
- 垃圾邮件过滤:通过训练一个逻辑回归模型来判断邮件是否为垃圾邮件。
- 医疗诊断:预测患者患某种疾病的概率。
- 信用评分:评估个人或企业的信用风险。
### 测试用例
为了验证上述代码的正确性,你可以使用以下数据点进行测试:
```python
def test_gradient_descent():
X = np.array([[0, 0], [1, 0], [2, 0]])
y = np.array([0, 1, 0])
model = LogisticRegressionGD(learning_rate=0.1, max_iter=100)
model.fit(X, y)
assert np.allclose(model.weights, [0., 0.], atol=0.5), "Weights do not match expected values"
assert model.bias >= 0, "Bias should be non-negative"
test_gradient_descent()
```
这个测试函数将创建一个简单的数据集,并使用梯度下降方法训练逻辑回归模型。它检查权重是否在预期范围内,并且偏置不为负数,从而验证了方法的正确性。