溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

python人工智能human?learn繪圖怎么用

發(fā)布時(shí)間:2021-11-23 11:07:17 來(lái)源:億速云 閱讀:149 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)python人工智能human learn繪圖怎么用,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

    如今,數(shù)據(jù)科學(xué)家經(jīng)常給帶有標(biāo)簽的機(jī)器學(xué)習(xí)模型數(shù)據(jù),以便它可以找出規(guī)則。

    這些規(guī)則可用于預(yù)測(cè)新數(shù)據(jù)的標(biāo)簽。

    python人工智能human?learn繪圖怎么用

    這很方便,但是在此過(guò)程中可能會(huì)丟失一些信息。也很難知道引擎蓋下發(fā)生了什么,以及為什么機(jī)器學(xué)習(xí)模型會(huì)產(chǎn)生特定的預(yù)測(cè)。

    除了讓機(jī)器學(xué)習(xí)模型弄清楚所有內(nèi)容之外,還有沒(méi)有一種方法可以利用我們的領(lǐng)域知識(shí)來(lái)設(shè)置數(shù)據(jù)標(biāo)記的規(guī)則?

    python人工智能human?learn繪圖怎么用

    是的,這可以通過(guò) human-learn 來(lái)完成。

    什么是 human-learn

    human-learn 是一種工具,可讓你使用交互式工程圖和自定義模型來(lái)設(shè)置數(shù)據(jù)標(biāo)記規(guī)則。在本文中,我們將探索如何使用 human-learn 來(lái)創(chuàng)建帶有交互式圖紙的模型。

    安裝 human-learn

    pip install human-learn

    我將使用來(lái)自sklearn的Iris數(shù)據(jù)來(lái)展示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繪圖怎么用

    互動(dòng)繪圖

    human-learn 允許你繪制數(shù)據(jù)集,然后使用工程圖將其轉(zhuǎn)換為模型。 為了演示這是如何有用的,想象一下如何創(chuàng)建數(shù)據(jù)集的散點(diǎn)圖,如下所示:

    python人工智能human?learn繪圖怎么用

    查看上面的圖時(shí),你會(huì)看到如何將它們分成3個(gè)不同的區(qū)域,如下所示:

    python人工智能human?learn繪圖怎么用

    但是,可能很難將圖形編寫為規(guī)則并將其放入函數(shù)中,human-learn的交互式繪圖將派上用場(chǎng)。

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

    – 動(dòng)圖01

    繪制方法:使用雙擊開(kāi)始繪制多邊形。然后單擊以創(chuàng)建多邊形的邊。再次雙擊可停止繪制當(dāng)前多邊形。

    我們對(duì)其他列也做同樣的事情:

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

    python人工智能human?learn繪圖怎么用

    創(chuàng)建模型并進(jìn)行預(yù)測(cè)

    一旦完成對(duì)數(shù)據(jù)集的繪制,就可以使用以下方法創(chuàng)建模型:

    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類,使用類似的方法來(lái)擬合sklearn的模型,例如fit和predict_proba。

    讓我們來(lái)看看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]]
    """

    需要說(shuō)明的是,predict_proba給出了樣本具有特定標(biāo)簽的概率。 例如,[5.71326574e-01 4.28530630e-01 1.42795945e-04]的第一個(gè)預(yù)測(cè)表示樣本具有標(biāo)簽1的可能性為57.13%,樣本具有標(biāo)簽2的可能性為42.85%,而樣本為標(biāo)簽2的可能性為0.014% 該樣本的標(biāo)簽為0。

    預(yù)測(cè)新數(shù)據(jù)

    # 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])

    解釋結(jié)果

    為了了解模型如何根據(jù)該預(yù)測(cè)進(jìn)行預(yù)測(cè),讓我們可視化新樣本。

    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()

    使用上面的函數(shù)在petal_length和petal_width繪圖上繪制一個(gè)新樣本,該樣本的點(diǎn)被標(biāo)記為0的概率著色。

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

    python人工智能human?learn繪圖怎么用

    其他列也是如此,我們可以看到紅點(diǎn)位于具有許多黃點(diǎn)的區(qū)域中! 這就解釋了為什么模型預(yù)測(cè)新樣本的標(biāo)簽為0。這很酷,不是嗎?

    預(yù)測(cè)和評(píng)估測(cè)試數(shù)據(jù)

    現(xiàn)在,讓我們使用該模型來(lái)預(yù)測(cè)測(cè)試數(shù)據(jù)中的所有樣本并評(píng)估其性能。 開(kāi)始使用混淆矩陣進(jìn)行評(píng)估:

    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分?jǐn)?shù)評(píng)估結(jié)果:

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

    關(guān)于“python人工智能human learn繪圖怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI