使用DFP算法求解最大熵模型的学习问题用python,加载的手写数据集实现
时间: 2023-06-26 09:03:53 浏览: 117
首先需要导入相关的库:
```python
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
```
然后加载手写数字数据集:
```python
digits = load_digits()
X, y = digits.data, digits.target
```
接着将数据集划分为训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
```
定义最大熵模型类:
```python
class MaxEnt:
def __init__(self, max_iter=100):
self.max_iter = max_iter
def _init_params(self, X, y):
self.num_samples, self.num_features = X.shape
self.num_classes = len(set(y))
self.X, self.y = X, y
self.w = np.zeros((self.num_features, self.num_classes))
self._calcu_feat_list()
def _calcu_feat_list(self):
self.feat_list = []
for i in range(self.num_samples):
feat_dict = {}
for j in range(self.num_features):
feat_dict[(j, self.X[i][j])] = feat_dict.get((j, self.X[i][j]), 0) + 1
self.feat_list.append(feat_dict)
def _calcu_p_y_given_x(self, x):
prob_list = []
for i in range(self.num_classes):
w_i = self.w[:, i]
prob = np.exp(np.dot(w_i, x)) / sum(np.exp(np.dot(self.w[:, j], x)) for j in range(self.num_classes))
prob_list.append(prob)
return prob_list
def train(self, X, y):
self._init_params(X, y)
for i in range(self.max_iter):
error = 0
for j in range(self.num_samples):
x = np.zeros(self.num_features)
for k, v in self.feat_list[j].items():
x[k[0]] = v
prob_list = self._calcu_p_y_given_x(x)
pred_y = np.argmax(prob_list)
if pred_y != self.y[j]:
error += 1
for k, v in self.feat_list[j].items():
self.w[k[0], self.y[j]] += v
self.w[k[0], pred_y] -= v
if error == 0:
break
def predict(self, X):
pred_list = []
for i in range(len(X)):
x = X[i]
prob_list = self._calcu_p_y_given_x(x)
pred_y = np.argmax(prob_list)
pred_list.append(pred_y)
return pred_list
```
最后实例化类并进行训练和预测:
```python
model = MaxEnt()
model.train(X_train, y_train)
pred_y = model.predict(X_test)
```
完整代码如下:
```python
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
class MaxEnt:
def __init__(self, max_iter=100):
self.max_iter = max_iter
def _init_params(self, X, y):
self.num_samples, self.num_features = X.shape
self.num_classes = len(set(y))
self.X, self.y = X, y
self.w = np.zeros((self.num_features, self.num_classes))
self._calcu_feat_list()
def _calcu_feat_list(self):
self.feat_list = []
for i in range(self.num_samples):
feat_dict = {}
for j in range(self.num_features):
feat_dict[(j, self.X[i][j])] = feat_dict.get((j, self.X[i][j]), 0) + 1
self.feat_list.append(feat_dict)
def _calcu_p_y_given_x(self, x):
prob_list = []
for i in range(self.num_classes):
w_i = self.w[:, i]
prob = np.exp(np.dot(w_i, x)) / sum(np.exp(np.dot(self.w[:, j], x)) for j in range(self.num_classes))
prob_list.append(prob)
return prob_list
def train(self, X, y):
self._init_params(X, y)
for i in range(self.max_iter):
error = 0
for j in range(self.num_samples):
x = np.zeros(self.num_features)
for k, v in self.feat_list[j].items():
x[k[0]] = v
prob_list = self._calcu_p_y_given_x(x)
pred_y = np.argmax(prob_list)
if pred_y != self.y[j]:
error += 1
for k, v in self.feat_list[j].items():
self.w[k[0], self.y[j]] += v
self.w[k[0], pred_y] -= v
if error == 0:
break
def predict(self, X):
pred_list = []
for i in range(len(X)):
x = X[i]
prob_list = self._calcu_p_y_given_x(x)
pred_y = np.argmax(prob_list)
pred_list.append(pred_y)
return pred_list
digits = load_digits()
X, y = digits.data, digits.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
model = MaxEnt()
model.train(X_train, y_train)
pred_y = model.predict(X_test)
accuracy = sum(pred_y == y_test) / len(y_test)
print('Accuracy:', accuracy)
```
阅读全文