使用python实现softmax回归算法,使用已知鸢尾花数据对模型进行训练,并对未知鸢尾花数据进行预测。
时间: 2024-05-15 14:16:12 浏览: 86
tables-3.6.1-cp39-cp39-win_amd64.whl
首先,我们需要导入必要的库和数据集:
```python
import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
```
接着,我们将数据集分为训练集和测试集:
```python
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)
```
然后,我们需要对数据进行预处理。首先,我们需要对特征进行归一化处理:
```python
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
```
接下来,我们需要对标签进行one-hot编码:
```python
def one_hot(y):
n_classes = len(np.unique(y))
return np.eye(n_classes)[y]
y_train = one_hot(y_train)
y_test = one_hot(y_test)
```
现在,我们可以实现softmax回归算法。首先,我们需要定义一个softmax函数:
```python
def softmax(z):
e = np.exp(z - np.max(z, axis=1, keepdims=True))
return e / np.sum(e, axis=1, keepdims=True)
```
然后,我们定义一个损失函数:
```python
def cross_entropy_loss(y_true, y_pred):
return -np.mean(np.sum(y_true * np.log(y_pred), axis=1))
```
接下来,我们可以开始训练模型了。我们需要定义一个函数来计算梯度:
```python
def grad(X, y_true, y_pred):
m = X.shape[0]
return 1/m * np.dot(X.T, (y_pred - y_true))
```
然后,我们需要定义一个函数来进行模型训练:
```python
def fit(X, y, lr=0.1, n_epochs=1000):
n_features = X.shape[1]
n_classes = y.shape[1]
W = np.random.randn(n_features, n_classes)
b = np.zeros(n_classes)
losses = []
for epoch in range(n_epochs):
z = np.dot(X, W) + b
y_pred = softmax(z)
loss = cross_entropy_loss(y, y_pred)
losses.append(loss)
grad_W = grad(X, y, y_pred)
grad_b = np.mean(y_pred - y, axis=0)
W -= lr * grad_W
b -= lr * grad_b
if (epoch+1) % 100 == 0:
print(f"Epoch {epoch+1}/{n_epochs}, loss={loss:.4f}")
return W, b, losses
```
现在,我们可以对模型进行训练:
```python
W, b, losses = fit(X_train, y_train)
```
最后,我们可以使用训练好的模型对测试集进行预测,并计算准确率:
```python
def predict(X, W, b):
z = np.dot(X, W) + b
y_pred = softmax(z)
return np.argmax(y_pred, axis=1)
y_pred = predict(X_test, W, b)
accuracy = np.mean(y_pred == np.argmax(y_test, axis=1))
print(f"Accuracy: {accuracy:.2f}")
```
完整代码如下:
```python
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
def softmax(z):
e = np.exp(z - np.max(z, axis=1, keepdims=True))
return e / np.sum(e, axis=1, keepdims=True)
def cross_entropy_loss(y_true, y_pred):
return -np.mean(np.sum(y_true * np.log(y_pred), axis=1))
def grad(X, y_true, y_pred):
m = X.shape[0]
return 1/m * np.dot(X.T, (y_pred - y_true))
def fit(X, y, lr=0.1, n_epochs=1000):
n_features = X.shape[1]
n_classes = y.shape[1]
W = np.random.randn(n_features, n_classes)
b = np.zeros(n_classes)
losses = []
for epoch in range(n_epochs):
z = np.dot(X, W) + b
y_pred = softmax(z)
loss = cross_entropy_loss(y, y_pred)
losses.append(loss)
grad_W = grad(X, y, y_pred)
grad_b = np.mean(y_pred - y, axis=0)
W -= lr * grad_W
b -= lr * grad_b
if (epoch+1) % 100 == 0:
print(f"Epoch {epoch+1}/{n_epochs}, loss={loss:.4f}")
return W, b, losses
def predict(X, W, b):
z = np.dot(X, W) + b
y_pred = softmax(z)
return np.argmax(y_pred, axis=1)
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
def one_hot(y):
n_classes = len(np.unique(y))
return np.eye(n_classes)[y]
y_train = one_hot(y_train)
y_test = one_hot(y_test)
W, b, losses = fit(X_train, y_train)
y_pred = predict(X_test, W, b)
accuracy = np.mean(y_pred == np.argmax(y_test, axis=1))
print(f"Accuracy: {accuracy:.2f}")
```
阅读全文