簡體   English   中英

有監督的機器學習,產生訓練有素的估計器

[英]Supervised Machine Learning, producing a trained estimator

我有一個作業,應該使用scikit,numpy和pylab執行以下操作:

“以下所有內容均應使用所提供的training_data.csv文件中的數據。training_data為您提供了一組帶標記的整數對,代表兩個運動隊的得分,並帶有標簽。

編寫以下函數:

plot_scores()應該繪制數據的散點圖。

預測(數據集)應產生訓練有素的估算器,以猜測導致給定得分的運動(從我們保留的數據集中,將作為1000 x 2 np數組輸入)。 您可以使用scikit中的任何算法。

可選的附加功能“預處理”將在傳遞數據集進行預測之前對其進行處理。

到目前為止,這是我所做的:

import numpy as np
import scipy as sp
import pylab as pl
from random import shuffle

def plot_scores():
    k=open('training_data.csv')
    lst=[]
    for triple in k:
        temp=triple.split(',')
        lst.append([int(temp[0]), int(temp[1]), int(temp[2][:1])])
    array=np.array(lst)
    pl.scatter(array[:,0], array[:,1])
    pl.show()

def preprocess(dataset):
    k=open('training_data.csv')
    lst=[]
    for triple in k:
        temp=triple.split(',')
        lst.append([int(temp[0]), int(temp[1]), int(temp[2][:1])])
    shuffle(lst)
    return lst

在預處理過程中,我重新整理了數據,因為我應該使用其中的一些數據進行訓練,並使用其中的一些數據進行測試,但是原始數據完全不是隨機的。 我的問題是,我應該如何在預測(數據集)中“產生訓練有素的估計量”? 這應該是返回另一個函數的函數嗎? 以及哪種算法最適合根據如下所示的數據集進行分類: 在此處輸入圖片說明

該任務可能希望您訓練標准的scikit分類器模型並返回它,例如

from sklearn.svm import SVC
def predict(dataset):
    X = ... # features, extract from dataset
    y = ... # labels, extract from dataset
    clf = SVC() # create classifier
    clf.fit(X, y) # train
    return clf

盡管從函數的名稱( predict )來看,您應該檢查它是否真的要返回經過訓練的分類器或返回給定dataset參數的預測,因為這會更典型。

作為分類器,您基本上可以使用任何您喜歡的人。 您的圖看起來像您的數據集是線性可分離的(這些類沒有顏色,但是我假設污點是這兩個類)。 在線性可分離數據上,幾乎沒有任何事情會失敗。 嘗試使用SVM,邏輯回歸,隨機森林,朴素貝葉斯... ...為了獲得更多樂趣,您可以嘗試繪制決策邊界,請參閱此處 (其中也包含可用分類器的概述)。

我建議您看一下這種結構:

from random import shuffle
import matplotlib.pyplot as plt
# import a classifier you need


def get_data():
    # open your file and parse data to prepare X as a set of input vectors and Y as a set of targets
    return X, Y


def split_data(X, Y):
    size = len(X)
    indices = range(size)
    shuffle(indices)
    train_indices = indices[:size/2]
    test_indices = indices[size/2:]
    X_train = [X[i] for i in train_indices]
    Y_train = [Y[i] for i in train_indices]
    X_test = [X[i] for i in test_indices]
    Y_test = [Y[i] for i in test_indices]
    return X_train, Y_train, X_test, Y_test


def plot_scatter(Y1, Y2):
    plt.figure()
    plt.scatter(Y1, Y2, 'bo')
    plt.show()


# get data
X, Y = get_data()

# split data
X_train, Y_train, X_test, Y_test = split_data(X, Y)

# create a classifier as an object
classifier = YourImportedClassifier()

# train the classifier, after that the classifier is the trained estimator you need
classifier.train(X_train, Y_train) # or .fit(X_train, Y_train) or another train routine

# make a prediction
Y_prediction = classifier.predict(X_test)

# plot the scatter
plot_scatter(Y_prediction, Y_test)

我認為您正在尋找的是clf.fit()函數,而不是創建產生另一個函數的函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM