Data Science Primer: NumPy, pandas, matplotlib

ডেটা সায়েন্স প্রাইমার — NumPy, pandas, matplotlib

Read: ~35 min Advanced 5 practice problems Install locally

1. Python's Killer App — Data Science

More than any other reason, Python's data stack explains why it became the world's most popular language. NumPy gives you fast n-dimensional arrays; pandas builds spreadsheet-like DataFrames on top; matplotlib (and friends) turn data into pictures. Almost every modern ML library — scikit-learn, PyTorch, TensorFlow — speaks NumPy arrays.

অন্য যেকোনো কারণের চেয়ে বেশি — Python-এর data stack-ই একে বিশ্বের সবচেয়ে জনপ্রিয় ভাষায় পরিণত করেছে। NumPy দ্রুত n-dimensional array, pandas spreadsheet-এর মতো DataFrame, matplotlib দিয়ে plot। প্রায় সব আধুনিক ML library — scikit-learn, PyTorch, TensorFlow — NumPy array-এ কথা বলে।
Install locally: pip install numpy pandas matplotlib. The browser sandbox may not have these pre-installed — examples in this module demonstrate the concepts; run them locally or in a Jupyter notebook / Google Colab for full effect.

2. NumPy — Fast Arrays

numpy_demo.py
import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a, a.dtype, a.shape)

# Vectorized operations — no loops
print(a * 2)
print(a + 10)
print(a ** 2)

# Broadcasting — different shapes, compatible axes
m = np.array([[1, 2, 3], [4, 5, 6]])
print(m + np.array([10, 20, 30]))

# Statistics
print("mean:", a.mean(), "std:", a.std(), "sum:", a.sum())

A vectorized NumPy operation can be 10–100× faster than the same work done with a Python for loop. The trick: the heavy lifting runs in C underneath, not in Python bytecode.

3. pandas — DataFrames

pandas_demo.py
import pandas as pd

df = pd.DataFrame({
    "name":  ["Asif", "Mou", "Rafi", "Ayesha"],
    "city":  ["Dhaka", "Khulna", "Dhaka", "Chittagong"],
    "score": [88, 92, 77, 95]
})

print(df)

# Select columns
print(df["score"].mean())

# Filter rows
print(df[df.score >= 85])

# Group and aggregate
print(df.groupby("city")["score"].mean())

# Sort
print(df.sort_values("score", ascending=False))

4. Loading Real Data

load_data.py
import pandas as pd

# CSV is the most common format
# df = pd.read_csv("sales.csv")
# df = pd.read_excel("data.xlsx", sheet_name="Q1")
# df = pd.read_json("records.json")
# df = pd.read_sql("SELECT * FROM users", conn)

# For this demo we build one inline
from io import StringIO
csv = StringIO("month,sales\nJan,120\nFeb,150\nMar,180\nApr,210")
df = pd.read_csv(csv)
print(df)
print("Total:", df["sales"].sum())
print("Avg:",   df["sales"].mean())

5. matplotlib — Quick Plots

matplotlib is the grandparent of Python plotting. It is low-level but reliable. Seaborn, Plotly, Altair are higher-level friends.

plot_demo.py
import matplotlib.pyplot as plt

months = ["Jan", "Feb", "Mar", "Apr"]
sales  = [120, 150, 180, 210]

plt.plot(months, sales, marker="o")
plt.title("Quarterly sales")
plt.xlabel("Month")
plt.ylabel("Units sold")
plt.grid(True)

# In scripts: plt.savefig("sales.png"); in notebooks: plt.show()
plt.savefig("/tmp/sales.png")
print("saved plot to /tmp/sales.png")

6. A Tiny End-to-End Mini-Analysis

mini.py
import pandas as pd
from io import StringIO

raw = """name,city,salary
Asif,Dhaka,45000
Mou,Dhaka,55000
Rafi,Khulna,40000
Ayesha,Chittagong,60000
Kamal,Dhaka,70000
"""

df = pd.read_csv(StringIO(raw))

