python人工智能human?learn繪圖創(chuàng)建機器學習模型
如今,數(shù)據(jù)科學家經(jīng)常給帶有標簽的機器學習模型數(shù)據(jù),以便它可以找出規(guī)則。
這些規(guī)則可用于預測新數(shù)據(jù)的標簽。
這很方便,但是在此過程中可能會丟失一些信息。也很難知道引擎蓋下發(fā)生了什么,以及為什么機器學習模型會產(chǎn)生特定的預測。
除了讓機器學習模型弄清楚所有內容之外,還有沒有一種方法可以利用我們的領域知識來設置數(shù)據(jù)標記的規(guī)則?
是的,這可以通過 human-learn 來完成。
什么是 human-learn
human-learn 是一種工具,可讓你使用交互式工程圖和自定義模型來設置數(shù)據(jù)標記規(guī)則。在本文中,我們將探索如何使用 human-learn 來創(chuàng)建帶有交互式圖紙的模型。
安裝 human-learn
pip install human-learn
我將使用來自sklearn的Iris數(shù)據(jù)來展示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
互動繪圖
human-learn 允許你繪制數(shù)據(jù)集,然后使用工程圖將其轉換為模型。 為了演示這是如何有用的,想象一下如何創(chuàng)建數(shù)據(jù)集的散點圖,如下所示:
查看上面的圖時,你會看到如何將它們分成3個不同的區(qū)域,如下所示:
但是,可能很難將圖形編寫為規(guī)則并將其放入函數(shù)中,human-learn的交互式繪圖將派上用場。
from hulearn.experimental.interactive import InteractiveCharts charts = InteractiveCharts(train, labels='target') charts.add_chart(x='sepal_length', y='sepal_width')
– 動圖01
繪制方法:使用雙擊開始繪制多邊形。然后單擊以創(chuàng)建多邊形的邊。再次雙擊可停止繪制當前多邊形。
我們對其他列也做同樣的事情:
charts.add_chart(x='petal_length', y='petal_width')
創(chuàng)建模型并進行預測
一旦完成對數(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類,使用類似的方法來擬合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。
預測新數(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])
解釋結果
為了了解模型如何根據(jù)該預測進行預測,讓我們可視化新樣本。
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繪圖上繪制一個新樣本,該樣本的點被標記為0的概率著色。
plot_prediction(0, columns=['petal_length', 'petal_width'])
其他列也是如此,我們可以看到紅點位于具有許多黃點的區(qū)域中! 這就解釋了為什么模型預測新樣本的標簽為0。這很酷,不是嗎?
預測和評估測試數(shù)據(jù)
現(xiàn)在,讓我們使用該模型來預測測試數(shù)據(jù)中的所有樣本并評估其性能。 開始使用混淆矩陣進行評估:
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分數(shù)評估結果:
f1_score(y_test, predictions, average='micro')
結論
剛剛我們學習了如何通過繪制數(shù)據(jù)集來生成規(guī)則來標記數(shù)據(jù)。 這并不是說你應該完全消除機器學習模型,而是在處理數(shù)據(jù)時加入某種人工監(jiān)督。
以上就是python人工智能human learn繪圖可創(chuàng)建機器學習模型的詳細內容,更多關于human learn繪圖創(chuàng)建機器學習模型的資料請關注腳本之家其它相關文章!
相關文章
Python2.7環(huán)境Flask框架安裝簡明教程【已測試】
這篇文章主要介紹了Python2.7環(huán)境Flask框架安裝方法,結合實例形式詳細分析了Python2.7環(huán)境下安裝Flask框架遇到的問題與相關解決方法、注意事項,并給出了一個基本的測試示例,需要的朋友可以參考下2018-07-07Python使用lambda拋出異常實現(xiàn)方法解析
這篇文章主要介紹了Python使用lambda拋出異常實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08詳解程序意外中斷自動重啟shell腳本(以Python為例)
這篇文章主要介紹了詳解程序意外中斷自動重啟shell腳本(以Python為例),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07淺析Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請求
這篇文章主要介紹了Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請求,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值 ,需要的朋友可以參考下2020-03-03