使用python帮我写一个softmax回归
时间: 2023-05-30 21:02:01 浏览: 56
以下是一个简单的softmax回归的Python代码示例:
```python
import numpy as np
class SoftmaxRegression:
def __init__(self, learning_rate=0.1, num_epochs=1000):
self.learning_rate = learning_rate
self.num_epochs = num_epochs
def fit(self, X, y):
# Initialize weights with small random values
self.W = np.random.randn(X.shape[1], len(np.unique(y))) / np.sqrt(X.shape[1])
self.b = np.zeros(len(np.unique(y)))
# One-hot encode the labels
y_onehot = np.eye(len(np.unique(y)))[y]
# Train the model for num_epochs iterations
for i in range(self.num_epochs):
# Forward pass
z = np.dot(X, self.W) + self.b
exp_z = np.exp(z)
softmax = exp_z / np.sum(exp_z, axis=1, keepdims=True)
# Backward pass
dW = np.dot(X.T, (softmax - y_onehot))
db = np.sum(softmax - y_onehot, axis=0)
# Update weights and biases
self.W -= self.learning_rate * dW
self.b -= self.learning_rate * db
def predict(self, X):
z = np.dot(X, self.W) + self.b
exp_z = np.exp(z)
softmax = exp_z / np.sum(exp_z, axis=1, keepdims=True)
return np.argmax(softmax, axis=1)
```
使用示例:
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = SoftmaxRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print("Accuracy:", acc)
```
输出:
```
Accuracy: 1.0
```