线性回归未按预期工作

我通过在for循环中训练了20万次模型,获得了0.97的精度(我想这意味着97%),并将其保存到了.pickle文件中。问题是它似乎没有在学习,因为即使不训练模型,我得到的结果也是一样的,而且精度在70-90%之间。好吧,如果我得到更高的精度,我会认为它在学习,但如我所说,结果没有变化。

无论如何,即使精度在70-97%之间,它也只能正确预测大约20-45%的数据。正如你所见,我是新手,我正在按照这个教程学习:https://www.youtube.com/watch?v=3AQ_74xrch8

这是代码:

import pandas as pdimport numpy as npimport pickleimport sklearnfrom sklearn import linear_modeldata = pd.read_csv('student-mat.csv', sep=';')data = data[['G1', 'G2', 'G3', 'studytime', 'failures', 'absences']]predict = 'G3'X = np.array(data.drop([predict], 1))y = np.array(data[predict])x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.1)# comment after train the model #best_accuracy = 0array_best_accurary = []for _ in range(200000):    x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(X, y, test_size=0.1)    linear = linear_model.LinearRegression()    linear.fit(x_train, y_train)    accuracy = linear.score(x_test, y_test)    if accuracy > best_accuracy:        best_accuracy = accuracy        array_best_accurary.append(best_accuracy)        with open('student_model.pickle', 'wb') as f:            pickle.dump(linear, f)print(max(array_best_accurary), '\n')# ## uncomment after train the model# picke_in = open('student_model.pickle', 'rb')# linear = pickle.load(picke_in)print('Coeficient:\n', linear.coef_)print('Intercept:\n', linear.intercept_, '\n')predictions = linear.predict(x_test)total = len(predictions)correct_predictions = []for x in range(total):    print('Predict', predictions[x], '- Correct', y_test[x])    if int(predictions[x]) == y_test[x]:        correct_predictions.append(1)print('\n')print('Total:', total)print('Total correct predicts:', len(correct_predictions))

这是输出结果:

0.977506233512022 Coeficient: [ 0.14553549  0.98120042 -0.18857019 -0.31539844  0.03324807]Intercept: -1.3929098924365348 Predict 9.339230104273398 - Correct 9Predict -1.7999979510132014 - Correct 0Predict 18.220125096856393 - Correct 18Predict 3.5669380684894634 - Correct 0Predict 8.394034346453692 - Correct 10Predict 11.17472103817094 - Correct 12Predict 6.877027043616517 - Correct 7Predict 13.10046638328761 - Correct 14Predict 8.460530481589299 - Correct 9Predict 5.619296478409708 - Correct 9Predict 5.056861318329287 - Correct 6Predict -0.4602308511632893 - Correct 0Predict 5.4907111970972124 - Correct 7Predict 7.098301508597935 - Correct 0Predict 9.060702343692888 - Correct 11Predict 14.906413508421672 - Correct 16Predict 5.337146104521532 - Correct 7Predict 6.451206767114973 - Correct 6Predict 12.005846951225159 - Correct 14Predict 9.181910373164804 - Correct 0Predict 7.078728252841696 - Correct 8Predict 12.944012673326714 - Correct 13Predict 9.296195408827478 - Correct 10Predict 9.726422674287734 - Correct 10Predict 5.872952989811228 - Correct 6Predict 11.714775970606564 - Correct 12Predict 10.699461464343582 - Correct 11Predict 8.079501926145412 - Correct 8Predict 17.050354493553698 - Correct 17Predict 11.950269035741151 - Correct 12Predict 11.907234340295231 - Correct 12Predict 8.394034346453692 - Correct 8Predict 9.563804949756388 - Correct 10Predict 15.08795365845874 - Correct 15Predict 15.197484489040267 - Correct 14Predict 9.339230104273398 - Correct 10Predict 6.72710996076076 - Correct 8Predict 15.778083095387622 - Correct 16Predict 8.238497037369088 - Correct 9Predict 11.357208854852361 - Correct 12Total: 40Total correct predicts: 8

我知道这是一个浮点数,但即使我向上或向下取整,我仍然得不到预期的结果。我知道我的代码过于简单,但即使我考虑预测值等于(期望预测值 – 1),在上面的输出中,这将给我27个正确的预测,约占总数的60%。这不是太低了吗?我期望的是大约70-80%的正确率。

我主要的疑问是,为什么即使精度在70-97%之间,我得到的正确结果也只有大约20-45%。也许我误解了它的工作原理,有人能解释一下吗?

我使用的数据集:https://archive.ics.uci.edu/ml/datasets/Student+Performance


回答:

你的问题存在几个问题。

首先,在回归设置(如你这里的)中,我们不使用“精度”和“准确性”这两个术语,这些术语是为分类问题保留的(在分类问题中有非常具体的含义,而且它们远非同义词)。

尽管如此,你接下来的步骤是为自己澄清你的度量标准,即你的linear.score(x_test, y_test)到底返回了什么;在这里,和许多其他类似的设置一样,文档是你最好的朋友:

score(self, X, y, sample_weight=None)

返回预测的决定系数R^2。

所以,你的度量标准是决定系数R^2,或R平方。

虽然0.97的R^2值听起来相当不错(而且有时可以解释为97%,但这并不意味着“正确预测”),但在像这里的预测设置中使用这个度量标准相当有问题;引用我在另一个SO线程中的回答:

事实上,整个R平方的概念直接来自统计学世界,那里强调的是解释性模型,而在机器学习环境中,它在预测性模型中的用途很小;至少据我所知,除了非常入门的课程外,我从未(我指的是从未…)见过任何预测建模问题中使用R平方来进行任何形式的性能评估;也不是偶然,流行的机器学习介绍,如Andrew Ng的机器学习在Coursera上甚至不屑于提及它。而且,正如在上面的Github线程中所指出的(强调增加):

特别是在使用测试集时,R^2的含义对我来说有点不清楚。

我完全同意这个观点。

所以,你最好使用预测回归问题的标准度量标准之一,如均方误差(MSE)平均绝对误差(MAE) – 后者具有与你的因变量相同单位的优势;由于这两个量都是错误,所以意味着“越低越好”。看看scikit-learn中可用的回归度量标准以及如何使用它们。

最后但同样重要的是,独立于上述讨论,我看不出你是如何得出这个结果评估的:

Total: 40Total correct predicts: 8

因为,如果我们应用截断规则(即15.49截断为15,但15.51截断为16),我看到大约一半的预测确实是“正确”的…

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注