0Pricing
NLP Academy · レッスン

TF-IDF 特徴量で学習する

scikit-learn で分類器を適合させる

「TF-IDF 特徴量で学習する」はCoddyKit上の無料NLP Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはNLP Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 NLP Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

From Text to Numbers First

A model cannot read raw sentences. You first convert documents into TF-IDF vectors, then train logistic regression on those numbers. 🔢

Fit the Vectorizer

The TfidfVectorizer learns your vocabulary and weighting from the training texts. One call turns a list of strings into a feature matrix.

from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer()
X = vec.fit_transform(train_texts)

Labels Stay Separate

Your features X come from the text, but your labels y come from you. Each document needs its correct class, like spam or not-spam.

Split Before You Train

Hold out a test set so you can judge fairly. train_test_split keeps some data unseen until the very end of evaluation.

from sklearn.model_selection import train_test_split
X_tr, X_te, y_tr, y_te = train_test_split(X, y)

Fit Means Learn

Calling fit tells logistic regression to find the word weights that best separate your classes on the training data.

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(max_iter=1000)
clf.fit(X_tr, y_tr)

Raise max_iter If It Warns

Text has many features, so the solver may need more steps. Bumping max_iter clears the common convergence warning you will see.

Predict on New Vectors

To classify fresh text, transform it with the same fitted vectorizer, then call predict. Never refit the vectorizer on test data.

preds = clf.predict(X_te)

Transform, Do Not Fit, on Test

Test text uses transform, not fit_transform. Refitting would leak test information and quietly inflate your scores.

Check the Accuracy

A quick score call gives accuracy on the held-out set. It is your first signal that training actually worked.

print(clf.score(X_te, y_te))

Same Steps Stay in Sync

Train and prediction must share the exact same vocabulary. Using one fitted vectorizer everywhere keeps feature columns aligned.

A Pipeline Saves You

Wrapping the vectorizer and model in a Pipeline chains transform and predict automatically, so you never forget a step.

from sklearn.pipeline import make_pipeline
pipe = make_pipeline(TfidfVectorizer(), LogisticRegression())

Quick Check

How should you prepare test text before predicting?

Recap: Vectorize Then Fit

Vectorize text with TF-IDF, split, then fit logistic regression. Reuse the same vectorizer for test data, ideally inside a pipeline. ✅

よくある質問

「TF-IDF 特徴量で学習する」レッスンは無料ですか?

はい。「TF-IDF 特徴量で学習する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、NLP Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 NLP Academyコースには全4レッスンが含まれています。

「TF-IDF 特徴量で学習する」で何を学びますか?

scikit-learn で分類器を適合させる ブラウザで直接実行するハンズオンコードでNLP Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

NLP Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのNLP Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「TF-IDF 特徴量で学習する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このNLP Academyレッスンでコードを書いて実行できますか?

はい。すべてのNLP Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ロジスティック回帰がテキストで強い理由
  2. TF-IDF 特徴量で学習する
  3. 最も強い係数を調べる
  4. 正則化の強さを調整する
← NLP Academyに戻る