📌  相关文章
📜  for idx, col_name in enumerate(X_train.columns): print("The coefficient for {} is {}".format(file_name, regression_model.coef_[0][idx])) - Python (1)

📅  最后修改于: 2023-12-03 15:00:49.910000             🧑  作者: Mango

Python代码片段介绍:获取回归模型系数

这段Python代码可以用于获取线性回归模型的各个特征系数。在本例中,代码通过循环遍历X_train的每个列名,依次获取回归模型中的各个系数。最终,代码会打印每个特征的系数值。

代码解释

代码中的 enumerate() 函数可以将一个可遍历的数据对象组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环中。

format() 函数用于将传入的参数进行字符串格式化。

line11的regression_model.coef_是线性回归模型的系数,是一个二维数组,第一个维度表示模型的个数,第二个维度表示特征的个数。因为在这里只有一个模型,数量为1,所以使用regression_model.coef_[0]取出系数的数组。

返回类型

返回类型是 markdown 的文本格式

代码
for idx, col_name in enumerate(X_train.columns):
    print("The coefficient for {} is {}".format(col_name, regression_model.coef_[0][idx]))
输出结果
The coefficient for age is -0.012469350713788234
The coefficient for sex_female is 8.762584065506855
The coefficient for sex_male is -8.762584065506853
The coefficient for bmi is 0.3807106266997645
The coefficient for children_0 is -0.06605803000190659
The coefficient for children_1 is -0.946643170369065
The coefficient for children_2 is 0.2108032984623088
The coefficient for children_3 is 0.8800441822437507
The coefficient for children_4 is -0.35114697863423206
The coefficient for children_5 is -0.727399301642389
The coefficient for smoker_no is -16.907467151888124
The coefficient for smoker_yes is 16.907467151888128
The coefficient for region_northeast is 0.29321109056761263
The coefficient for region_northwest is 0.12145285638370737
The coefficient for region_southeast is -0.15412187246409794
The coefficient for region_southwest is -0.2605410744872277