utils#
有用的预处理工具函数。
模块内容#
函数#
从文件加载特征和目标。 |
|
获取 DataFrame 中每种特定 dtype 的特征数量。 |
|
将数据分割成训练集和测试集。 |
|
将堆叠的多序列数据分割成训练集和测试集。非堆叠的数据可以使用 split_data。 |
|
获取目标分布。 |
目录#
- evalml.preprocessing.utils.load_data(path, index, target, n_rows=None, drop=None, verbose=True, **kwargs)[source]#
从文件加载特征和目标。
- 参数
path (str) – 文件路径或 http/ftp/s3 URL。
index (str) – 索引列。
target (str) – 目标列。
n_rows (int) – 要返回的行数。默认为 None。
drop (list) – 要删除的列列表。默认为 None。
verbose (bool) – 如果为 True,则打印有关特征和目标的信息。默认为 True。
**kwargs – 应传递给 panda 的 read_csv 方法的其他关键字参数。
- 返回
特征矩阵和目标。
- 返回类型
pd.DataFrame, pd.Series
- evalml.preprocessing.utils.number_of_features(dtypes)[source]#
获取 DataFrame 中每种特定 dtype 的特征数量。
- 参数
dtypes (pd.Series) – 要获取特征数量的 DataFrame.dtypes。
- 返回
dtypes 和每种输入类型的特征数量。
- 返回类型
pd.Series
示例
>>> X = pd.DataFrame() >>> X["integers"] = [i for i in range(10)] >>> X["floats"] = [float(i) for i in range(10)] >>> X["strings"] = [str(i) for i in range(10)] >>> X["booleans"] = [bool(i%2) for i in range(10)]
列出每种 dtype 对应的列数。
>>> number_of_features(X.dtypes) Number of Features Boolean 1 Categorical 1 Numeric 2
- evalml.preprocessing.utils.split_data(X, y, problem_type, problem_configuration=None, test_size=None, random_seed=0)[source]#
将数据分割成训练集和测试集。
- 参数
X (pd.DataFrame 或 np.ndarray) – 形状为 [n_samples, n_features] 的数据
y (pd.Series 或 np.ndarray) – 长度为 [n_samples] 的目标数据
problem_type (str 或 ProblemTypes) – 监督学习问题的类型。有关完整列表,请参阅 evalml.problem_types.problemtype.all_problem_types。
problem_configuration (dict) – 配置搜索所需的附加参数。例如,在时间序列问题中,应为 time_index、gap 和 max_delay 变量传递值。
test_size (float) – 应包含在测试集中的数据点百分比。对于非时间序列问题,默认为 0.2 (20%);对于时间序列问题,默认为 0.1 (10%)。
random_seed (int) – 随机数生成器的种子。默认为 0。
- 返回
特征和目标数据各自分割成训练集和测试集。
- 返回类型
pd.DataFrame, pd.DataFrame, pd.Series, pd.Series
- 引发
ValueError – 如果 problem_configuration 缺失或对于多序列问题不包含 time_index 和 series_id。
示例
>>> X = pd.DataFrame([1, 2, 3, 4, 5, 6], columns=["First"]) >>> y = pd.Series([8, 9, 10, 11, 12, 13]) ... >>> X_train, X_validation, y_train, y_validation = split_data(X, y, "regression", random_seed=42) >>> X_train First 5 6 2 3 4 5 3 4 >>> X_validation First 0 1 1 2 >>> y_train 5 13 2 10 4 12 3 11 dtype: int64 >>> y_validation 0 8 1 9 dtype: int64
- evalml.preprocessing.utils.split_multiseries_data(X, y, series_id, time_index, **kwargs)[source]#
将堆叠的多序列数据分割成训练集和测试集。非堆叠的数据可以使用 split_data。
- 参数
X (pd.DataFrame) – 形状为 [n_samples*n_series, n_features] 的输入训练数据。
y (pd.Series) – 长度为 [n_samples*n_series] 的目标训练目标。
series_id (str) – 包含序列 ID 的列的名称。
time_index (str) – 包含时间索引的列的名称。
**kwargs – 要传递给 split_data 函数的附加关键字参数。
- 返回
特征和目标数据各自分割成训练集和测试集。
- 返回类型
pd.DataFrame, pd.DataFrame, pd.Series, pd.Series
- evalml.preprocessing.utils.target_distribution(targets)[source]#
获取目标分布。
- 参数
targets (pd.Series) – 目标数据。
- 返回
目标数据及其按百分比表示的频率分布。
- 返回类型
pd.Series
示例
>>> y = pd.Series([1, 2, 4, 1, 3, 3, 1, 2]) >>> print(target_distribution(y).to_string()) Targets 1 37.50% 2 25.00% 3 25.00% 4 12.50% >>> y = pd.Series([True, False, False, False, True]) >>> print(target_distribution(y).to_string()) Targets False 60.00% True 40.00%