2023-01-12 汽车状态分类器练习

https://mofanpy.com/tutorials/machine-learning/ML-practice/build-car-classifier-from-scratch1

汽车状态分类器练习

模型改为PyTorch实现
参考:https://blog.csdn.net/t18438605018/article/details/123563036

数据预处理

# 下载汽车数据
import pandas as pd
from urllib.request import urlretrieve

def load_data(download=True):
    # download data from : http://archive.ics.uci.edu/ml/datasets/Car+Evaluation
    if download:
        data_path, _ = urlretrieve("http://archive.ics.uci.edu/ml/machine-learning-databases/car/car.data", "car.csv")
        print("Downloaded to car.csv")

    # use pandas to view the data structure
    col_names = ["buying", "maint", "doors", "persons", "lug_boot", "safety", "class"]
    data = pd.read_csv("car.csv", names=col_names)
    return data
load_data(download=True)
Downloaded to car.csv
buying maint doors persons lug_boot safety class
0 vhigh vhigh 2 2 small low unacc
1 vhigh vhigh 2 2 small med unacc
2 vhigh vhigh 2 2 small high unacc
3 vhigh vhigh 2 2 med low unacc
4 vhigh vhigh 2 2 med med unacc
... ... ... ... ... ... ... ...
1723 low low 5more more med med good
1724 low low 5more more med high vgood
1725 low low 5more more big low unacc
1726 low low 5more more big med good
1727 low low 5more more big high vgood

1728 rows × 7 columns

data = load_data(download=True)
# print(data.head)
for name in data.keys():
    print(name, data[name].unique())
Downloaded to car.csv
buying ['vhigh' 'high' 'med' 'low']
maint ['vhigh' 'high' 'med' 'low']
doors ['2' '3' '4' '5more']
persons ['2' '4' 'more']
lug_boot ['small' 'med' 'big']
safety ['low' 'med' 'high']
class ['unacc' 'acc' 'vgood' 'good']
# onehot预处理
def convert2onehot(data):
    # covert data to onehot representation
    return pd.get_dummies(data, prefix=data.columns)
new_data = convert2onehot(data)
new_data.to_csv("car_onehot.csv", index=False)
new_data
image.png

搭建模型

import numpy as np
import pandas as pd
import torch
import torch.utils.data as Data
import matplotlib.pyplot as plt
from IPython import display
# 打乱数据的顺序, 然后将训练和测试数据以 7/3 比例分开
# prepare training data
new_data = pd.read_csv("car_onehot.csv")
new_data = new_data.values.astype(np.float32)       # change to numpy array and float32
np.random.shuffle(new_data)
sep = int(0.7*len(new_data))
train_data = new_data[:sep]                         # training data (70%)
test_data = new_data[sep:]                          # test data (30%)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using {device} device")

# 输入数据转换为Tensor
torch_train_data = torch.tensor(train_data)
torch_test_data  = torch.tensor(test_data)

# 设置输入输出变量
x_train = torch_train_data[:, :21]
y_train = torch_train_data[:, 21:]
x_test = torch_test_data[:, :21]
y_test = torch_test_data[:, 21:]

# 搭建网络
net = torch.nn.Sequential(
    torch.nn.Linear(21,128),  # 隐藏层1
    torch.nn.ReLU(),
    torch.nn.Linear(128,128), # 隐藏层2
    torch.nn.ReLU(),
    torch.nn.Linear(128,4),   # 输出层
    #torch.nn.Softmax(dim=1)  # CrossEntropyLoss本身就会对输出取softmax,所以无需这一层
).to(device)
net
Using cuda device

Sequential(
  (0): Linear(in_features=21, out_features=128, bias=True)
  (1): ReLU()
  (2): Linear(in_features=128, out_features=128, bias=True)
  (3): ReLU()
  (4): Linear(in_features=128, out_features=4, bias=True)
)
# 定义优化器和损失函数
# opt       = torch.optim.Adam(net.parameters(), lr=0.2, betas=(0.9, 0.99)) 
opt       = torch.optim.SGD(net.parameters(), lr=0.2) 
loss_func = torch.nn.CrossEntropyLoss().to(device)

train_dataset = Data.TensorDataset(x_train, y_train)
test_dataset  = Data.TensorDataset(x_test, y_test)

# 利用DataLoader批训练
BATCH_SIZE = 32    # 每次训练数据数量
EPOCH      = 100  # 总共训练的轮次

train_loader  = Data.DataLoader(
    dataset = train_dataset,
    batch_size = BATCH_SIZE,
    shuffle = True,
    num_workers = 5
)

test_loader  = Data.DataLoader(
    dataset = test_dataset,
    batch_size = BATCH_SIZE,
    shuffle = True,
    num_workers = 5
)
def show_img_acc_and_loss():
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))

    ax1.cla()
    ax1.plot(epoches, train_acc_his, 'r', label = 'train_acc')
    ax1.plot(epoches, test_acc_his, 'b--', label = 'test_acc')
    ax1.legend()
    ax1.set_xlabel('Epoches')
    ax1.set_ylabel('Accuracy')
    ax1.set_title('Model Accuracy')

    ax2.cla()
    ax2.plot(epoches, train_loss_his, 'r', label = 'train_loss')
    ax2.plot(epoches, test_loss_his, 'b--', label = 'test_loss')
    ax2.legend()
    ax2.set_xlabel('Epoches')
    ax2.set_ylabel('Loss')
    ax2.set_title('Model Loss')

    # plt.show()
    # plt.pause(0.5)
    # display.clear_output(wait=True) # 刷新图片

#show_img_acc_and_loss()   
def show_img_Ratio():
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4)) 

    # 各个batch的数据合并
    im_tp1 = [b for a in train_pred_his for b in a]
    # 合并list语句,参考:
    # https://blog.csdn.net/cxj540947672/article/details/107337082?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-107337082-blog-113962252.pc_relevant_3mothn_strategy_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=3
    im_tp1 = np.array(im_tp1)

    im_tt1 = [b for a in train_targ_his for b in a]
    # 合并list语句,参考:
    # https://blog.csdn.net/cxj540947672/article/details/107337082?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-107337082-blog-113962252.pc_relevant_3mothn_strategy_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=3
    im_tt1 = np.array(im_tt1)

    im_tp2 = [b for a in test_pred_his for b in a]
    # 合并list语句,参考:
    # https://blog.csdn.net/cxj540947672/article/details/107337082?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-107337082-blog-113962252.pc_relevant_3mothn_strategy_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=3
    im_tp2 = np.array(im_tp2)

    im_tt2 = [b for a in test_targ_his for b in a]
    # 合并list语句,参考:
    # https://blog.csdn.net/cxj540947672/article/details/107337082?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0-107337082-blog-113962252.pc_relevant_3mothn_strategy_recovery&spm=1001.2101.3001.4242.1&utm_relevant_index=3
    im_tt2 = np.array(im_tt2)

    for c in range(4):
        tp1 = ax1.bar(c+0.1, height=100*(im_tp1 == c).sum()/len(im_tp1), width=0.2, color='r')
        tt1 = ax1.bar(c-0.1, height=100*(im_tt1 == c).sum()/len(im_tp1), width=0.2, color='b')
        tp2 = ax2.bar(c+0.1, height=(im_tp2 == c).sum(), width=0.2, color='r')
        tt2 = ax2.bar(c-0.1, height=(im_tt2 == c).sum(), width=0.2, color='b')

    ax1.set_xticks(range(4), ["accepted", "good", "unaccepted", "very good"])
    ax1.legend(handles=[tp1, tt1], labels=["prediction", "target"])
    ax1.set_ylim(0, 100) 
    ax1.set_ylabel('Ratio (%)')
    ax1.set_title('Train Datasets')

    ax2.set_xticks(range(4), ["accepted", "good", "unaccepted", "very good"])
    ax2.legend(handles=[tp2, tt2], labels=["prediction", "target"])
    ax2.set_ylim(0, 400) 
    ax2.set_ylabel('Ratio (%)')
    ax2.set_title('Test Datasets')

    # plt.show()
    # plt.pause(0.5)
    # display.clear_output(wait=True) # 刷新图片

#show_img_Ratio()
# 训练并测试模型

train_acc_his  = []
train_loss_his = []
test_acc_his   = []
test_loss_his  = []
epoches        = []

#fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4))

for epoch in range (EPOCH):
    # print ('Epoch: ', (int(epoch)+1))

    #______________训练模型

    net.train() # 训练模式

    # 记录各个steps的历史值
    loss_his = [] 
    acc_his  = []
    train_pred_his = []
    train_targ_his = []

    for step, (b_x, b_y) in enumerate(train_loader):
        b_x, b_y = b_x.to(device), b_y.to(device)
        output = net(b_x)  # 调用网络
        loss   = loss_func(output, b_y) # 计算损失
        opt.zero_grad()    # 清空梯度
        loss.backward()    # 反向传播
        opt.step()         # 更新参数

        loss_his.append(loss.data.cpu().cpu().numpy()) # 记录历史损失
        # 计算准确度
        prediction = torch.max(output, 1)[1] # 取softmax输出最大值作为预测值
        # 第一个1表示返回这一行中最大值,第二个1表示只返回索引值,参考:
        # https://blog.csdn.net/weixin_43635550/article/details/100534904
        pred = prediction.data.cpu().numpy()
        target = torch.max(b_y, 1)[1]
        targ = target.data.cpu().numpy()
        acc = (pred==targ).sum() / b_y.shape[0] # 这一个batch的accuracy
        acc_his.append(acc)
        train_pred_his.append(pred)
        train_targ_his.append(targ)
        # print('step: ', step)

    train_acc  = sum(acc_his)/len(acc_his)   # 当前epoch的训练准确度
    train_loss = sum(loss_his)/len(loss_his) # 当前epoch的损失值

    #______________测试模型

    net.eval() # 测试模式

    # 记录各个steps的历史值
    loss_his = [] 
    acc_his  = []   
    test_pred_his = []
    test_targ_his = []

    for step, (b_x, b_y) in enumerate(test_loader):
        b_x, b_y = b_x.to(device), b_y.to(device)
        output = net(b_x)  # 调用网络
        loss   = loss_func(output, b_y) # 计算损失

        loss_his.append(loss.data.cpu().numpy()) # 记录历史损失
        # 计算准确度
        prediction = torch.max(output, 1)[1] # 取softmax输出最大值作为预测值
        # 第一个1表示返回这一行中最大值,第二个1表示只返回索引值,参考:
        # https://blog.csdn.net/weixin_43635550/article/details/100534904
        pred = prediction.cpu().numpy()
        target = torch.max(b_y, 1)[1]
        targ = target.cpu().numpy()
        acc = (pred==targ).sum() / b_y.shape[0] # 这一个batch的accuracy
        acc_his.append(acc)
        test_pred_his.append(pred)
        test_targ_his.append(targ)
        # print('step: ', step)

    test_acc  = sum(acc_his)/len(acc_his)   # 当前epoch的训练准确度
    test_loss = sum(loss_his)/len(loss_his) # 当前epoch的损失值

    #______________打印训练和测试结果
    if (int(epoch)+1) % 5 == 0:
        epoches.append((int(epoch)+1))

        print("Epoch:  %i" % (int(epoch)+1),"| Train Accuracy: %.2f" % train_acc, "| Train Loss: %.2f" % train_loss)
        train_acc_his.append(train_acc)      # 记录各个epoch的历史值
        train_loss_his.append(train_loss)

        print("Epoch:  %i" % (int(epoch)+1),"| Test Accuracy: %.2f" % test_acc, "| Test Loss: %.2f" % test_loss)
        test_acc_his.append(test_acc)      # 记录各个epoch的历史值
        test_loss_his.append(test_loss)

        # —————————————结果可视化
        show_img_acc_and_loss()
        show_img_Ratio()
        plt.pause(0.5)
        display.clear_output(wait=True) # 刷新图片
Epoch:  100 | Train Accuracy: 1.00 | Train Loss: 0.00
Epoch:  100 | Test Accuracy: 0.99 | Test Loss: 0.02
output_14_1.png
output_14_2.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,718评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,683评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,207评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,755评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,862评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,050评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,136评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,882评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,330评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,651评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,789评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,477评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,135评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,864评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,099评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,598评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,697评论 2 351

推荐阅读更多精彩内容