品牌网站建设 蝌蚪小8,艺术学院网站建设管理办法,北京企业,工程建设国家标准网站博主前期相关的博客可见下#xff1a; 机器学习项目实战-能源利用率 Part-1#xff08;数据清洗#xff09; 机器学习项目实战-能源利用率 Part-2#xff08;探索性数据分析#xff09; 这部分进行的特征工程与特征筛选。
三 特征工程与特征筛选
一般情况下我们分两步走…博主前期相关的博客可见下 机器学习项目实战-能源利用率 Part-1数据清洗 机器学习项目实战-能源利用率 Part-2探索性数据分析 这部分进行的特征工程与特征筛选。
三 特征工程与特征筛选
一般情况下我们分两步走特征工程与特征筛选
特征工程 概括性来说就是尽可能的多在数据中提取特征各种数值变换特征组合分解等各种手段齐上阵。
特征选择 就是找到最有价值的那些特征作为我们模型的输入但是之前做了那么多可能有些是多余的有些还没被发现所以这俩阶段都是一个反复在更新的过程。比如我在建模之后拿到了特征重要性这就为特征选择做了参考有些不重要的我可以去掉那些比较重要的我还可以再想办法让其做更多变换和组合来促进我的模型。所以特征工程并不是一次性就能解决的需要通过各种结果来反复斟酌。
3.1 特征变换 与 One-hot encode 有点像分析特征之间的相关性 features data.copy()
numeric_subset data.select_dtypes(number)
for col in numeric_subset.columns:if col score:nextelse:numeric_subset[log_ col] np.log(abs(numeric_subset[col]) 0.01)categorical_subset data[[Borough, Largest Property Use Type]]
categorical_subset pd.get_dummies(categorical_subset)features pd.concat([numeric_subset, categorical_subset], axis 1)
features.shape这段代码的目的是为了生成特征矩阵 features它包含了原始数据 data 的数值特征和分类特征的处理结果。
首先代码复制了原始数据 data并将其赋值给 features。
接下来通过 select_dtypes(number) 选择了 data 中的数值类型的列并将结果存储在 numeric_subset 中。
然后使用一个循环遍历 numeric_subset 的列对每一列进行处理。对于列名为 ‘score’ 的列直接跳过使用 next。对于其他列将其绝对值加上一个很小的常数0.01然后取对数并将结果存储在 numeric_subset 中以 ‘log_’ 开头的列名中。
接着从 data 中选择了 ‘Borough’ 和 ‘Largest Property Use Type’ 两列作为分类特征并使用 pd.get_dummies 进行独热编码One-Hot Encoding得到它们的编码结果并将结果存储在 categorical_subset 中。
最后使用 pd.concat 将 numeric_subset 和 categorical_subset 按列方向axis1进行拼接得到最终的特征矩阵 features。
最后一行代码输出了 features 的形状行数和列数。 3.2 共线特征 在数据中Site EUI 和 Weather Norm EUI就是要考虑的目标他俩描述的基本是同一个事 plot_data data[[Weather Normalized Site EUI (kBtu/ft²), Site EUI (kBtu/ft²)]].dropna()plt.plot(plot_data[Site EUI (kBtu/ft²)], plot_data[Weather Normalized Site EUI (kBtu/ft²)], bo)
plt.xlabel(Site EUI); plt.ylabel(Weather Norm EUI)
plt.title(Weather Norm EUI vs Site EUI, R %.4f % np.corrcoef(data[[Weather Normalized Site EUI (kBtu/ft²), Site EUI (kBtu/ft²)]].dropna(), rowvarFalse)[0][1])3.3 剔除共线特征
def remove_collinear_features(x, threshold):Objective:Remove collinear features in a dataframe with a correlation coefficientgreater than the threshold. Removing collinear features can help a modelto generalize and improves the interpretability of the model.Inputs: threshold: any features with correlations greater than this value are removedOutput: dataframe that contains only the non-highly-collinear featuresy x[score]x x.drop(columns [score])corr_matrix x.corr()iters range(len(corr_matrix.columns) - 1)drop_cols []for i in iters:for j in range(i):item corr_matrix.iloc[j: (j1), (i1): (i2)] col item.columnsrow item.indexval abs(item.values) if val threshold:# print(col.values[0], |, row.values[0], |, round(val[0][0], 2))drop_cols.append(col.values[0])drops set(drop_cols)# print(drops)x x.drop(columns drops)x x.drop(columns [Weather Normalized Site EUI (kBtu/ft²), Water Use (All Water Sources) (kgal),log_Water Use (All Water Sources) (kgal),Largest Property Use Type - Gross Floor Area (ft²)])x[score] yreturn xfeatures remove_collinear_features(features, 0.6) # 阈值为0.6
features features.dropna(axis 1, how all)
print(features.shape)
features.head()
这段代码定义了一个名为 remove_collinear_features 的函数用于移除具有高相关性的特征。移除具有高相关性的特征可以帮助模型泛化并提高模型的解释性。
函数的输入参数为 x包含特征和目标变量的数据框和 threshold相关系数的阈值阈值以上的特征相关性会被移除。
首先将目标变量 score 存储在变量 y 中并将其从 x 中移除。
接下来计算特征之间的相关系数矩阵 corr_matrix。
然后使用两个嵌套的循环遍历相关系数矩阵中的元素。当相关系数的绝对值大于等于阈值时将该特征的列名添加到 drop_cols 列表中。
完成循环后将 drop_cols 转换为集合 drops以去除重复的特征列名。
然后从 x 中移除 drops 中的特征列以及其他预定义的特征列。
接下来将目标变量 y 添加回 x 中并将结果返回。
最后对更新后的特征矩阵 features 进行处理移除所有包含缺失值的列并输出其形状行数和列数并展示前几行数据。 3.4 数据集划分
no_score features[features[score].isna()]
score features[features[score].notnull()]
print(no_score.shape: , no_score.shape)
print(score.shape, score.shape)from sklearn.model_selection import train_test_split
features score.drop(columns score)
labels pd.DataFrame(score[score])
features features.replace({np.inf: np.nan, -np.inf: np.nan})
X, X_test, y, y_test train_test_split(features, labels, test_size 0.3, random_state 42)
print(X.shape)
print(X_test.shape)
print(y.shape)
print(y_test.shape)这段代码分为几个步骤 首先将特征矩阵 features 分为两部分no_score 和 score。其中no_score 是 features 中目标变量 score 为空的部分而 score 则是 features 中目标变量 score 不为空的部分。 输出 no_score 和 score 的形状行数和列数分别使用 no_score.shape 和 score.shape 打印结果。 导入 sklearn.model_selection 模块中的 train_test_split 函数。 从 score 中移除目标变量 score 列得到特征矩阵 features。 创建标签目标变量矩阵 labels其中只包含目标变量 score 列。 使用 replace 方法将 features 中的无穷大值替换为缺失值NaN。 使用 train_test_split 函数将特征矩阵 features 和标签矩阵 labels 划分为训练集和测试集。参数 test_size 设置测试集的比例为 0.3random_state 设置随机种子为 42。将划分后的结果分别存储在 X、X_test、y 和 y_test 中。 输出训练集 X、测试集 X_test、训练集标签 y 和测试集标签 y_test 的形状行数和列数分别使用 X.shape、X_test.shape、y.shape 和 y_test.shape 打印结果。
这段代码的目的是将数据集划分为训练集和测试集并准备好用于训练和评估模型的特征矩阵和标签矩阵。
3.5 建立一个Baseline 在建模之前我们得有一个最坏的打算就是模型起码得有点作用才行。 # 衡量标准: Mean Absolute Error
def mae(y_true, y_pred):return np.mean(abs(y_true - y_pred))baseline_guess np.median(y)print(The baseline guess is a score of %.2f % baseline_guess)
print(Baseline Performance on the test set: MAE %.4f % mae(y_test, baseline_guess))这段代码定义了一个衡量标准函数 mae并计算了一个基准预测结果。 mae 函数计算了预测值与真实值之间的平均绝对误差Mean Absolute Error。它接受两个参数 y_true 和 y_pred分别表示真实值和预测值。函数内部通过 np.mean(abs(y_true - y_pred)) 计算平均绝对误差并返回结果。 baseline_guess 是基准预测的结果它被设置为标签目标变量 y 的中位数。这相当于一种简单的基准方法用中位数作为所有预测的固定值。 使用 print 函数打印基准预测结果的信息。The baseline guess is a score of %.2f % baseline_guess 会输出基准预测结果的值保留两位小数。Baseline Performance on the test set: MAE %.4f % mae(y_test, baseline_guess) 会输出基准预测结果在测试集上的性能即平均绝对误差MAE保留四位小数。
这段代码的目的是计算基准预测结果并输出基准预测结果的信息以及在测试集上的性能评估使用平均绝对误差作为衡量标准。 3.6 保存数据
no_score.to_csv(data/no_score.csv, index False)
X.to_csv(data/training_features.csv, index False)
X_test.to_csv(data/testing_features.csv, index False)
y.to_csv(data/training_labels.csv, index False)
y_test.to_csv(data/testing_labels.csv, index False)Reference
机器学习项目实战-能源利用率 Part-1数据清洗
机器学习项目实战-能源利用率 Part-2探索性数据分析
机器学习项目实战-能源利用率1-数据预处理