python人工智能human learn绘图可创建机器学习模型


Posted in Python onNovember 23, 2021

如今,数据科学家经常给带有标签的机器学习模型数据,以便它可以找出规则。

这些规则可用于预测新数据的标签。

python人工智能human learn绘图可创建机器学习模型

这很方便,但是在此过程中可能会丢失一些信息。也很难知道引擎盖下发生了什么,以及为什么机器学习模型会产生特定的预测。

除了让机器学习模型弄清楚所有内容之外,还有没有一种方法可以利用我们的领域知识来设置数据标记的规则?

python人工智能human learn绘图可创建机器学习模型

是的,这可以通过 human-learn 来完成。

什么是 human-learn

human-learn 是一种工具,可让你使用交互式工程图和自定义模型来设置数据标记规则。在本文中,我们将探索如何使用 human-learn 来创建带有交互式图纸的模型。

安装 human-learn

pip install human-learn

我将使用来自sklearn的Iris数据来展示human-learn的工作原理。

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import pandas as pd 
# Load data
X, y = load_iris(return_X_y=True, as_frame=True)
X.columns = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']
# Train test split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)
# Concatenate features and labels of the training data
train = pd.concat([X_train, pd.DataFrame(y_train)], axis=1)
train

python人工智能human learn绘图可创建机器学习模型

互动绘图

human-learn 允许你绘制数据集,然后使用工程图将其转换为模型。 为了演示这是如何有用的,想象一下如何创建数据集的散点图,如下所示:

python人工智能human learn绘图可创建机器学习模型

查看上面的图时,你会看到如何将它们分成3个不同的区域,如下所示:

python人工智能human learn绘图可创建机器学习模型

但是,可能很难将图形编写为规则并将其放入函数中,human-learn的交互式绘图将派上用场。

from hulearn.experimental.interactive import InteractiveCharts
charts = InteractiveCharts(train, labels='target')
charts.add_chart(x='sepal_length', y='sepal_width')

– 动图01

绘制方法:使用双击开始绘制多边形。然后单击以创建多边形的边。再次双击可停止绘制当前多边形。

我们对其他列也做同样的事情:

charts.add_chart(x='petal_length', y='petal_width')

python人工智能human learn绘图可创建机器学习模型

创建模型并进行预测

一旦完成对数据集的绘制,就可以使用以下方法创建模型:

from hulearn.classification import InteractiveClassifier
model = InteractiveClassifier(json_desc=charts.data())
preds = model.fit(X_train, y_train).predict_proba(X_train)
print(preds.shape) # Output: (150, 3)

cool! 我们将工程图输入InteractiveClassifier类,使用类似的方法来拟合sklearn的模型,例如fit和predict_proba。

让我们来看看pred的前5行:

print('Classes:', model.classes_)
print('Predictions:\n', preds[:5, :])
"""Output
Classes: [1, 2, 0]
Predictions:
 [[5.71326574e-01 4.28530630e-01 1.42795945e-04]
 [2.00079952e-01 7.99720168e-01 1.99880072e-04]
 [2.00079952e-01 7.99720168e-01 1.99880072e-04]
 [2.49812641e-04 2.49812641e-04 9.99500375e-01]
 [4.99916708e-01 4.99916708e-01 1.66583375e-04]]
"""

需要说明的是,predict_proba给出了样本具有特定标签的概率。 例如,[5.71326574e-01 4.28530630e-01 1.42795945e-04]的第一个预测表示样本具有标签1的可能性为57.13%,样本具有标签2的可能性为42.85%,而样本为标签2的可能性为0.014% 该样本的标签为0。

预测新数据

# Get the first sample of X_test
new_sample = new_sample = X_test.iloc[:1]
# Predict
pred = model.predict(new_sample)
real = y_test[:1]
print("The prediction is", pred[0])
print("The real label is", real.iloc[0])

解释结果

为了了解模型如何根据该预测进行预测,让我们可视化新样本。

def plot_prediction(prediction: int, columns: list):
    """Plot new sample
    Parameters
    ----------
    prediction : int
        prediction of the new sample
    columns : list
        Features to create a scatter plot 
    """    
    index = prediction_to_index[prediction] 
    col1, col2 = columns    
    plt.figure(figsize=(12, 3))
    plt.scatter(X_train[col1], X_train[col2], c=preds[:, index])
    plt.plot(new_sample[col1], new_sample[col2], 'ro', c='red', label='new_sample')    
    plt.xlabel(col1)
    plt.ylabel(col2)
    plt.title(f"Label {model.classes_[index]}")
    plt.colorbar()
    plt.legend()

使用上面的函数在petal_length和petal_width绘图上绘制一个新样本,该样本的点被标记为0的概率着色。

plot_prediction(0, columns=['petal_length', 'petal_width'])

python人工智能human learn绘图可创建机器学习模型

