← All courses CSIS 3352 · Module 4

Python and Feature Engineering for Artificial Intelligence

📅 No graded activity this module · On the Midterm (Modules 1–9) — moved to Tue Oct 27
📌 The Lecture 4 deck states no learning objectives. The 15 objectives below are derived from the deck's own structure — 3 parts (Python Basics · NumPy & Pandas · Preparing Features for ML) across 21 numbered slides. Each objective's bullets are also its Concept Check rubric.
⚠️ Source labeling conflict: the PDF deck is titled Lecture 4 (syllabus: Module 4), but the notebook's header says "Lecture 6" and tells you to run it alongside the "Lecture 5 slides." The code is the same in both files. This page follows the deck and syllabus: Module 4.
📌 The deck shows code but no output. Results quoted on this page (e.g., average score 72.6, Train rows: 8) come from running the provided notebook.

Study Guide

Module 4 · 3 parts · 21 numbered slides · one running example
📌 Code is from the Lecture 4 deck. Lines marked Output come from running the provided notebook — the deck shows code only. Purple OOP reminder boxes are Java-bridge study aids, not deck content; nothing in them is on the quiz or flashcards.

The notebook builds one small class of students and uses it all the way through to a train/test split, "the same way a real project flows."

nameagemajorscorepassed
Ali20CS881
Sara21Math921
Omar19CS650
Priya22Business741
Liam20CS550
Noor23Math951
Diego21Business600
Emma20CS821
Kwame22Math701
Yuki19Business450
df_students["passed"] = (df_students["score"] >= 70).astype(int) # 1 = passed, 0 = did not pass
⚠️ Source gap: slide 11's comment says students.csv "already has our class data saved," and the deck never shows where passed comes from. Only the notebook defines it: passed = 1 if score ≥ 70, else 0 (6 of 10 passed). Kwame's 70 counts as a pass because the test is >=.

1–2 · Running code & variables

print("Hello, Data Science!") name = "Ayesha" age = 20 gpa = 3.8 print(name, age, gpa)
  • Execution order — Python code runs top to bottom, one line at a time.
  • print() — displays output; "the very first command every Python programmer learns."
  • Variable — a name that stores a value, so you can reuse it later. No need to declare a type — Python figures it out automatically.

3 · Data types

TypeSlide exampletype() prints
intx = 10<class 'int'>
floaty = 3.14<class 'float'>
strz = "hello"<class 'str'>
boolis_valid = True<class 'bool'>
🧩 OOP reminder — x/y here are not X/y. Slide 4's lowercase x and y are throwaway variable names (10 and 3.14). From slide 16 on, uppercase X = features (several input columns) and lowercase y = target (one column you predict). Keep that casing exact — Python is case-sensitive, so X and x are different variables.

4–5 · Lists vs dictionaries

fruits = ["apple", "banana", "cherry"] fruits.append("mango") print(fruits[0]) # first item print(len(fruits)) # how many items student = {"name": "Ayesha", "age": 20, "major": "CS"} print(student["name"]) student["gpa"] = 3.8 # add a new key-value pair print(student)
Output apple 4 Ayesha {'name': 'Ayesha', 'age': 20, 'major': 'CS', 'gpa': 3.8}
ListDictionary
StoresAn ordered collection of itemsKey-value pairs
Get a valueBy position, starting at index 0 — fruits[0]By key — student["name"]
Change itMutable — fruits.append("mango")student["gpa"] = 3.8 adds a pair
Deck's use case—Labeled data like a student record
🧩 OOP reminder — methods. fruits.append("mango") is a method call: fruits is a list object and append is a method that belongs to it. Same object.method() dot syntax as Java's list.add("mango") on an ArrayList. len(fruits) is different — it's a plain function that takes the list as an argument.

6 · Functions

def greet(name): return f"Hello, {name}!" print(greet("Data Science Class"))
Output Hello, Data Science Class!
  • Function — a reusable block of code you can call by name.
  • Why — functions keep code organized, and avoid repeating yourself.
