0
我想在第1列的一個要素的數據集上擬合模型,在第0列附加一個向量。無論我嘗試什麼,曲線對數據的擬合都很差。線性迴歸模型擬合不佳
這是代碼。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
col = ['id','ri','na','mg','al','si','k','ca','ba','fe','glass_type']
data = pd.read_csv('glass.data', names=col, index_col='id')
x = np.array(data)[:, 0]
x = x.reshape(np.size(x), 1)
y = np.array(data)[:, 3]
y = y.reshape(np.size(y), 1)
# initialising
m = np.size(x)
# appending ones vector in x
one = np.ones([m, 1], dtype=float)
x1 = np.append(one, x, axis=1)
# weight matrix
theta = np.zeros([2, 1])
i_list = []
j_l = []
error = np.zeros([m, 1])
# gradient descent
for i in range(3500):
h = x1.dot(theta)
error = h - y
theta = theta - (0.0001/m) * np.sum(x1.T.dot(error)) + (1.5/m) * np.sum(np.sum((theta[:, 1:2])**2))
i_list.append(i)
j = (1/(2*m)) * np.sum((h-y)**2)
j_l.append(j)
# plotting
plt.subplot(1, 2, 1)
plt.plot(x, y, '.r')
plt.plot(x, x1.dot(theta), '-b')
plt.subplot(1,2, 2)
plt.plot(i_list, j_l, '-g')
plt.show()
This how the data fits (image) 請建議我如何改善它。謝謝:)
這數據看起來並不像一個線可以適應它。它似乎幾乎沒有關係,它圍繞着這個中心點。嘗試更改您要放入的參數或迴歸模型。 – Primusa
@primusa我使用scikit學習適合數據,它很適合它。 – quantumbiker