SVM(以及大多数其他ML方法)以二维数字特征矩阵的形式接受输入,因此您必须将数据转换为该格式甚至使用SVM。所以,虽然你想做 一些 在参数调整之前进行特征工程以确认您的管道以您认为应该的方式工作,您不一定需要将两者完全分开。
如果使用自动或参数化特征工程方法,则该方法可以成为超参数调整过程的一部分。
一种方法是使用 Featuretools ,一个Python中的开源自动化特征工程库,与机器学习库一起使用 Scikit-学习 。
这是一个使用Featuretools中的演示数据集的管道,它在同一步骤中执行超参数调整和特征工程:
import featuretools as ft from featuretools.primitives import (Sum, Max, Mean, Min, Percentile, Day, Weekend, Weekday) from featuretools.selection import remove_low_information_features from itertools import combinations from sklearn.metrics import f1_score from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelEncoder from sklearn.preprocessing import StandardScaler, Imputer retail_data = ft.demo.load_retail(nrows=1000) # predict each customer's country labels = LabelEncoder().fit_transform(retail_data['customers'].df['Country']) def score_pipeline(max_depth, agg_primitives, trans_primitives, C): feature_matrix, feature_defs = ft.dfs(entityset=retail_data, target_entity='customers', ignore_variables={'customers': ['Country']}, max_depth=max_depth, agg_primitives=agg_primitives, trans_primitives=trans_primitives, verbose=True) # one-hot encode to transform to numeric feature_matrix, feature_defs = ft.encode_features(feature_matrix, feature_defs) # remove feature with all nans or all single value feature_matrix, feature_defs = remove_low_information_features(feature_matrix, feature_defs) # impute missing values imputer = Imputer(missing_values='NaN', strategy='mean', axis=0) feature_matrix = imputer.fit_transform(feature_matrix) model = SVC(C=C, verbose=True) X_train, X_test, y_train, y_test = train_test_split(feature_matrix, labels, test_size=0.1) model.fit(X_train, y_train) predictions = model.predict(X_test) return f1_score(y_test, predictions, average='macro') poss_agg_primitives = [Sum, Max, Mean, Min] poss_trans_primitives = [Percentile, Day, Weekend, Weekday] scores = [] for agg_primitives in combinations(poss_agg_primitives, 2): for trans_primitives in combinations(poss_trans_primitives, 2): for max_depth in range(1, 3): for C in [0.01, 0.1, 1.0]: score = score_pipeline(max_depth, agg_primitives, trans_primitives, C) scores.append(score) print("Best score: {:.3f}".format(max(scores)))
您必须首先执行特征工程/特征选择。在调整值之前,您必须知道将使用哪些变量。
至于你如何进行特征选择,这是另一个问题。您可以使用诸如此类的技术 主成分分析 , 奇异值分解 或许多其他人。这是一个活跃的研究领域,如果您只是在Google上搜索,您会发现很多描述各种技术的论文。
这个 是我最近阅读的一篇论文,它使用基于熵的技术进行特征选择。