⚠️ Not explained in the source: the f"…{name}…" string form on slide 7. The output shows the value of name appears where {name} sits; the deck gives no name or rule for this syntax.

7 · Loops

for fruit in fruits: print(fruit) i = 0 while i < 3: print("count:", i) i += 1
Output apple banana cherry mango count: 0 count: 1 count: 2
LoopRepeats…Slide example
forOnce for each item in a collectionfor fruit in fruits: → 4 lines
whileAs long as a condition stays truewhile i < 3: → count 0, 1, 2

8 · NumPy

import numpy as np arr = np.array([1, 2, 3, 4, 5]) print("Mean:", arr.mean()) print("Sum:", arr.sum())
Output Mean: 3.0 Sum: 15
  • NumPy arrays — store numbers efficiently, and support fast math operations.
  • Role — the foundation that pandas and most ML libraries are built on.

9–10 · pandas DataFrame & read_csv()

import pandas as pd data = {"name": ["Ali", "Sara"], "score": [88, 92]} df_demo = pd.DataFrame(data) df = pd.read_csv("students.csv") df.head() # show the first 5 rows
  • DataFrame — think of it as a spreadsheet in Python.
  • pandas — the core tool for loading, exploring, and cleaning real datasets.
  • read_csv() — loads a CSV file straight into a DataFrame; usually the very first line of code in a real project.
🧩 OOP reminder — building objects. np.array(...) and pd.DataFrame(data) each create an object; np and pd are just short names (aliases) for the imported libraries. Everything after a dot on arr or df — .mean(), .head(), .sort_values() — is a method on that object. df["score"] pulls out one column, which is itself an object, so df["score"].mean() calls .mean() on the column.

11–15 · Asking the data questions

OperationSlide codeQuestion it answersResult (run)
Filteringdf[df["score"] > 80]"Who scored above 80?"Ali 88, Sara 92, Noor 95, Emma 82
Sortingdf.sort_values("score", ascending=False)Top (or bottom) performers at a glanceNoor 95 first … Yuki 45 last
Aggregationdf["score"].mean() · .max()One number for a column (mean, sum, max, count)72.6 · 95
describe()df.describe()Full statistical summary in one linecount, mean, std, min, 25%, 50%, 75%, max
GroupBydf.groupby("major")["score"].mean()"Average score, per major?"Business 59.67 · CS 72.5 · Math 85.67
Output — df.describe() age score passed count 10.000000 10.000 10.000000 mean 20.700000 72.600 0.600000 std 1.337494 16.668 0.516398 min 19.000000 45.000 0.000000 25% 20.000000 61.250 0.000000 50% 20.500000 72.000 1.000000 75% 21.750000 86.500 1.000000 max 23.000000 95.000 1.000000
  • Read the passed column's mean — 0.6 means 6 of the 10 students have passed = 1.
  • describe() skips text columns — on the run, name and major don't appear because they aren't numbers.

The flow on one line

students.csv→ df→ X = featuresy = target→ encode X→ train_test_split→ X_trainX_testy_trainy_test

15–16 · Features (X) and label / target (y)

X = df[["age", "score"]] # features: input columns a model learns from y = df["passed"] # label: the target we want to predict
  • Features (X) — the input columns a model learns patterns from. Choosing good features is one of the most important parts of the job.
  • Label / target (y) — the target column: what you're trying to predict.
  • Supervised — every supervised model needs both features and a label to learn from. Cross-reference: Module 1 defines supervised learning as learning from labeled data where the correct answer is already known.

17 · Feature selection

df[["age", "score", "passed"]].corr()["passed"]
Output age 0.611315 score 0.844243 passed 1.000000
  • Feature selection — not every column helps a model; feature selection keeps the useful ones.
  • Starting point — check which features correlate with the label. Here score correlates more strongly than age.
⚠️ Not defined in the source: "correlate" / .corr(). The deck uses it as a ranking tool but never explains what the numbers mean or their range. Also note from the notebook: passed is computed directly from score (≥ 70), so a strong score correlation is built into this toy data.

