Scikit - Learn を使った特徴抽出

Machine LearningMachine LearningBeginner
オンラインで実践に進む

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、scikit-learn ライブラリを使って特徴抽出を行う方法を学びます。特徴抽出とは、生データを機械学習アルゴリズムで使用できる数値特徴に変換するプロセスです。これには、テキストや画像などのさまざまな種類のデータから関連情報を抽出する作業が含まれます。

VM のヒント

VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。

Jupyter Notebook の読み込みには数秒かかる場合があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。

学習中に問題があった場合は、Labby にお問い合わせください。セッション終了後にフィードバックを提供してください。すぐに問題を解決いたします。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/DataPreprocessingandFeatureEngineeringGroup(["Data Preprocessing and Feature Engineering"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/feature_extraction("Feature Extraction") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/feature_extraction -.-> lab-71129{{"Scikit - Learn を使った特徴抽出"}} ml/sklearn -.-> lab-71129{{"Scikit - Learn を使った特徴抽出"}} end

辞書からの特徴の読み込み

このステップでは、scikit-learn のDictVectorizerクラスを使って辞書から特徴を読み込む方法を学びます。

from sklearn.feature_extraction import DictVectorizer

measurements = [
    {'city': 'Dubai', 'temperature': 33.},
    {'city': 'London', 'temperature': 12.},
    {'city': 'San Francisco', 'temperature': 18.},
]

vec = DictVectorizer()
features = vec.fit_transform(measurements).toarray()
feature_names = vec.get_feature_names_out()

print(features)
print(feature_names)

特徴ハッシュ

このステップでは、scikit-learn のFeatureHasherクラスを使って特徴ハッシュを行う方法を学びます。特徴ハッシュは、ハッシュ関数を使って特徴を固定長のベクトルにマッピングする手法です。

from sklearn.feature_extraction import FeatureHasher

movies = [
    {'category': ['thriller', 'drama'], 'year': 2003},
    {'category': ['animation', 'family'], 'year': 2011},
    {'year': 1974},
]

hasher = FeatureHasher(input_type='string')
hashed_features = hasher.transform(movies).toarray()

print(hashed_features)

テキスト特徴抽出

このステップでは、scikit-learn のCountVectorizerTfidfVectorizerクラスを使ってテキスト特徴抽出を行う方法を学びます。これらのクラスは、テキストデータを数値特徴に変換するために使用できます。

from sklearn.feature_extraction.text import CountVectorizer

corpus = [
    'This is the first document.',
    'This is the second second document.',
    'And the third one.',
    'Is this the first document?',
]

vectorizer = CountVectorizer()
features = vectorizer.fit_transform(corpus).toarray()
feature_names = vectorizer.get_feature_names_out()

print(features)
print(feature_names)

ベクトル化クラスのカスタマイズ

このステップでは、ベクトル化クラスに呼び出し可能な関数を渡すことで、それらの動作をカスタマイズする方法を学びます。

def my_tokenizer(s):
    return s.split()

vectorizer = CountVectorizer(tokenizer=my_tokenizer)
features = vectorizer.fit_transform(corpus).toarray()

print(features)

まとめ

この実験では、scikit-learn ライブラリを使って特徴抽出を行う方法を学びました。辞書からの特徴の読み込み、特徴ハッシュ、テキスト特徴抽出など、様々な手法を検討しました。また、特定のニーズに合うようにベクトル化クラスの動作をカスタマイズする方法も学びました。特徴抽出は機械学習において重要なステップであり、生データをアルゴリズムが予測やデータの分類に使用できる形式に変換するのに役立ちます。

OSZAR »