Код: Выделить всё
dfx = df1[['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']]
dfy=df1['ANNUAL']
X_train, X_test, y_train, y_test = train_test_split(dfx, dfy, test_size=0.2, random_state=0)
Когда я запускаю линейную регрессию с ниже кода.
Код: Выделить всё
#create a linear regression model
regressor = LinearRegression()
# fitting the model
regressor.fit(X_train, y_train)
#predicting response
y_pred = regressor.predict(X_test)
testScore = math.sqrt(mean_squared_error(y_test,y_pred))
print('testScore: %.2f RMSE' % (testScore))
print('R2 SCORE:', r2_score(y_test, y_pred))
Поэтому, чтобы получить лучший результат, я выполнил регуляризацию L1 и L2 с помощью кода.< /p>
Код: Выделить всё
##create a Ridge model
rdf = Ridge(alpha = .5)
## create Lasso model
lrf = Lasso(alpha = .5)
Для гребня i гор RMSE 0,11 и 0,13 для Лассо.
И снова, чтобы улучшить модель, я добавил настройку гиперпараметров в Ridge и Lasso.
Для Ridge:
Код: Выделить всё
# creating a dictionary containing potential values of alpha
alpha_values = {'alpha':[0.0001,0.001, 0.01,0.02,0.03,0.04, 0.05, 0.06, 0.08, 1, 2, 3, 5, 8, 10, 20, 50, 100]}
# Passing in a Ridge estimator, potential alpha values, scoring method and cross validation parameters to the GridSearchCV
ridge= GridSearchCV(Ridge(), alpha_values, cv=10 )
# Fitting the model to the data and extracting best value of alpha
print('The best value of alpha is:',ridge.fit(X_train, y_train).best_params_)
Как я могу улучшить эти модели
Подробнее здесь: https://stackoverflow.com/questions/690 ... -in-python