18 · Encoding

major_encoded = pd.get_dummies(df["major"], prefix="major") X = pd.concat([X, major_encoded], axis=1) X.head()
Output age score major_Business major_CS major_Math 0 20 88 False True False 1 21 92 False False True 2 19 65 False True False 3 22 74 True False False 4 20 55 False True False
  • Why — models need numbers, not text; encoding converts categories into numeric columns.
  • get_dummies() — a quick, common way to one-hot encode a column. prefix="major" names the new columns major_Business / major_CS / major_Math.
  • concat — attaches the encoded columns to X, giving 5 columns.
⚠️ Source vs. run conflict: the deck says encoding makes "numeric columns," but when the notebook runs on current pandas the dummy columns display True / False (bool type), not 1 / 0. The deck doesn't address this. For the exam, keep the deck's wording: get_dummies converts categories into numeric columns. Also unexplained in the deck: axis=1 in pd.concat.

19–20 · Scaling vs normalization

from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_scaled = scaler.fit_transform(X[["age", "score"]]) print(X_scaled[:5]) from sklearn.preprocessing import MinMaxScaler normalizer = MinMaxScaler() X_norm = normalizer.fit_transform(X[["age", "score"]]) print(X_norm[:5])
ScalingNormalization
What it doesPuts features on a comparable range using their mean and standard deviationRescales features into a fixed [0, 1] range
ToolStandardScalerMinMaxScaler
Importsklearn.preprocessing — both
Columns usedX[["age", "score"]] only — dummy columns left out
First row on the run (Ali: 20, 88)[-0.55, 0.97][0.25, 0.86]
🧩 OOP reminder — object creation. scaler = StandardScaler() creates an instance of the StandardScaler class. In Java that's StandardScaler scaler = new StandardScaler(); — Python has no new; calling the class name builds the object. Then scaler.fit_transform(...) calls a method on that instance. Naming tip: CapitalizedWords = class (StandardScaler, MinMaxScaler); lowercase_with_underscores = function or method (train_test_split, get_dummies, fit_transform).
⚠️ Not explained in this deck: fit_transform() (see the Module 3 page, where fit learns the parameters and transform applies them) and the [:5] in print(X_scaled[:5]), which on the run prints the first 5 rows. The deck also gives no formula for either scaler.

21 · Train/test split

from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42 ) print("Train rows:", len(X_train)) print("Test rows:", len(X_test))
Output Train rows: 8 Test rows: 2
train · 8 rows
test · 2
  • Why — we hold back some data to fairly test the model after training.
  • train_test_split() — does this split for us in one line; returns X_train, X_test, y_train, y_test in that order.
⚠️ Not explained in the source: what test_size=0.2 and random_state=42 each control. The run shows 10 rows → 8 train / 2 test.
⚠️ Course-material conflict: here the scalers are fit on the full X before the split, and the split uses the unscaled X (X_scaled / X_norm are only printed). Module 3's lab did scaling after the split — fit_transform on X_train, transform on X_test. This page keeps the Lecture 4 code as written; ask Dr. Al Amin which order he expects on the midterm.

Flashcards

Question
Answer

Practice Quiz

Concept Check

Explain each objective in your own words, then self-grade against the rubric

Code Lab

Reproduce the Lecture 4 code from memory. Drill one slide, one part, or the Full program. Each non-blank line is scored pass / fail against the deck's code — spacing, indentation, quote style, and # comments are ignored, but names and capitalization must match (Python is case-sensitive: X ≠ x). Slide 22's multi-line train_test_split(…) call is scored as one line.

① Warm-up — put Part 3 in the deck's order
② Reproduce the code

Notes Template

Module 4 · Print this tab for handwritten recall practice

📚 Reference documentation (official — for lookup, not page content)

4 data types + examples

List vs dictionary

for vs while

Filter · sort · aggregate · groupby

Features X vs target y

get_dummies + concat

Scaling vs normalization

train_test_split → 4 outputs