注:数据集放在文章末尾
● LASSO —— sklearn
import numpy as np
from numpy import genfromtxt
from sklearn import linear_model# 读入数据
data = genfromtxt(r"longley.csv",delimiter=',')# 切分数据
x_data = data[1:,2:]
y_data = data[1:,1]# 创建模型
model = linear_model.LassoCV()
model.fit(x_data, y_data)# lasso系数
print(model.alpha_)
# 相关系数
print(model.coef_)
输出:
model.predict(x_data[-2,np.newaxis])
输出:
● 数据集:“longley.csv”:
"","GNP.deflator","GNP","Unemployed","Armed.Forces","Population","Year","Employed"
"1947",83,234.289,235.6,159,107.608,1947,60.323
"1948",88.5,259.426,232.5,145.6,108.632,1948,61.122
"1949",88.2,258.054,368.2,161.6,109.773,1949,60.171
"1950",89.5,284.599,335.1,165,110.929,1950,61.187
"1951",96.2,328.975,209.9,309.9,112.075,1951,63.221
"1952",98.1,346.999,193.2,359.4,113.27,1952,63.639
"1953",99,365.385,187,354.7,115.094,1953,64.989
"1954",100,363.112,357.8,335,116.219,1954,63.761
"1955",101.2,397.469,290.4,304.8,117.388,1955,66.019
"1956",104.6,419.18,282.2,285.7,118.734,1956,67.857