我的代码:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import SGDRegressor
alpha_lst = [0.0001,1,100]
outlier = [(0,2),(21, 13), (-23, -15), (22,14), (23, 14)]
for i in range(len(alpha_lst)):
plt.figure(figsize = (17,14))
k = 0
X= b * np.sin(phi)
Y= a * np.cos(phi)
for j in outlier:
plt.subplot(3,5,k+1)
k+=1
X = np.append(X,j[0]).reshape(-1,1)
Y = np.append(Y,j[1]).reshape(-1,1)
clf = SGDRegressor(alpha=alpha_lst[i], eta0=0.001, learning_rate='constant',random_state=0)
clf.fit(X,Y)
coef = clf.coef_
intercept = clf.intercept_
y_min = np.amin(X)
y_max = np.amax(X)
hyper_plane = draw_hyper_plane(coef,intercept,y_min,y_max)
plt.scatter(X,Y,color='blue')
plt.show()
我的绘图功能:
def draw_hyper_plane(coef,intercept,y_max,y_min):
points=np.array([[((-coef*y_min - intercept)/coef), y_min],[((-coef*y_max - intercept)/coef), y_max]])
plt.plot(points[:,0], points[:,1])
实际输出:
所需的输出:
我的问题:
如何修改代码以获得所需的输出?
离群值对超平面位置有什么影响?
答案 0 :(得分:0)
您可以删除绘图功能,而只需创建select c.name, b.number, b.start_date
from customer c
left join booking b
on b.customer_id = c.id
and b.start_date = (
select b1.start_date
from booking b1
where b1.customer_id = b.customer_id
order by abs(timestampdiff(second, now(), b1.start_date))
limit 1
)
。之后,使用y_pred = clf.predict(X)
,它将绘制所需的超平面。
答案 1 :(得分:0)
您可以尝试以下代码:
hypers = [0.001,1,100]
plt.figure(figsize = (20,16))
for j,lr in enumerate(hypers):
outlier_points = [(0,2),(21, 13), (-23, -15), (22,14), (23, 14)]
X = b * np.sin(phi)
Y = a * np.cos(phi)
for c,k in enumerate(range(5*j+1, 5*(j+1)+1)):
X= np.append(X,outlier_points[c][0])
Y= np.append(Y,outlier_points[c][1])
#training the model afte updating the outliers
clf = SGDRegressor(alpha = lr, random_state=12)
clf.fit(X.reshape(-1,1), Y)
Y_pred =clf.predict(X.reshape(-1,1))
plt.subplot(4,5,k)
plt.scatter(X,Y)
plt.plot(X,Y_pred, color ='red')
plt.title(str(lr))
plt.show()