【问题标题】:What do the results on a Sci-Kit machine learning program represent?Sci-Kit 机器学习程序的结果代表什么?
【发布时间】:2019-11-28 23:03:16
【问题描述】:

我正在浏览 Google 的机器学习视频,并完成了一个程序,该程序利用数据库收集有关鲜花的信息。程序成功运行,但我对结果有误解:

from scipy.spatial import distance
def euc(a,b):
    return distance.euclidean(a, b)

class ScrappyKNN():

    def fit(self, x_train, y_train):

        self.x_train = x_train

        self.y_train = y_train

   def predict(self, x_test):

        predictions = []

        for row in x_test:

            label = self.closest(row)

            predictions.append(label)

        return predictions

   def closest(self, row):

        best_dist = euc(row, self.x_train[0])

        best_index = 0

        for i in range(1, len(self.x_train)):

            dist = euc(row, self.x_train[i])

            if dist < best_dist:

                best_dist = dist

                best_index = i

        return self.y_train[best_index]

from sklearn import datasets

iris = datasets.load_iris()

x = iris.data

y = iris.target

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x,y, test_size =.5)

print(x_train.shape, x_test.shape)

my_classifier = ScrappyKNN()

my_classifier .fit(x_train, y_train)

prediction = my_classifier.predict(x_test)



from sklearn.metrics import accuracy_score

print(accuracy_score(y_test, prediction))

结果如下: (75, 4) (75, 4) 0.96

96% 是准确率,但 75 和 4 究竟代表什么?

【问题讨论】:

    标签: python machine-learning scikit-learn


    【解决方案1】:

    您正在此行打印数据集的形状:

    print(x_train.shape, x_test.shape) 
    

    x_trainx_test 似乎各有 75 行(即数据点)和 4 列(即特征)。除非您有奇数个数据点,否则这些维度应该相同,因为您在这条线上执行 50/50 的训练/测试数据拆分:

    x_train, x_test, y_train, y_test = train_test_split(x,y, test_size =.5)
    

    【讨论】:

      【解决方案2】:

      在我看来,您正在使用euclidean metrics 从头开始​​编写K Nearest Neighour

      根据您的代码x_train, x_test, y_train, y_test = train_test_split(x,y, test_size =.5),您所做的是将traintest 数据分别拆分为50%。 sklearn train-test-split 实际上是按行拆分数据,因此特征(列数)必须相同。因此(75,4) 是您的行数,然后分别是训练集和测试集的特征数。

      现在,0.96 的准确度分数基本上意味着,在您的测试集中的 75 行中,96% 的预测正确。

      这会比较您的测试集和预测集的结果(y_pred 从prediction = my_classifier.predict(x_test) 计算得出。)

      TP, TN 是正确预测的数量,而 TP + TN + FP + FN 基本上总和为 75(您正在测试的总行数)。

      注意:在执行train-test-split 时,通常最好将数据拆分为 80/20 而不是 50/50,以提供更好的预测。

      【讨论】:

        猜你喜欢
        • 2020-10-15
        • 2013-05-23
        • 2020-07-17
        • 2014-07-16
        • 1970-01-01
        • 2017-02-05
        • 2020-02-21
        • 2013-10-13
        • 2015-02-14
        相关资源
        最近更新 更多