其他列也是如此,我们可以看到红点位于具有许多黄点的区域中! 这就解释了为什么模型预测新样本的标签为0。这很酷,不是吗?

预测和评估测试数据

现在,让我们使用该模型来预测测试数据中的所有样本并评估其性能。 开始使用混淆矩阵进行评估:

from sklearn.metrics import confusion_matrix, f1_score
predictions = model.predict(X_test)
confusion_matrix(y_test, predictions, labels=[0,1,2])
array([[13,  0,  0],
       [ 0, 15,  1],
       [ 0,  0,  9]])

我们还可以使用F1分数评估结果:

f1_score(y_test, predictions, average='micro')

结论

刚刚我们学习了如何通过绘制数据集来生成规则来标记数据。 这并不是说你应该完全消除机器学习模型,而是在处理数据时加入某种人工监督。

以上就是python人工智能human learn绘图可创建机器学习模型的详细内容,更多关于human learn绘图创建机器学习模型的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python实现多线程网页下载器
Apr 15 Python
python实现NB-IoT模块远程控制
Jun 20 Python
Python中pandas dataframe删除一行或一列:drop函数详解
Jul 03 Python
Python爬虫PyQuery库基本用法入门教程
Aug 04 Python
python try except 捕获所有异常的实例
Oct 18 Python
python实时获取外部程序输出结果的方法
Jan 12 Python
python 矢量数据转栅格数据代码实例
Sep 30 Python
Python自动采集微信联系人的实现示例
Feb 28 Python
Python 高效编程技巧分享
Sep 10 Python
解决pip安装的第三方包在PyCharm无法导入的问题
Oct 15 Python
最新PyCharm从安装到PyCharm永久激活再到PyCharm官方中文汉化详细教程
Nov 17 Python
python与idea的集成的实现
Nov 20 Python
利用Python实现Picgo图床工具
Nov 23 #Python
python turtle绘图命令及案例
python机器学习Github已达8.9Kstars模型解释器LIME
如何在python中实现ECDSA你知道吗
Python jiaba库的使用详解
Nov 23 #Python
python 中的jieba分词库
Nov 23 #Python
python周期任务调度工具Schedule使用详解
Nov 23 #Python
You might like
php pki加密技术(openssl)详解
2013/07/01 PHP
php curl选项列表(超详细)
2013/07/01 PHP
ThinkPHP模板引擎之导入资源文件方法详解
2014/06/18 PHP
php短网址和数字之间相互转换的方法
2015/03/13 PHP
laravel框架中表单请求类型和CSRF防护实例分析
2019/11/23 PHP
javascript分页代码(当前页码居中)
2012/09/20 Javascript
Angularjs制作简单的路由功能demo
2015/04/14 Javascript
jQuery实现tab选项卡效果的方法
2015/07/08 Javascript
jQuery 选择器(61种)整理总结
2016/09/26 Javascript
js判断输入框不能为空格或null值的实现方法
2018/03/02 Javascript
D3.js实现拓扑图的示例代码
2018/06/30 Javascript
js实现随机8位验证码
2020/07/24 Javascript
jQuery HTML css()方法与css类实例详解
2020/05/20 jQuery
你不知道的SpringBoot与Vue部署解决方案
2020/11/09 Javascript
Vue2.x-使用防抖以及节流的示例
2021/03/02 Vue.js
python实现哈希表
2014/02/07 Python
python网络编程学习笔记(二):socket建立网络客户端
2014/06/09 Python
Python 爬虫模拟登陆知乎
2016/09/23 Python
python输入错误密码用户锁定实现方法
2017/11/27 Python
PyTorch上实现卷积神经网络CNN的方法
2018/04/28 Python
Python实现的tcp端口检测操作示例
2018/07/24 Python
Python变量类型知识点总结
2019/02/18 Python
Python基于滑动平均思想实现缺失数据填充的方法
2019/02/21 Python
python 通过手机号识别出对应的微信性别(实例代码)
2019/12/22 Python
基于pytorch padding=SAME的解决方式
2020/02/18 Python
Python更换pip源方法过程解析
2020/05/19 Python
python实现每天自动签到领积分的示例代码
2020/08/18 Python
python Gabor滤波器讲解
2020/10/26 Python
Python根据URL地址下载文件并保存至对应目录的实现
2020/11/15 Python
Pyecharts 中Geo函数常用参数的用法说明
2021/02/01 Python
使用Python webdriver图书馆抢座自动预约的正确方法
2021/03/04 Python
html5实现多图片预览上传及点击可拖拽控件
2018/03/15 HTML / CSS
美国在线面料商店:Fashion Fabrics Club
2020/01/31 全球购物
学校领导班子群众路线整改措施
2014/09/16 职场文书
美术教师个人工作总结
2015/02/06 职场文书
卫生院艾滋病宣传活动总结
2015/05/09 职场文书