Skip to content

特徴量

特徴量は、DataRobotがモデリングに使用するデータセット内の列を表します。 各特徴量には、型、統計、有用性などのプロパティがあり、データについて理解し、情報に基づいたモデリングの決定を行うのに役立ちます。 このページでは、プロジェクトで特徴量を使用する方法について説明します。

特徴量の取得

プロジェクト内のすべての特徴量を取得したり、特定の特徴量を名前で取得したりすることができます。

すべての特徴量を取得する

プロジェクトからすべての特徴量を取得するには、Project.get_features()を使用します。

>>> import datarobot as dr
>>> project = dr.Project.get('5e3c94aff86f2d10692497b5')
>>> features = project.get_features()
>>> len(features)
21
>>> features[0].name
'Partition'
>>> features[0].feature_type
'Numeric' 

また、Project.iterate_features()を使用して特徴量を反復処理することもできます。

>>> from itertools import islice
>>> feature_iterator = project.iterate_features(offset=0, limit=10)
>>> for feature in islice(feature_iterator, 5):
...     print(feature.name, feature.feature_type)
Partition Numeric
CustomerID Categorical
Age Numeric
Income Numeric
Education Categorical 

特定の特徴量を取得する

単一の特徴量を名前で取得するには、Feature.get()を使用します。

>>> feature = dr.Feature.get(project_id=project.id, feature_name='Age')
>>> feature.name
'Age'
>>> feature.feature_type
'Numeric'
>>> feature.project_id
'5e3c94aff86f2d10692497b5' 

特徴量のプロパティを探索する

各特徴量オブジェクトには、その特徴量の特徴や分布に関する詳細な情報が含まれています。

特徴量の基本情報

>>> feature = project.get_features()[0]
>>> feature.name
'Age'
>>> feature.feature_type
'Numeric'
>>> feature.id
12345
>>> feature.project_id
'5e3c94aff86f2d10692497b5' 

特徴量の統計

数値特徴量の場合、EDAサンプルからサマリー統計にアクセスできます。

>>> numeric_feature = dr.Feature.get(project_id=project.id, feature_name='Income')
>>> numeric_feature.min
25000.0
>>> numeric_feature.max
150000.0
>>> numeric_feature.mean
67500.0
>>> numeric_feature.median
65000.0
>>> numeric_feature.std_dev
18500.5 

日付特徴量の場合、サマリー統計はISO-8601形式の日付文字列として表されます。

>>> date_feature = dr.Feature.get(project_id=project.id, feature_name='TransactionDate')
>>> date_feature.min
'2020-01-01T00:00:00Z'
>>> date_feature.max
'2023-12-31T23:59:59Z' 

特徴量のデータ品質

特徴量のデータ品質指標をチェックするには:

>>> feature = dr.Feature.get(project_id=project.id, feature_name='Email')
>>> feature.unique_count
1250
>>> feature.na_count
5
>>> feature.low_information
False
>>> feature.importance
0.85 

importance属性は、特徴量とターゲットの関係性の強さを数値で表すもので、モデルとは無関係です。 パーティション列などのモデリング以外の特徴量の場合、この値はNoneとなることがあります。

ターゲットリーケージの検出

特徴量にターゲットリーケージがあるかどうかをチェックするには:

>>> feature = dr.Feature.get(project_id=project.id, feature_name='CustomerID')
>>> feature.target_leakage
'FALSE' 

ターゲットリーケージは以下の値を返す可能性があります。

  • FALSE:ターゲットリーケージは検出されない。
  • MODERATE:ターゲットリーケージのリスクは中程度。
  • HIGH_RISK:ターゲットリーケージのリスクが高い。
  • SKIPPED_DETECTION:この特徴量ではターゲットリーケージの検出は実行されなかった。

時系列に対する適性

時系列プロジェクトの場合、特徴量を日時パーティション列として使用できるかどうかを確認します。

>>> date_feature = dr.Feature.get(project_id=project.id, feature_name='Date')
>>> date_feature.time_series_eligible
True
>>> date_feature.time_series_eligibility_reason
'Suitable for use as datetime partition column'
>>> date_feature.time_step
1
>>> date_feature.time_unit
'DAY' 

特徴量のヒストグラムを取得する

ヒストグラムは、特徴量の分布を視覚的に表現します。 任意の特徴量でヒストグラムのデータを取得するには:

