NLP Basics

Python · Advanced Python & AI

Theory

Natural Language Processing (NLP) lets computers work with human text. Models need numbers, so the first job is turning text into vectors. A typical classic pipeline is: clean the text (lowercase, strip punctuation), tokenize it into words, optionally remove stop words like 'the', then vectorize. CountVectorizer counts how often each vocabulary word appears in each document (bag of words).

TfidfVectorizer goes further with TF-IDF (term frequency x inverse document frequency): words frequent in one document but rare across the collection get higher weights, while words found everywhere get lower ones, and each row is normalised to length 1. For ['great service', 'poor support'] the vocabulary is great, poor, service, support, and each document gets 0.707 for its two words. These vectors can then feed a classifier such as logistic regression for sentiment analysis.

Why we use it

Huge amounts of business data are text: reviews, tickets, emails, chats. NLP turns it into numbers so you can classify sentiment, route tickets, find topics or search documents automatically.

When to use it

TF-IDF with a linear model is a fast, strong baseline for text classification and keyword search. For tasks needing context and meaning (summaries, question answering), transformer-based models usually perform far better.

How it works

  1. Raw texts
  2. Clean: lowercase, remove punctuation
  3. Tokenize into words
  4. Vectorize with TfidfVectorizer: one column per word
  5. Have labels (e.g. sentiment)? Yes: Train a classifier on them. No: Cluster or search the vectors.
  6. Predictions or insights from text

Syntax

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

Example

vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(['great service', 'poor support'])

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic