当前位置:网站首页>Regression analysis based on elasticnetcv

Regression analysis based on elasticnetcv

2022-06-12 18:16:00 Py little brother

Elastic network regularization (Elastic Net Rerularization) It is a method to reduce the risk of over fitting in regression analysis . The regularization of elastic networks is actually LASSO(The Least Shrinkage and Selection) The phenomenon combination of algorithm and ridge regression algorithm .LASSO Can effectively constrain L1 Norm or Manhattan distance .L1 Norm means that for two points , The sum of the absolute values of the difference between their coordinate values . For ridge regression algorithm L1 The square of the norm is taken as the penalty term .
Define a ElasticNetCV object

clf=ElasticNetCV(max_iter=200,cv=10,l1_ratio=.1)

In the above code ElasticNetCV Class uses a l1_ratio Parameters of , Its value is 0-1, if 0, Then we just use ridge regression algorithm , if 1, Only use LASSO Algorithm . This parameter can be a single value , It can also be a numerical list .
The following is a forecast analysis using the previously preprocessed rainfall data and the California house price data .
For rainfall data , The score is

Scores 0.05276796268304984

It is found that the fitting effect is not good , Under fitting phenomenon occurs . It can also be seen from the picture

For the housing price data, there are

Scores 0.5606109126805701

The complete code is as follows

"XU YONG KANG"
from sklearn.linear_model import ElasticNetCV
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing

def regress(x,y,title):
    clf=ElasticNetCV(max_iter=200,cv=10,l1_ratio=.1)
    clf.fit(x,y)
    print('Scores',clf.score(x,y))

    pred=clf.predict(x)
    plt.title('Scatter plot of prediction and '+title)
    plt.xlabel('Prediction')
    plt.ylabel('Target')
    plt.scatter(y,pred,color='red')
    plt.plot(y,y,label='Fit')
    plt.legend()
    plt.grid(True)
    plt.show()

rain=.1*np.load('rain.npy')
rain[rain<0]=0.05/2
dates=np.load('doy.npy')

x=np.vstack((dates[:-1],rain[:-1]))
y=rain[1:]
regress(x.T,y,'rain data')

housing = fetch_california_housing()
x=housing.data
y=housing.target
regress(x,y,'California house prices')
原网站

版权声明
本文为[Py little brother]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281623301958.html