【问题标题】:How can I ensure that I'm getting the correct results from this regression generator?我怎样才能确保我从这个回归生成器中得到正确的结果?
【发布时间】:2017-09-15 03:27:58
【问题描述】:

我编写了一个简单的脚本来生成和回归随机样本数据:

import matplotlib.pyplot as plt
import numpy as np
import random
import sklearn.datasets
import sklearn.linear_model as lm
##########################################
n = np.random.randint(1,10)
b = np.random.randint(50,200)
X1_, Y1_ = sklearn.datasets.make_regression(n_samples=100, n_features=1, noise=n, bias=b)
X1 = X1_.reshape(len(X1_), 1)
Y1 = Y1_.reshape(len(Y1_), 1)
##########################################
x = np.array(X1)
y = np.array(Y1)
##########################################
lr = lm.LinearRegression()
lr.fit(x, y)
td = np.arange(1, 101, 1).reshape(100, 1)
n_y = lr.predict(td)
##########################################
f, ax = plt.subplots(1, 2, sharey=True)
ax[0].scatter(x, y)
ax[0].set_xlim([-4, 4])
ax[0].set_title("x, y")
ax[1].plot(x, n_y, 'g')
ax[1].set_xlim([-4, 4])
ax[1].set_title("x_tr, y_lr")
f.suptitle("Regression")
plt.ylim(y.min()-1, y.max()+1)
##########################################
print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("X1",  type(X1),  str(np.shape(X1)),  len(X1),   str(X1)))
print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("Y1",  type(Y1),  str(np.shape(Y1)),  len(Y1),   str(Y1)))
print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("x",   type(x),   str(np.shape(x)),   len(x),    str(x)))
print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("y",   type(y),   str(np.shape(y)),   len(y),    str(y)))
print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("td",  type(td),  str(np.shape(td)),  len(td),   str(td)))
print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("n_y", type(n_y), str(np.shape(n_y)), len(n_y),  str(n_y)))
##########################################
plt.show()

虽然它似乎工作正常,没有错误,但我仍然担心准确性:回归线总是充满随机角度,而且形状奇怪。我该如何测试呢?是否有任何我应该注意的错误报告功能?

【问题讨论】:

    标签: python machine-learning scikit-learn regression precision-recall


    【解决方案1】:

    您观察到的是因为您的数据是随机的。回归本质上是恢复生成数据的分布,因此具有讽刺意味的是,您试图恢复随机生成器的分布,而随机生成器本质上是试图隐藏其分布。

    如果你想测试回归方法,你应该使用互联网上一些流行的机器学习数据集。例如:UCI ML 数据集集合(回归任务的过滤器):http://archive.ics.uci.edu/ml/datasets.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 2016-06-15
      • 2021-09-24
      • 2022-01-06
      • 1970-01-01
      相关资源
      最近更新 更多