常用的分类预测模型:逻辑回归、KNN、朴素贝叶斯、LDA、SVC、GBDT、决策树
如果需要了解相关原理的话,可以另行百度,这里只讲解训练流程,废话不多说,直接开始讲解训练流程
训练流程
一、导入训练数据
train_data = pd.read_csv('training.csv', header = True)
train_data
- 这里用的数据就是一个普通的 DataFrame 数据,样式大概如下
- 第一列不需要,后面读取数据的时候不读入即可
- 主要预测目标是最后一列 Type

二、属性和预测值编码
- 这里主要做的是数据的向量化,把非数字的其他类型编码数字
- 这里用的是 LabelEncoder
- x、y 每个 encoder 会对应着每个编码器的类型,后面解码要使用对应的解码器
- x:表示属性值
- y:表示预测值
from sklearn.preprocessing import LabelEncoder
y_encoder = LabelEncoder()
x_encoder = LabelEncoder()
x = train_data[['C', 'H', 'O', 'N', 'S', 'group', 'AImod', 'DBE', 'MZ', 'OC', 'HC', 'SC', 'NC', 'NOSC', 'DBE.C', 'DBE.O', 'location', 'sample']]
for col in ['group', 'location', 'sample']:
x[col] = x_encoder.fit_transform(x[col])
y = y_encoder.fit_transform(train_data['Type'])
三、划分训练、测试集
- 在当前数据上划分一部分为训练集和测试集
- test_size:这是测试集大小比例,可以自行修改,会影响后面使用测试集测试模型准确度,这里的设置是当前数据集测试出来较好的配置
- random_state:这个随机因素也是会影响后面模型的准确度,按需配置
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.24, random_state=3)
四、测试结果评价指标
- 这里主要输出 Precision、Recall、F1、Support 这四个关键指标
import numpy as np
from sklearn.</

最低0.47元/天 解锁文章
1027

被折叠的 条评论
为什么被折叠?



