原教程
加载数据集
表格数据集CSV
此处使用NumPy来导入,除此之外还可以使用csv和Pandas导入
数据集链接
import csv
import numpy as np
wine_path = "../../data/chapter3/winequality-white.csv" # 路径
wineq_numpy = np.loadtxt(wine_path, dtype=np.float32, delimiter=";",
skiprows=1)
wineq_numpy #输出演示
# 检查
col_list = next(csv.reader(open(wine_path), delimiter=';')) # 取文件第一行
wineq_numpy.shape, col_list # 检查是否读取全部数据
# 将numpy数组转化为torch张量
import torch
wineq = torch.from_numpy(wineq_numpy)
wineq.shape, wineq.type()
# 拆分data和targtet
data = wineq[:, :-1] # 除最后一列外所有列,索引用法见原教程2.5
data, data.shape
target = wineq[:, -1] # 最后一列
target, target.shape
# 标签转化为整数,一般标签有实际比较意义如评分时使用,适用于本数据集
target = wineq[:, -1].long()
# 标签独热编码,一般标签无实际意义,仅作分类时使用
target_onehot = torch.zeros(target.shape[0], 10)
target_onehot.scatter_(1, target.unsqueeze(1), 1.0)
# 函数下划线结尾指就地修改张量,不返回新的张量
# unsqueeze(1)方法可提供一个额外的维度,具体见原文
# 获取每一列均值和标准差
data_mean = torch.mean(data, dim=0)
data_mean
data_var = torch.var(data, dim=0)
data_var
# 归一化
data_normalized = (data - data_mean) / torch.sqrt(data_var)
data_normalized
np.loadtxt()用法
next()用法
原文中有阈值预测的示例,此处不做介绍
文件式代码
'''
输出归一化属性数据和标签
'''
import csv
import torch as t
import numpy as np
DataPath = r'DataBase/winequality-white.csv'
IniDat_numpy = np.loadtxt(DataPath, dtype=np.float32, delimiter=';', skiprows=1)
col_list = next(csv.reader(open(DataPath), delimiter=';')) # 每一列属性名称
IniDat = t.from_numpy(IniDat_numpy)
data = IniDat[:, :-1] # 取最后一列外的张量做属性数据
target = IniDat[:, -1] # 取最后一列做标签
target = target.long() # 整数化标签
# target = t.zeros(target.shape[0], 10).scatter(1, target.unsqueeze(1), 1.0) # 对标签进行one-hot编码
data_mean = t.mean(data, dim=0) # 求均值
data_var = t.var(data, dim=0) # 求方差
data_normalized = (data - data_mean) / t.sqrt(data_var) # 归一化
print(data_normalized, '\n', target)