-1
我寫了一個簡單的腳本生成並消退隨機抽樣數據:我如何確保從迴歸生成器中獲得正確的結果?
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()
,雖然它似乎是工作的罰款,沒有任何錯誤,我仍然關心的準確性:迴歸線總是充滿隨機角度,奇怪的形狀。我如何測試這個?是否有任何我應該注意的錯誤報告功能?
什麼是一些很好的示例? –