我正在尝试从scikit实现SDGregressor -学习如何解决简单的线性回归问题,但是我的代码每次都给出了RMSLE的不同值?
我想知道为什么会这样?另外,我想知道如何获得RMSLE的最小值?
from sklearn import linear_model
from sklearn.metrics import mean_squared_error
from math import sqrt
import math
import matplotlib.pyplot as plt
#load data
train = pd.read_csv('Train.csv')
test = pd.read_csv('Test.csv')
#split data
x_train = train.GrLivArea[:1000].values.reshape(-1,1)
y_train = train.SalePrice[:1000].values.reshape(-1,1)
x_train_normal = np.log(x_train)
y_train_normal = np.log(y_train) #Normalization
x_test = train.GrLivArea[1000:].values.reshape(-1,1)
y_test = train.SalePrice[1000:].values.reshape(-1,1)
x_test_normal = np.log(x_test)
y_test_normal = np.log(y_test) # Normalization
y_test_transform = np.exp(y_test_normal)
Model = linear_model.SGDRegressor()
Model.n_iter = np.ceil(10**7 / len(y_train_normal))
Model.fit(x_train_normal,y_train_normal)
Sale_Prices_Predicted = Model.predict(x_test_normal)
Sale_Prices_Prediceted_Transform = np.exp(Sale_Prices_Predicted)
rmslee = rmsle(y_test_transform, Sale_Prices_Prediceted_Transform)
print("RMSLE: ", rmslee)例如:
0.28153047299638045
0.28190513681658363
0.28207666380009233
0.28126007334118047发布于 2018-01-28 19:00:55
非常简单,每次初始化SGDRegessor的方式都不一样。如果您想要有可重复的结果,您需要修复种子。
不同的随机初始化结果略有不同。机器学习中非常常见的情况。
对于任意初始化的任何类型的模型,都可以出现此行为:
文档SGDRegressor:model.SGDRegressor.html
random_state : int,RandomState实例或无,可选(default=None) 伪随机数生成器的种子,用于对数据进行洗牌。如果int,random_state是随机数生成器使用的种子;如果是RandomState实例,则random_state是随机数生成器;如果没有,则随机数生成器是np.random使用的RandomState实例。
https://stackoverflow.com/questions/48490157
复制相似问题