# Summaries
print("rows:", len(df))
print("salary stats:")
print(df["salary"].describe())

# Top 3 by salary
print("\nTop 3:")
print(df.nlargest(3, "salary"))

# Average salary by city
print("\nAvg salary by city:")
print(df.groupby("city")["salary"].mean())

7. Vocabulary (শব্দভাণ্ডার)

TermMeaningবাংলায়
ndarrayNumPy's n-dimensional array.NumPy-র n-dim array।
VectorizationWhole-array ops without Python loops.Loop ছাড়া পুরো array-এ operation।
BroadcastingAligning shapes for element-wise ops.Element-wise ops-এর জন্য shape align করা।
DataFrame2-D labeled table (pandas).2-D labeled টেবিল।
Series1-D labeled array (pandas column).1-D labeled array।

8. Practice Problems

  1. Using NumPy, create an array of integers 1–10 and compute its mean and std.
    NumPy দিয়ে ১-১০ array তৈরি করে mean ও std বের করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans1.py
    import numpy as np
    a = np.arange(1, 11)
    print(a)
    print("mean:", a.mean(), "std:", a.std())
  2. From a pandas DataFrame of students with scores, find the highest scorer.
    DataFrame থেকে সর্বোচ্চ স্কোরধারী খুঁজে বের করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans2.py
    import pandas as pd
    df = pd.DataFrame({"name": ["A", "B", "C"], "score": [80, 95, 75]})
    print(df.loc[df.score.idxmax()])
  3. Using pandas, read a CSV string and show rows where salary > 50000.
    pandas দিয়ে CSV পড়ে salary > 50000 এমন row দেখান।
    ✨ Show Answer (উত্তর দেখুন)
    ans3.py
    import pandas as pd
    from io import StringIO
    csv = StringIO("name,salary\nA,40000\nB,60000\nC,55000")
    df = pd.read_csv(csv)
    print(df[df.salary > 50000])
  4. Make a bar chart of months vs. sales using matplotlib.
    matplotlib দিয়ে month বনাম sales-এর bar chart তৈরি করুন।
    ✨ Show Answer (উত্তর দেখুন)
    ans4.py
    import matplotlib.pyplot as plt
    plt.bar(["Jan", "Feb", "Mar"], [100, 150, 200])
    plt.savefig("/tmp/bar.png")
    print("saved")
  5. Explain in 2 sentences why NumPy vectorized ops are faster than Python loops.
    ব্যাখ্যা করুন — NumPy vectorized ops Python loop-এর চেয়ে কেন দ্রুত।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: NumPy stores numbers in a compact C array and performs the computation inside a pre-compiled C loop, so it pays the Python interpreter overhead only once instead of once per element. A Python for loop has to execute bytecode for every element, check types, and handle Python objects — all of which is 10–100× slower on large arrays.

    NumPy সংখ্যাগুলো compact C array-এ রাখে এবং হিসাব C-তে লেখা compiled loop-এ চালায়, তাই Python interpreter overhead প্রতি element-এ নয়, একবারই দিতে হয়। Python for loop প্রতিটি element-এ bytecode চালায়, type চেক করে, Python object handle করে — যা বড় array-এ ১০-১০০x ধীর।

Summary — Module 36

NumPy vectorizes numeric work; pandas puts it in labeled, spreadsheet-like DataFrames; matplotlib plots the results. Together they are why Python dominates data science and ML research. Start in a Jupyter notebook or Google Colab — read_csv, poke at the data, plot, repeat. This module's 40 minutes will save you 40 hours later.

NumPy সংখ্যা-ভিত্তিক কাজ vectorize করে; pandas labeled DataFrame দেয়; matplotlib plot করে। এই তিনটি মিলেই Python-কে data science-এ রাজা বানিয়েছে। Jupyter বা Google Colab-এ শুরু করুন — read_csv, data দেখুন, plot করুন, পুনরায় করুন।

Next Module → Web Development — Flask / FastAPI।