当前位置:网站首页>Kaggle(四)Scikit-learn
Kaggle(四)Scikit-learn
2022-08-03 05:10:00 【study_code_ing】
Scikit-learn是一个支持有监督和无监督学习的开源机器学习库。它还为模型拟合、数据预处理、模型选择和评估以及许多其他实用程序提供了各种工具。
一、训练和测试分类器
对于第一个示例,我们将在数据集上训练和测试一个分类器。 我们将使用此示例来回忆scikit-learn的API。
%matplotlib inline
import matplotlib.pyplot as plt1、手写数字的数据集
一共有1797个样本,每个样本有64个元素,对应到8x8像素点组成的矩阵,每个值是其灰度值
from sklearn.datasets import load_digits
X, y = load_digits(return_X_y=True)X.shape
以图像的形式显示出来
plt.imshow(X[0].reshape(8, 8), cmap='gray');# 下面完成灰度图的绘制
# 灰度显示图像
plt.axis('off')# 关闭坐标轴
print('The digit in the image is {}'.format(y[0]))# 格式化打印数字为零

2 、划分数据为训练集与测试集
划分数据为训练集与测试集,添加stratify参数,以使得训练和测试数据集的类分布与整个数据集的类分布相同。
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, test_size=0.25, random_state=42)
LogisticRegression
from sklearn.linear_model import LogisticRegression # 求出Logistic回归的精确度得分
clf = LogisticRegression(
solver='lbfgs', multi_class='ovr', max_iter=5000, random_state=42)
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
print('Accuracy score of the {} is {:.4f}'.format(clf.__class__.__name__,
accuracy))回归模型的精确度大致为0.9622
RandomForestClassifier

XGBClassifier

GradientBoostingClassifier

GradientBoostingClassifier

SVC

LinearSVC

二、标准化数据
归一化
学习(即,fit方法)训练集上的统计数据

标准化(即,transform方法)训练集和测试集
可知我们将训练和测试这个模型并得到归一化后的数据集回归模型的精确度上升
预测

归一化后的数据集


三、交叉验证
顾名思义,就是重复的使用数据,把得到的样本数据进行切分,组合为不同的训练集和测试集,用训练集来训练模型,用测试集来评估模型预测的好坏。在此基础上可以得到多组不同的训练集和测试集,某次训练集中的某样本在下次可能成为测试集中的样本,即所谓“交叉”。
交叉验证(Cross-Validation)_南有芙蕖的博客-CSDN博客_交叉验证

四、 网格搜索调参
Grid Search:一种调参手段;
穷举搜索:在所有候选的参数选择中,通过循环遍历,尝试每一种可能性,表现最好的参数就是最终的结果。其原理就像是在数组里找最大值。
机器学习之Grid Search网格搜索(自动调参)_图灵的猫i的博客-CSDN博客_gridsearch
如下例子,我们希望优化LogisticRegression分类器的C和penalty参数。
我们可以使用get_params()检查管道的所有参数。

五、流水线操作
引入了Pipeline对象。它依次连接多个转换器和分类器(或回归器)。我们可以创建一个如下管道:


六、练习 异构数据:当您使用数字以外的数据时
导入数据

将数据集转换为x,y

处理数值数据
注意:不能直接使用用LogisticRegression分类器,会报错
大多数分类器都设计用于处理数值数据。 因此,我们需要将分类数据转换为数字特征。 最简单的方法是使用OneHotEncoder对每个分类特征进行读热编码。 让我们以sex与embarked列为例。 请注意,我们还会遇到一些缺失的数据。 我们将使用SimpleImputer用常量值替换缺失值。

这样,可以对分类特征进行编码。 但是,我们也希望标准化数字特征。 因此,我们需要将原始数据分成2个子组并应用不同的预处理:
(i)分类数据的独热编;
(ii)数值数据的标准缩放(归一化)。
我们还需要处理两种情况下的缺失值:
对于分类列,我们将字符串’missing_values’替换为缺失值,该字符串将自行解释为类别。
对于数值数据,我们将用感兴趣的特征的平均值替换缺失的数据。


转换完成后,将处理好的分类特征和数值特征转换为稀疏矩阵

七、建模预测
我们使用LogisticRegression分类器作为模型
边栏推荐
- idea使用@Autowired注解爆红原因及解决方法
- Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
- 背压机制
- Exception (abnormal) and Error (error) difference analysis
- Secondary development of WinForm controls
- typescript42-readonly修饰符
- 【Harmony OS】【FAQ】Hongmeng Questions Collection 1
- Interface testing framework combat (3) | JSON request and response assertion
- 【Harmony OS】【FAQ】鸿蒙问题合集1
- Alienware上线首个数字时装AR试穿体验
猜你喜欢

typescript49-交叉类型
![[Harmony OS] [ARK UI] ETS context basic operations](/img/40/d5924477c42e2b3246eb212f4be534.png)
[Harmony OS] [ARK UI] ETS context basic operations

Build your own web page on the Raspberry Pi (2)

How to prepare for the test interface test data

MCM box model modeling method and source analysis of atmospheric O3

Harmony OS ets ArkUI 】 【 】 the development basic page layout and data connection

Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)

Detailed explanation of MOSN reverse channel

快速上手 Mockito 单元测试框架

shell脚本循环语句
随机推荐
MySql数据库
Interface testing framework of actual combat (2) | interface request assertion
Harmony OS Date ano UI 】 【 】 the basic operation
在线密码生成工具推荐
阿里云对象存储oss私有桶生成链接
JS底层手写
Where is the value of testers
Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
1094 谷歌的招聘 (20 分)
Talking about GIS Data (6) - Projected Coordinate System
【精讲】利用原生js实现todolist
接口测试框架实战(三)| JSON 请求与响应断言
Response 重写设置返回值
【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
接口测试框架实战(一) | Requests 与接口请求构造
ss-4.2 多个eureka集群案例
斐讯K2路由编译Padavan华硕固件和心得
odps的临时查询能在写sql的时候就给结果一个命名不?
Common fluorescent dyes to modify a variety of groups and its excitation and emission wavelength data in the data
Interface testing framework combat (3) | JSON request and response assertion