>>> feature = dr.Feature.get(project_id=project.id, feature_name='Age')
>>> histogram = feature.get_histogram()
>>> histogram.plot
[{'count': 150, 'target': None, 'label': '18-25'},
 {'count': 320, 'target': None, 'label': '26-35'},
 {'count': 450, 'target': None, 'label': '36-45'},
 {'count': 280, 'target': None, 'label': '46-55'},
 {'count': 100, 'target': None, 'label': '56+'}] 

ビンの最大数を指定できます。

>>> histogram = feature.get_histogram(bin_limit=20)
>>> len(histogram.plot)
20 

特徴量セットの操作

特徴量セットは、モデリングに使用される特徴量のコレクションです。 プロジェクトから特徴量セットを取得し、その中に含まれる特徴量を調べることができます。

プロジェクトの特徴量セットを取得する

>>> project = dr.Project.get('5e3c94aff86f2d10692497b5')
>>> featurelists = project.get_featurelists()
>>> featurelists
[Featurelist('Raw Features'),
 Featurelist('Informative Features'),
 Featurelist('universe')] 

特徴量セット内の特徴量を調べる

>>> raw_features = project.get_featurelists()[0]
>>> raw_features.features
['Partition', 'CustomerID', 'Age', 'Income', 'Education', 'Email']
>>> len(raw_features.features)
21 

カスタム特徴量セットの作成

使用可能な特徴量のサブセットからカスタム特徴量セットを作成できます。

>>> all_features = project.get_features()
>>> selected_feature_names = [f.name for f in all_features if f.feature_type == 'Numeric']
>>> custom_featurelist = project.create_featurelist(
...     name='Numeric Features Only',
...     features=selected_feature_names
... )
>>> custom_featurelist
Featurelist('Numeric Features Only') 

カテゴリー特徴量の分析

カテゴリー特徴量については、カテゴリーの分布に関する追加のインサイトにアクセスできます。

カテゴリー特徴量でキーのサマリーを取得する

集計されたカテゴリー特徴量の場合、上位キーの統計を取得できます。

>>> categorical_feature = dr.Feature.get(project_id=project.id, feature_name='ProductCategory')
>>> key_summary = categorical_feature.key_summary
>>> key_summary[0]
{'key': 'Electronics',
 'summary': {'min': 0, 'max': 29815.0, 'stdDev': 6498.029, 'mean': 1490.75,
             'median': 0.0, 'pctRows': 5.0}} 

キーのサマリーでは、以下を含む上位50キーの統計を提供します。 - min:キーの最小値。 - max:キーの最大値。 - mean:キーの平均値。 - median:キーの中央値。 - stdDev:キーの標準偏差。 - pctRows:EDAサンプルにおけるキーの出現率。

多カテゴリー特徴量の分析

多カテゴリー特徴量の場合、ラベルの関係性に特化したインサイトを取得できます。

多カテゴリーのヒストグラムを取得する

>>> multicat_feature = dr.Feature.get(project_id=project.id, feature_name='Tags')
>>> histogram = multicat_feature.get_multicategorical_histogram()
>>> histogram
MulticategoricalHistogram(...) 

ペアワイズ相関の取得

多カテゴリー特徴量におけるラベル間の相関を分析します。

>>> correlations = multicat_feature.get_pairwise_correlations()
>>> correlations
PairwiseCorrelations(...) 

ペアワイズ結合の確率を取得する

>>> joint_probs = multicat_feature.get_pairwise_joint_probabilities()
>>> joint_probs
PairwiseJointProbabilities(...) 

ペアワイズ条件付き確率を取得する

>>> cond_probs = multicat_feature.get_pairwise_conditional_probabilities()
>>> cond_probs
PairwiseConditionalProbabilities(...) 

時系列特徴量のプロパティ

時系列プロジェクトでは、複数系列または交差系列設定で使用する場合、特徴量の追加プロパティを取得できます。

複数系列プロパティの取得

潜在的な複数系列の日時パーティション列で時系列プロパティを取得します。

>>> date_feature = dr.Feature.get(project_id=project.id, feature_name='Date')
>>> properties = date_feature.get_multiseries_properties(
...     multiseries_id_columns=['StoreID']
... )
>>> properties
{'time_series_eligible': True,
 'time_unit': 'DAY',
 'time_step': 1} 

交差系列プロパティの取得

複数系列ID列の交差系列プロパティを取得するには:

