python - 如何在 Python 中纠正超平面的位置?
问题描述
我的代码:
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])
实际输出:
期望的输出:
我的问题:
如何修改我的代码以获得所需的输出?
异常值对超平面位置的影响是什么?
什么参数影响平面的位置?
解决方案
那么你可以试试这个代码:
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()
推荐阅读
- java - 为什么 Override 方法仍然可以有父方法的注释?
- java - 如何从 Java 中的 arraylist api 响应中检索 jsonObjects
- php - Laravel 问题没有在“包”命名空间中定义命令,在 $ php artisan config:clear 之后
- swift - 带有 UIBezierPath 的分区栏
- pytorch - PyTorch RuntimeError:CUDA 内存不足。尝试分配 14.12 GiB
- racket - 可以在 PLT-Redex 中实现 let* like 表达式吗?
- flutter - Flutter Listview.builder 仅在我从其他屏幕弹出时复制其数据来自 firebase 的项目,否则它工作正常
- reactjs - React,设置新的状态值但不直接应用
- php - 每 1 小时更新 MySQL 表中大约 100 万行
- javascript - 如何减小电子角度应用程序的大小