温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python人工智能human learn绘图怎么用

发布时间:2021-11-23 11:07:17 来源:亿速云 阅读:142 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关python人工智能human learn绘图怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

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

    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绘图怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI