ElasticNetCV in Python: Get full grid of hyperparameters with corresponding MSE?

I have fitted a ElasticNetCV in Python with three splits:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import numpy as np
from sklearn.linear_model import LinearRegression
#Sample data:
num_samples = 100 # Number of samples
num_features = 1000 # Number of features
X = np.random.rand(num_samples, num_features)
Y = np.random.rand(num_samples)
#Model
l1_ratios = np.arange(0.1, 1.1, 0.1)
tscv=TimeSeriesSplit(max_train_size=None, n_splits=3)
regr = ElasticNetCV(cv=tscv.split(X), random_state=42,l1_ratio=l1_ratios)
regr.fit(X,Y)
</code>
<code>import numpy as np from sklearn.linear_model import LinearRegression #Sample data: num_samples = 100 # Number of samples num_features = 1000 # Number of features X = np.random.rand(num_samples, num_features) Y = np.random.rand(num_samples) #Model l1_ratios = np.arange(0.1, 1.1, 0.1) tscv=TimeSeriesSplit(max_train_size=None, n_splits=3) regr = ElasticNetCV(cv=tscv.split(X), random_state=42,l1_ratio=l1_ratios) regr.fit(X,Y) </code>
import numpy as np
from sklearn.linear_model import LinearRegression

#Sample data:
num_samples = 100  # Number of samples
num_features = 1000  # Number of features 
X = np.random.rand(num_samples, num_features)
Y = np.random.rand(num_samples)

#Model
l1_ratios = np.arange(0.1, 1.1, 0.1)
tscv=TimeSeriesSplit(max_train_size=None, n_splits=3)
regr = ElasticNetCV(cv=tscv.split(X), random_state=42,l1_ratio=l1_ratios)
regr.fit(X,Y)

Now I want to get the whole grid of combinations of hyperparameters with the corresponding MSE as a Data Frame, I tried the following. However, the problem is that the resulting data frame shows the MSE for a combination of hyperparameters which are not shown as the minimum in the ElasticNetCV object which can be obtained by regr.alpha_ and regr.l1_ratio_
:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>mse_path = regr.mse_path_
alpha_path = regr.alphas_
# Reshape mse_path to have l1_ratios, n_alphas, cross_validation_step as separate columns
mse_values = mse_path.flatten()
alpha_values = alpha_path.flatten()
l1_values=np.tile(l1_ratios ,int(alpha_values.shape[0]/l1_ratios.shape[0]))
repeated_l1_ratios = np.repeat(l1_ratios, 100)
# mse has dimensions (11, 100, 3)
array_3d = mse
# Flatten the 3D array into a 2D array
# Each sub-array of shape (100, 3) becomes a row in the new 2D array
array_2d = array_3d.reshape(-1, 3)
# Create a DataFrame from the 2D array
df = pd.DataFrame(array_2d, columns=['MSE Split1', 'MSE Split2', 'MSE Split3'])
df['alpha_values'] = alpha_values
df['l1_values'] = repeated_l1_ratios
</code>
<code>mse_path = regr.mse_path_ alpha_path = regr.alphas_ # Reshape mse_path to have l1_ratios, n_alphas, cross_validation_step as separate columns mse_values = mse_path.flatten() alpha_values = alpha_path.flatten() l1_values=np.tile(l1_ratios ,int(alpha_values.shape[0]/l1_ratios.shape[0])) repeated_l1_ratios = np.repeat(l1_ratios, 100) # mse has dimensions (11, 100, 3) array_3d = mse # Flatten the 3D array into a 2D array # Each sub-array of shape (100, 3) becomes a row in the new 2D array array_2d = array_3d.reshape(-1, 3) # Create a DataFrame from the 2D array df = pd.DataFrame(array_2d, columns=['MSE Split1', 'MSE Split2', 'MSE Split3']) df['alpha_values'] = alpha_values df['l1_values'] = repeated_l1_ratios </code>
mse_path =  regr.mse_path_
alpha_path = regr.alphas_

# Reshape mse_path to have l1_ratios, n_alphas, cross_validation_step as separate columns
mse_values = mse_path.flatten()
alpha_values = alpha_path.flatten()
l1_values=np.tile(l1_ratios ,int(alpha_values.shape[0]/l1_ratios.shape[0]))
repeated_l1_ratios = np.repeat(l1_ratios, 100)

# mse has dimensions (11, 100, 3)
array_3d = mse

# Flatten the 3D array into a 2D array
# Each sub-array of shape (100, 3) becomes a row in the new 2D array
array_2d = array_3d.reshape(-1, 3)

# Create a DataFrame from the 2D array
df = pd.DataFrame(array_2d, columns=['MSE Split1', 'MSE Split2', 'MSE Split3'])

df['alpha_values'] = alpha_values
df['l1_values'] = repeated_l1_ratios

The following then results in a hyperparameter combination that is not the true one. So when combining the MSEs and the hyperparameter values, something is wrong:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code># Calculate the minimum MSE for each row across the three splits
df['Min MSE'] = df[['MSE Split1', 'MSE Split2', 'MSE Split3']].min(axis=1)
# Identify the row with the overall minimum MSE
min_mse_row_index = df['Min MSE'].idxmin()
# Retrieve the row with the minimum MSE
min_mse_row = df.loc[min_mse_row_index]
print("Row with the minimum MSE across all splits:")
print(min_mse_row)
</code>
<code># Calculate the minimum MSE for each row across the three splits df['Min MSE'] = df[['MSE Split1', 'MSE Split2', 'MSE Split3']].min(axis=1) # Identify the row with the overall minimum MSE min_mse_row_index = df['Min MSE'].idxmin() # Retrieve the row with the minimum MSE min_mse_row = df.loc[min_mse_row_index] print("Row with the minimum MSE across all splits:") print(min_mse_row) </code>
# Calculate the minimum MSE for each row across the three splits
df['Min MSE'] = df[['MSE Split1', 'MSE Split2', 'MSE Split3']].min(axis=1)

# Identify the row with the overall minimum MSE
min_mse_row_index = df['Min MSE'].idxmin()

# Retrieve the row with the minimum MSE
min_mse_row = df.loc[min_mse_row_index]

print("Row with the minimum MSE across all splits:")
print(min_mse_row)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật