套索和岭回归低精度问题

Lasso and Ridge Regression Low-Accuracy Problem

我在我的森林火灾样本数据集上应用套索回归和岭回归,但是我的准确度太低,我应该达到

我已经尝试更改 alpha 和训练集值

#Kütüphaneleri importladım
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
#Dosyami yukledim
forest = pd.read_csv('forestfires.csv')
#Coulmn ve row feaute adlarimi duzenledim
forest.month.replace(('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'),(1,2,3,4,5,6,7,8,9,10,11,12), inplace=True)
forest.day.replace(('mon','tue','wed','thu','fri','sat','sun'),(1,2,3,4,5,6,7), inplace=True)
# iloc indeksin sırasıyla, loc indeksin kendisiyle işlem yapmaya olanak verir.Burada indeksledim
X = forest.iloc[:,0:12].values
y = forest.iloc[:,12].values
# 30 -70 olarak train test setlerimi ayirdim
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=3)
#x-y axis trainler arasina linear regressyon kurdum
lr = LinearRegression()
lr.fit(X_train, y_train)
#ridge regression modeli kurdum
rr = Ridge(alpha=0.01)
rr.fit(X_train, y_train)

rr100 = Ridge(alpha=100)
rr100.fit(X_train, y_train)
#lasso regression icin modelledim
train_score = lr.score(X_train, y_train)
test_score = lr.score(X_test, y_test)

Ridge_train_score = rr.score(X_train, y_train)
Ridge_test_score = rr.score(X_test, y_test)

Ridge_train_score100 = rr100.score(X_train, y_train)
Ridge_test_score100 = rr100.score(X_test, y_test)

print("linear regression train score:", train_score)
print("linear regression test score:", test_score)
print('ridge regression train score low score: %.2f' % Ridge_train_score)
print('ridge regression test score low score: %.2f' % Ridge_test_score)
print('ridge regression train score high score: %.2f' % Ridge_train_score100)
print('ridge regression test score high score: %.2f' % Ridge_test_score100)

考虑到您的问题:我没有在您的代码中看到任何 Lasso 回归。尝试一些 LassoCVElasticNetCV(l1_ratio=[.1, .5, .7, .9, .95, .99, 1]) 始终是找到合理 alpha 值的良好开端。对于 Ridge,RidgeCV 是 CV 算法。与 LassoCVElasticNetCV 相比,RidgeCV 使用 LOO-CV AND 采用一组固定的 alpha 值,因此它需要更多的用户-处理以获得最佳输出。以下面给定的代码示例为例:

import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LassoCV, ElasticNetCV
from sklearn.linear_model import Ridge, RidgeCV

forest = pd.read_csv('forestfires.csv')
#Coulmn ve row feaute adlarimi duzenledim
forest.month.replace(('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'),(1,2,3,4,5,6,7,8,9,10,11,12), inplace=True)
forest.day.replace(('mon','tue','wed','thu','fri','sat','sun'),(1,2,3,4,5,6,7), inplace=True)
# iloc indeksin sırasıyla, loc indeksin kendisiyle işlem yapmaya olanak verir.Burada indeksledim
X = forest.iloc[:,0:12].values
y = forest.iloc[:,12].values
# 30 -70 olarak train test setlerimi ayirdim
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=3)
#x-y axis trainler arasina linear regressyon kurdum
lr = LinearRegression()

# The cross validation algorithms:
lasso_cv = LassoCV()  # LassoCV will try to find the best alpha for you
# ElasticNetCV will try to find the best alpha for you, for a given set of combinations of Ridge and Alpha
enet_cv = ElasticNetCV()
ridge_cv = RidgeCV()

lr.fit(X_train, y_train)

lasso_cv.fit(X_train, y_train)
enet_cv.fit(X_train, y_train)
ridge_cv.fit(X_train, y_train)

#ridge regression modeli kurdum
rr = Ridge(alpha=0.01)
rr.fit(X_train, y_train)
rr100 = Ridge(alpha=100)

现在检查找到的 alpha 值:

print('LassoCV alpha:', lasso_cv.alpha_)
print('RidgeCV alpha:', ridge_cv.alpha_)
print('ElasticNetCV alpha:', enet_cv.alpha_, 'ElasticNetCV l1_ratio:', enet_cv.l1_ratio_)
ridge_alpha = ridge_cv.alpha_
enet_alpha, enet_l1ratio = enet_cv.alpha_, enet_cv.l1_ratio_

并将新的 RdigeCV and/or ElasticNetCV 围绕这些值居中(l1_ratios <0>1 将被 ElasticNetCV):

enet_new_l1ratios = [enet_l1ratio * mult for mult in [.9, .95, 1, 1.05, 1.1]]
ridge_new_alphas = [ridge_alpha * mult for mult in [.9, .95, 1, 1.05, 1.1]]

# fit Enet and Ridge again:
enet_cv = ElasticNetCV(l1_ratio=enet_new_l1ratios)
ridge_cv = RidgeCV(alphas=ridge_new_alphas)

enet_cv.fit(X_train, y_train)
ridge_cv.fit(X_train, y_train)

这应该是为您的模型找到合适的 alpha 值 and/or l1 比率的第一步。当然,其他步骤如特征工程和选择正确的模型(f.i。套索:执行特征选择)应该先于找到好的参数。 :)