# 一、实验
## 实验一:SVM
### 1. 实验目的
1. 掌握线性支持向量机(SVM)分类器;
2. 掌握基于高斯核的SVM分类器;
3. 掌握基于拉普拉斯核的SVM分类器。
### 2. 数据集介绍
| 数据集 | 样本数 | 维度 | 类数 | 数据类型 |
| ------ | ------ | ---- | ---- | ---------- |
| mnist | 3000 | 784 | 10 | 手写体数字 |
| yale | 165 | 1024 | 15 | 人脸图像 |
| lung | 203 | 3312 | 5 | 生物数据 |
### 3. 实验内容
1. 编写程序实现线性SVM分类器设计;
2. 编写程序实现基于高斯核的SVM分类器设计;
3. 编写程序实现基于拉普拉斯核的SVM分类器设计。
### 4. 代码实现
```python
# coding=utf-8
from sklearn import svm, datasets
import numpy as np
import scipy.io as scio
from sklearn.model_selection import train_test_split
import math
def load_data(path):
data = scio.loadmat(path)
data_x = data['X']
data_y = data['Y'][:, 0] - 1
return np.array(data_x), np.array(data_y)
def laplace(X1, X2):
K = np.zeros((len(X1), len(X2)), dtype=np.float)
for i in range(len(X1)):
for j in range(len(X2)):
K[i][j] = math.exp(-math.sqrt(np.dot(X1[i] - X2[j], (X1[i] - X2[j]).T)) / 2)
return K
def classify(path, kernel):
x, y = load_data(path)
train_data, test_data, train_label, test_label = train_test_split(x, y, test_size=0.25, random_state=10)
predictor = svm.SVC(gamma='scale', C=2.0, decision_function_shape='ovr', kernel=kernel)
predictor.fit(train_data, train_label.ravel())
print('训练集:', predictor.score(train_data, train_label))
print('测试集:', predictor.score(test_data, test_label))
if __name__ == '__main__':
mnist_path = 'datasets/MNIST.mat'
lung_path = 'datasets/lung.mat'
yale_path = 'datasets/Yale.mat'
print('mnist数据集:')
classify(mnist_path, 'linear')
print('lung数据集:')
classify(lung_path, 'rbf')
print('yale数据集:')
classify(yale_path, laplace)
```
## 实验二:神经网络
### 1. 实验目的
1. 掌握全连接神经网络的训练与测试方法;
2. 掌握基于RBF分类器训练与测试方法;
### 2. 数据集介绍
| 数据集 | 样本数 | 维度 | 类数 | 数据类型 |
| ------ | ------ | ---- | ---- | ---------- |
| mnist | 3000 | 784 | 10 | 手写体数字 |
| yale | 165 | 1024 | 15 | 人脸图像 |
| lung | 203 | 3312 | 5 | 生物数据 |
### 3. 实验内容
1. 编写程序实现全连接神经网络分类器设计;
2. 编写程序实现基于RBF分类器设计;
### 4. 代码实现
1. BP 神经网络
```python
# coding=utf-8
import numpy as np
import scipy.io as scio
from torch import nn, optim
import torch
from torch.utils.data import DataLoader, random_split, TensorDataset
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def load_data(path):
data = scio.loadmat(path)
data_x = torch.from_numpy(np.array(data['X']))
data_y = torch.from_numpy(np.array(data['Y'][:, 0] - 1))
data = TensorDataset(data_x, data_y)
train_size = int(0.8 * len(data))
test_size = len(data) - train_size
train_data, test_data = random_split(data, [train_size, test_size], generator=torch.manual_seed(0))
return train_data, test_data
class MyNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(MyNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)
def forward(self, x):
x = x.to(torch.float32)
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
return out
def train(train_loader, model):
optimizer = optim.SGD(model.parameters(), lr=0.5, momentum=0.8)
closs = nn.CrossEntropyLoss()
for i, data in enumerate(train_loader):
optimizer.zero_grad()
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
pred = model(inputs)
loss = closs(pred, labels)
loss.backward()
optimizer.step()
def test(test_loader, model):
correct = 0
total = 0
for i, data in enumerate(test_loader):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
pred = model(inputs)
_, predicted = torch.max(pred.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print(' 准确率:{}%'.format(correct * 100 / total))
return correct * 100 / total
if __name__ == '__main__':
mnist_path = 'datasets/MNIST.mat'
lung_path = 'datasets/lung.mat'
yale_path = 'datasets/Yale.mat'
print('mnist: ')
batch_size = 50
input_size = 784
hidden_size = 500
num_classes = 10
epoch = 30 # 0.5 0.8
train_data, test_data = load_data(mnist_path)
train_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=True)
model = MyNet(input_size, hidden_size, num_classes).to(device)
best = 0
for i in range(epoch):
print('epoch:', i, end='')
train(train_loader, model)
best = max(test(test_loader, model), best)
print('最高准确率:{}%'.format(best))
torch.cuda.empty_cache()
print('lung: ')
batch_size = 20
input_size = 3312
hidden_size = 500
num_classes = 5
epoch = 100 # 0.0005 0.9
train_data, test_data = load_data(lung_path)
train_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=True)
model = MyNet(input_size, hidden_size, num_classes).to(device)
best = 0
for i in range(epoch):
print('epoch:', i, end='')
train(train_loader, model)
best = max(test(test_loader, model), best)
print('最高准确率:{}%'.format(best))
torch.cuda.empty_cache()
print('yale: ')
batch_size = 10
input_size = 1024
hidden_size = 300
num_classes = 15
epoch = 50 # 0.000082 0.86
train_data, test_data = load_data(yale_path)
train_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=True)
model = MyNet(input_size, hidden_size, num_classes).to(device)
best = 0
for i in range(epoch):
print('epoch:', i, end='')
train(train_loader, model)
best = max(test(test_loader, model), best)
print('最高准确率:{}%'.format(best))
torch.cuda.empty_cache()
```
2. RBF 分类器
```python
# coding=utf-8
import numpy as np
import scipy.io as scio
from torch import nn, optim
import torch
from torch.utils.data import DataLoader, random_split, TensorDataset
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# device = 'cpu'
def load_data(path):
data = scio.loadmat(path)
data_x = torch.from_numpy(np.array(data['X']).astype(np.float32))
data_y = torch.from_numpy(np.array(data['Y'][:, 0] - 1))
data = TensorDataset(data_x, data_y)
train_size = int(0.8 * len(data))
test_size = len(data) - train_size
train_data, test_data = random_split(data, [train_size, test_size], generator=torch.manual_seed(0))
return data_x, train_data, test_data
class RBFN(nn.Module):
"""
以高斯核作为径向基函数
"""
def __init__(self, centers, n_out=3):
"""
:param centers: shape=[center_num,data_dim]
:param n_out: num_classes
"""
super(RBFN, self).__init__()
self.n_out = n_out
self.num_centers = centers.size(0) # 隐层节点的个数
self.dim_centure =
onnx
- 粉丝: 1w+
- 资源: 5627
最新资源
- 1998-2022年全国及31省份一般公共预算收支(财政收入、支出)数据-最新出炉.zip
- 1998-2022年上市公司产学研合作成果与专利数据汇总-最新出炉.zip
- 1998-2022年机构投资者持股数量与持股比例数据汇总-最新出炉.zip
- 1998-2022年中国各企业避税程度相关数据-最新出炉.zip
- 1998-2022年省级产业结构合理化与高级化完整计算过程(2022更新,无缺失)-最新出炉.zip
- 1998-2022年上市公司共同机构所有权指标(参考顶刊,含构造过程与Stata代码)-最新出炉.zip
- 1999-2020年全国各省法治水平指数数据统计-最新出炉.zip
- 1999-2021年中国分省平均受教育年限面板数据(两份)-最新出炉.zip
- 1999-2021年投资效率(Biddle模型和Chen模型,含非效率投资情况)数据-最新出炉.zip
- 1999-2022年全国分省份按城乡全社会固定资产投资数据汇总-最新出炉.zip
- 1999-2022年上市公司金融错配、信贷错配及金融资源配置数据合集-最新出炉.zip
- 1999-2022年上市公司员工人数数据统计-最新出炉.zip
- 1999-2022年省级研究与试验发展(R&D)经费内部支出更新(2022年,无缺失)-最新出炉.zip
- 1999-2022年投资者、消费者、企业等信心与景气指数表-最新出炉.zip
- 1999-2022年投资效率(Biddle和Chen模型,非效率投资情况)数据-最新出炉.zip
- 1999-2022年中国300多地级市土地出让金收入数据集-最新出炉.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