>>> multiseries_feature = dr.Feature.get(project_id=project.id, feature_name='StoreID')
>>> properties = multiseries_feature.get_cross_series_properties(
...     datetime_partition_column='Date',
...     cross_series_group_by_columns=['Region']
... )
>>> properties
{'name': 'StoreID',
 'eligibility': 'Eligible as cross-series group-by column',
 'isEligible': True} 

特徴量のフィルターと検索

さまざまな条件で特徴量をフィルターして、関心のある特定の特徴量を見つけることができます。

特徴量の型でフィルター

>>> all_features = project.get_features()
>>> numeric_features = [f for f in all_features if f.feature_type == 'Numeric']
>>> categorical_features = [f for f in all_features if f.feature_type == 'Categorical']
>>> text_features = [f for f in all_features if f.feature_type == 'Text'] 

データが欠損している特徴量を見つける

>>> features_with_missing = [f for f in project.get_features()
...                          if f.na_count is not None and f.na_count > 0]
>>> for feature in features_with_missing:
...     print(f"{feature.name}: {feature.na_count} missing values")
Email: 5 missing values
Phone: 12 missing values 

情報量の少ない特徴量を見つける

>>> low_info_features = [f for f in project.get_features() if f.low_information]
>>> [f.name for f in low_info_features]
['ConstantColumn', 'SingleValueColumn'] 

有用性のしきい値で特徴量を検索する

>>> important_features = [f for f in project.get_features()
...                       if f.importance is not None and f.importance > 0.5]
>>> sorted_features = sorted(important_features, key=lambda x: x.importance, reverse=True)
>>> for feature in sorted_features[:5]:
...     print(f"{feature.name}: {feature.importance:.3f}")
Income: 0.892
Age: 0.756
Education: 0.643 

一般的なワークフロー

プロジェクトの全特徴量を分析する

>>> project = dr.Project.get('5e3c94aff86f2d10692497b5')
>>> features = project.get_features()
>>>
>>> print(f"Total features: {len(features)}")
>>> print(f"Feature types: {set(f.feature_type for f in features)}")
>>>
>>> for feature in features:
...     print(f"\n{feature.name} ({feature.feature_type}):")
...     if feature.na_count is not None:
...         print(f"  Missing values: {feature.na_count}")
...     if feature.importance is not None:
...         print(f"  Importance: {feature.importance:.3f}")
...     if feature.target_leakage != 'SKIPPED_DETECTION':
...         print(f"  Target leakage: {feature.target_leakage}") 

特徴量に関する情報をDataFrameにエクスポートする

>>> import pandas as pd
>>>
>>> features = project.get_features()
>>> feature_data = []
>>> for feature in features:
...     feature_data.append({
...         'name': feature.name,
...         'type': feature.feature_type,
...         'importance': feature.importance,
...         'missing_count': feature.na_count,
...         'unique_count': feature.unique_count,
...         'low_information': feature.low_information,
...         'target_leakage': feature.target_leakage
...     })
>>>
>>> df = pd.DataFrame(feature_data)
>>> df.to_csv('feature_summary.csv', index=False) 

プロジェクト間で特徴量を比較する

>>> project1 = dr.Project.get('5e3c94aff86f2d10692497b5')
>>> project2 = dr.Project.get('5e3c94aff86f2d10692497b6')
>>>
>>> features1 = {f.name: f for f in project1.get_features()}
>>> features2 = {f.name: f for f in project2.get_features()}
>>>
>>> common_features = set(features1.keys()) & set(features2.keys())
>>> print(f"Common features: {len(common_features)}")
>>>
>>> for feature_name in common_features:
...     f1 = features1[feature_name]
...     f2 = features2[feature_name]
...     if f1.feature_type != f2.feature_type:
...         print(f"{feature_name}: type mismatch ({f1.feature_type} vs {f2.feature_type})") 

注意事項

  • 特徴量の統計(minmaxmeanmedianstd_dev)はEDAのサンプルデータから計算されます。
  • 数値以外の特徴量やサマリー統計が使用可能になる前に作成された特徴量の場合、値はNoneになります。
  • importance属性はモデルとは無関係で、特徴量とターゲットの関係性の強さを測定します。
  • 時系列プロジェクトでは、Featureオブジェクトは入力特徴量を表し、ModelingFeatureオブジェクトはパーティショニング後のモデリングに使用される特徴量を表します。
  • 特徴量のヒストグラムはEDAサンプルに基づいており、データセットの分布全体が反映されていない場合があります。