Scikit-Learn を使用した複数の DataFrame 列のラベル エンコーディング
pandas DataFrame で文字列ラベルを操作する場合、多くの場合、文字列ラベルを次のようにエンコードする必要があります。機械学習アルゴリズムとの互換性のための整数。 Scikit-learn の LabelEncoder はこのタスクには便利なツールですが、列ごとに複数の LabelEncoder オブジェクトを使用するのは面倒な場合があります。
これを回避するには、次のアプローチを利用できます:
df.apply(LabelEncoder().fit_transform)
これにより、DataFrame の各列に LabelEncoder が適用され、すべての文字列ラベルが効果的にエンコードされます。 integers.
OneHotEncoder による拡張エンコーディング
Scikit-Learn のより新しいバージョン (0.20 以降) では、ラベル エンコーディング文字列入力には OneHotEncoder() クラスが推奨されます。 :
OneHotEncoder().fit_transform(df)
OneHotEncoder は効率的な機能を提供しますカテゴリカル データに必要となることが多いワンホット エンコーディング。
逆変換および変換操作
逆変換またはエンコードされたラベルを変換するには、次の手法を使用できます。
- の辞書を維持しますLabelEncoders:
from collections import defaultdict d = defaultdict(LabelEncoder) # Encoding fit = df.apply(lambda x: d[x.name].fit_transform(x)) # Inverse transform fit.apply(lambda x: d[x.name].inverse_transform(x)) # Transform future data df.apply(lambda x: d[x.name].transform(x))
- 特定の列に ColumnTransformer を使用します:
from sklearn.preprocessing import ColumnTransformer, OneHotEncoder # Select specific columns for encoding encoder = OneHotEncoder() transformer = ColumnTransformer(transformers=[('ohe', encoder, ['col1', 'col2', 'col3'])]) # Transform the DataFrame encoded_df = transformer.fit_transform(df)
- Neuraxle の FlattenForEach ステップを使用します:
from neuraxle.preprocessing import FlattenForEach # Flatten all columns and apply LabelEncoder encoded_df = FlattenForEach(LabelEncoder(), then_unflatten=True).fit_transform(df)
具体的な内容に応じて要件に応じて、Scikit-Learn の複数の列のラベル エンコードに最適な方法を選択できます。
以上がScikit-Learn を使用して複数の DataFrame 列を効率的にエンコードするにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonは解釈された言語ですが、コンパイルプロセスも含まれています。 1)Pythonコードは最初にBytecodeにコンパイルされます。 2)ByteCodeは、Python Virtual Machineによって解釈および実行されます。 3)このハイブリッドメカニズムにより、Pythonは柔軟で効率的になりますが、完全にコンパイルされた言語ほど高速ではありません。

useaforloopwhenteratingoverasequenceor foraspificnumberoftimes; useawhileloopwhentinuninguntinuntilaConditionismet.forloopsareidealforknownownownownownownoptinuptinuptinuptinuptinutionsituations whileoopsuitsituations withinterminedationations。

pythonloopscanleadtoErrorslikeinfiniteloops、ModifiningListsDuringiteration、Off-Oneerrors、Zero-dexingissues、およびNestededLoopinefficiencies.toavoidhese:1)use'i

forloopsareadvastountousforknowterations and sequences、offeringsimplicityandeadability;

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール
