Most data is not a table, yet
A farmers' advisory service in East Africa receives thousands of text messages a week, in English, Swahili and a mix of both. An agronomist with a phone takes photographs of diseased leaves. A cooperative's weather station records the sound of rain on a metal plate. Delivery drivers' phones log GPS points. A mobile-money network records who paid whom. A bike-share scheme logs every trip with a start time.
None of this arrives as the tidy table of machine learning for tabular data, but most of it ends up feeding one. The core skill is representation: turning words, pixels, samples, coordinates, links and timestamps into numbers, in a way that keeps what matters for the question and discards what does not. Each kind of data has its own standard representations, its own libraries, and its own characteristic mistakes. This lesson works through six of them with small, runnable examples, and points to where each one is taught in depth.
The examples are synthetic or invented, as always in this course. Real text, images, locations and networks are among the most sensitive data that exist about people, and each section says why.
Text: tokens, bags of words and TF-IDF
A model cannot read. The first step is tokenisation: splitting text into units, usually words or pieces of words. The simplest tokeniser lower-cases the text, keeps runs of letters, and drops stop words (the, is, of) that appear everywhere and say little. Real tokenisers handle much more: numbers, punctuation, accents, and languages without spaces between words (Chinese, Japanese and Thai) or with long compound words built from many parts (Swahili, Finnish, Turkish). Modern language models use subword tokenisers, such as byte-pair encoding, which learn a vocabulary of frequent fragments so that any word, even an unseen one, can be spelled out of known pieces (Sennrich and colleagues, 2016; see language models in Lumenary CS).
Definition— bag of words and TF-IDF
A bag of words represents a document by the count of each vocabulary word in it, ignoring order. TF-IDF (term frequency times inverse document frequency) reweights the counts so that words found in few documents count more. With documents, of which contain the word , one common smoothed form is
after which each document's vector is scaled to length 1. The cosine similarity of two documents is then the dot product of their vectors: 1 for the same direction, 0 for no words in common.
The block builds this from scratch for eight invented messages to the advisory helpline, checks the result against scikit-learn's TfidfVectorizer, and uses it to find the messages most similar to a new one.
import re
from collections import Counter
import numpy as np
# Messages to a farmers' advisory helpline (invented for this lesson).
docs = [
"Caterpillars are eating the maize leaves, there are holes in every leaf",
"When will the fertiliser delivery arrive in our village?",
"My payment for the maize I sold has not arrived",
"Small holes in the leaves of my beans and the leaves are turning yellow",
"The fertiliser delivery truck came but the bags were wet",
"Is it too late to plant maize after the rains started late?",
"Worms inside the maize cobs, how do I stop them spreading?",
"I sent the payment twice by mobile money, please refund one",
]
STOP = {"the", "a", "an", "are", "is", "in", "of", "my", "i", "and", "to", "for", "has", "not",
"there", "when", "will", "our", "but", "were", "it", "after", "how", "do", "them", "by", "one",
"every", "came", "please", "inside", "too"}
def tokenise(text):
"""Lower-case, keep runs of letters, drop very common words."""
return [w for w in re.findall(r"[a-z]+", text.lower()) if w not in STOP]
tokens = [tokenise(d) for d in docs]
print("tokens of message 0:", tokens[0])
vocab = sorted({w for t in tokens for w in t})
index = {w: j for j, w in enumerate(vocab)}
# Bag of words: counts of each vocabulary word in each document.
tf = np.zeros((len(docs), len(vocab)))
for i, t in enumerate(tokens):
for w, c in Counter(t).items():
tf[i, index[w]] = c
# TF-IDF: weight each count by how rare the word is across documents, then normalise each row.
df = (tf > 0).sum(axis=0) # documents containing each word
idf = np.log((1 + len(docs)) / (1 + df)) + 1 # the smoothed form scikit-learn uses
tfidf = tf * idf
tfidf /= np.linalg.norm(tfidf, axis=1, keepdims=True)
print(f"{len(docs)} documents, {len(vocab)} distinct words")
for w in ["maize", "leaves", "holes", "refund"]:
print(f" idf({w!r}) = {idf[index[w]]:.3f} (in {df[index[w]]} of {len(docs)} documents)")
def vectorise(text):
v = np.zeros(len(vocab))
for w, c in Counter(tokenise(text)).items():
if w in index: # words never seen in training are dropped
v[index[w]] = c * idf[index[w]]
n = np.linalg.norm(v)
return v / n if n else v
for query in ["holes appearing in maize leaves, what pest is this?", "insects damaging my crop"]:
sims = tfidf @ vectorise(query) # cosine similarity: rows are unit vectors
print(f"\nquery: {query!r}")
for i in np.argsort(-sims, kind="stable")[:3]:
print(f" {sims[i]:.3f} {docs[i]}")
# The same matrix from scikit-learn, given our tokeniser, for comparison.
from sklearn.feature_extraction.text import TfidfVectorizer
sk = TfidfVectorizer(tokenizer=tokenise, lowercase=False, token_pattern=None).fit(docs)
same = list(sk.get_feature_names_out()) == vocab and np.allclose(sk.transform(docs).toarray(), tfidf)
print("\nscikit-learn's TfidfVectorizer gives the same matrix:", same)The weights behave as intended: "maize" appears in half the messages, so it gets a low idf (1.588), while "refund" appears once and gets 2.504. The first query finds the two messages about holes in leaves, scored 0.613 and 0.572. The third result, a payment complaint, shares only the word "maize", and is a reminder that similarity is not relevance.
The second query shows the fundamental weakness. "Insects damaging my crop" means much the same as the first message, but shares no word with any message, so every similarity is exactly zero. Bag-of-words models know that "leaf" and "leaves" are different strings and nothing more; stemming (cutting words to a root) helps with the first problem, and nothing in the bag of words helps with synonyms. That is the problem embeddings solve.
Embeddings and pretrained models
Definition— embedding
An embedding is a dense vector of numbers (typically a few hundred to a few thousand), produced by a trained neural network, that represents an item (a word, a sentence, an image, a product) so that items with similar meaning or use have nearby vectors. Similarity between embeddings is usually measured with the same cosine similarity as above.
Word embeddings such as word2vec learned a vector for each word from which words appear near it (Mikolov and colleagues, 2013). Today's sentence embeddings come from transformer models trained so that paraphrases land close together and unrelated sentences far apart (Reimers and Gurevych, 2019; the architecture is explained in attention and transformers). With such a model, "insects damaging my crop" and "caterpillars are eating the maize leaves" get a high similarity despite sharing no words, and multilingual models place a Swahili message and its English translation near each other.
Data scientists rarely train these models. They use pretrained ones, downloaded from a public model repository or called through a provider's interface, in a few lines like these (shown, not run here; the model name is a placeholder):
# Not run here: needs a downloaded model (hundreds of MB) and, for speed, often a GPU.
from sentence_transformers import SentenceTransformer # one open-source library among several
model = SentenceTransformer("name-of-a-multilingual-embedding-model")
vectors = model.encode(messages, normalize_embeddings=True) # shape (n_messages, dimension)
similarity = vectors @ vectors.T # cosine similarities, as with TF-IDFThe same vectors then feed everything you already know: a logistic regression to route messages to the right expert, clustering to discover new topics, or a nearest-neighbour search to find past answers to similar questions. For millions of vectors, exact search is too slow, and libraries for approximate nearest-neighbour search (such as FAISS, Johnson and colleagues, 2017) or vector-search features in databases trade a little accuracy for a great deal of speed. Images get the same treatment, with vision models producing image embeddings; and since the 2020s, very large language models can classify or extract information from text directly when given instructions, which data science with AI discusses.
Before using a pretrained model, check five things. Licence: may you use it for your purpose? Model card: what was it trained on, and for which languages? A model trained mostly on English web text may embed Swahili or Hausa poorly, and tokenisers built for English split other languages into many more pieces, which makes them slower and costlier to process. Evaluation on your data: a few hundred labelled examples from your own domain tell you more than any leaderboard. Bias: embeddings absorb associations from their training text, including stereotypes. Privacy: sending farmers' messages to an external service is sharing personal data with a third party, which needs a legal basis, an agreement, and often their consent; a model run on your own hardware avoids that.
Images and audio are arrays
A greyscale image is a two-dimensional array of brightness values; a colour image adds a third axis of three channels (red, green, blue), usually as 8-bit unsigned integers from 0 to 255. Audio is a one-dimensional array of samples, the air pressure measured thousands of times a second. Once loaded (by libraries such as Pillow for images, or soundfile for audio), they are NumPy arrays like any other.
import numpy as np
# An image is an array: height x width (x 3 colour channels), usually 8-bit integers 0..255.
img = np.full((10, 14), 30, dtype=np.uint8) # a dark background
img[2:8, 4:11] = 200 # a bright rectangle (a leaf on soil, say)
img[4:6, 6:8] = 90 # a darker spot on it
print("shape", img.shape, "dtype", img.dtype, "| pixel at row 3, column 5:", img[3, 5])
shades = " .:-=+*#%@"
for row in img:
print(" " + "".join(shades[int(v) * (len(shades) - 1) // 255] * 2 for v in row))
# Edges: a 3x3 Sobel filter slid over the image (a convolution, the building block of CNNs).
kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
f = img.astype(float) # never do arithmetic in uint8: 200 + 100 wraps
gx = sum(kx[i, j] * f[i:i + 8, j:j + 12] for i in range(3) for j in range(3))
gy = sum(kx.T[i, j] * f[i:i + 8, j:j + 12] for i in range(3) for j in range(3))
edges = np.hypot(gx, gy)
print("edge strength (rows 1-8, columns 1-12), as a fraction of the maximum:")
for row in edges / edges.max():
print(" " + "".join(shades[int(v * (len(shades) - 1))] * 2 for v in row))
print("200 + 100 in uint8 arrays:", (np.array([200], np.uint8) + np.array([100], np.uint8))[0], "| as floats:", 200.0 + 100.0)
photo = (3000, 4000, 3)
print(f"a 12-megapixel colour photo as uint8: {np.prod(photo) / 1e6:.0f} MB in memory")
# Audio is a 1-D array of samples. One second at 8,000 samples per second:
rate = 8000
t = np.arange(rate) / rate
rng = np.random.default_rng(0)
signal = 0.6 * np.sin(2 * np.pi * 440 * t) + 0.3 * np.sin(2 * np.pi * 1250 * t) + rng.normal(0, 0.4, rate)
spectrum = np.abs(np.fft.rfft(signal)) / (rate / 2)
freqs = np.fft.rfftfreq(rate, 1 / rate)
top = sorted(np.argsort(-spectrum)[:2])
print("\nstrongest frequencies (Hz):", [int(freqs[k]) for k in top],
"| amplitudes:", [round(float(spectrum[k]), 2) for k in top],
f"| typical noise amplitude: {np.median(spectrum):.3f}")
print(f"highest frequency this sampling rate can represent: {rate // 2} Hz (the Nyquist frequency)")Four lessons are in this output. First, an image's coordinates are (row, column), with row 0 at the top, the opposite of a mathematical graph; libraries differ on this, and mixing conventions flips or transposes images. Second, a convolution (sliding a small grid of weights over the image) turns raw brightness into features, here edges: strongest at the corners of the rectangle, present around the dark spot, zero on flat areas. Convolutional neural networks learn thousands of such filters instead of using hand-designed ones (deep architectures). Third, 8-bit arithmetic wraps around: 200 + 100 is 44 in uint8, which silently ruins brightness adjustments; convert to floats first. Fourth, size: a single 12-megapixel photo is 36 MB as an array, so image datasets are processed in batches, resized, and stored compressed.
For audio, the Fourier transform turns a signal from a function of time into a sum of frequencies. Buried in noise whose amplitude (0.4) is comparable to the tones, the two frequencies we put in (440 Hz and 1,250 Hz) stand out clearly, with amplitudes close to the 0.6 and 0.3 we used, against a typical noise level of 0.008 per frequency. A signal sampled 8,000 times a second cannot represent anything above 4,000 Hz, the Nyquist frequency; higher frequencies fold back and masquerade as lower ones (aliasing). Speech and sound models usually work on spectrograms, Fourier transforms of short overlapping windows, which turn audio into an image-like array of frequency against time.
Photos and recordings carry hidden data. Photo files often store the camera's GPS position and the time in their metadata; a face, a voice or a licence plate can identify a person; and data-protection laws such as the EU's GDPR treat biometric data used to identify people as a special category requiring extra protection. Strip metadata you do not need, and think about who could be identified before collecting or publishing images or audio.
Geospatial data: coordinates, distances and projections
Positions on Earth are usually given as latitude (degrees north of the equator, from −90 to 90) and longitude (degrees east of the Greenwich meridian, from −180 to 180), on the WGS 84 reference ellipsoid that GPS uses. Two traps catch almost everyone once. Order: many APIs and people write (latitude, longitude), but GeoJSON and most GIS software store (longitude, latitude), and a swapped point can land thousands of kilometres away, still looking valid. Degrees are not kilometres: a degree of latitude is always about 111 km, but a degree of longitude shrinks with the cosine of the latitude, to about 56 km at 60° N.
Definition— haversine formula
For two points with latitudes and longitudes (in radians) on a sphere of radius , the great-circle distance is
With km, the mean radius, it is accurate to about 0.5% on the real, slightly flattened Earth; geodesic methods on the ellipsoid (in libraries such as PROJ and GeographicLib) are accurate to millimetres.
The haversine form is preferred to the spherical law of cosines because it stays numerically accurate for points close together. (The derivation uses spherical trigonometry; the plane version is trigonometry.)
import numpy as np
R = 6371.0 # mean Earth radius in km
def haversine(lat1, lon1, lat2, lon2):
"""Great-circle distance in km between points given in degrees (works on arrays too)."""
p1, p2 = np.radians(lat1), np.radians(lat2)
dp, dl = p2 - p1, np.radians(np.asarray(lon2) - np.asarray(lon1))
a = np.sin(dp / 2) ** 2 + np.cos(p1) * np.cos(p2) * np.sin(dl / 2) ** 2
return 2 * R * np.arcsin(np.sqrt(a))
def naive(lat1, lon1, lat2, lon2):
"""The common mistake: treat degrees as a flat grid, 111.2 km per degree in both directions."""
return 111.2 * np.hypot(np.asarray(lat2) - lat1, np.asarray(lon2) - lon1)
pairs = { # approximate city-centre coordinates
"Nairobi - Mombasa": (-1.286, 36.817, -4.043, 39.668),
"Quito - Guayaquil": (-0.180, -78.468, -2.170, -79.922),
"Delhi - Mumbai": (28.614, 77.209, 19.076, 72.878),
"Oslo - Bergen": (59.913, 10.752, 60.391, 5.322),
"Reykjavik - Akureyri": (64.147, -21.943, 65.684, -18.111),
}
print(f"{'':22s} {'haversine':>10s} {'naive':>8s} {'error':>7s}")
for name, (a, b, c, d) in pairs.items():
h, n = haversine(a, b, c, d), naive(a, b, c, d)
print(f"{name:22s} {h:8.0f} km {n:5.0f} km {100 * (n - h) / h:+6.0f}%")
print("\nWeb Mercator stretches lengths by 1/cos(latitude):")
for lat in (0, 30, 45, 60, 70):
print(f" latitude {lat:2d}: x{1 / np.cos(np.radians(lat)):.2f} in length, x{1 / np.cos(np.radians(lat)) ** 2:.2f} in area")
# Nearest clinic for 2,000 synthetic villages, near the equator and near 60 degrees north.
rng = np.random.default_rng(4)
for centre_lat in (0.0, 60.0):
v_lat = centre_lat + rng.uniform(-1, 1, 2000); v_lon = rng.uniform(-2, 2, 2000)
c_lat = centre_lat + rng.uniform(-1, 1, 15); c_lon = rng.uniform(-2, 2, 15)
d_true = haversine(v_lat[:, None], v_lon[:, None], c_lat[None, :], c_lon[None, :]) # 2000 x 15
d_naive = naive(v_lat[:, None], v_lon[:, None], c_lat[None, :], c_lon[None, :])
wrong = d_true.argmin(axis=1) != d_naive.argmin(axis=1)
extra = d_true[np.arange(2000), d_naive.argmin(axis=1)] - d_true.min(axis=1)
detour = f", sending them {extra[wrong].mean():.1f} km further on average" if wrong.any() else ""
print(f"around latitude {centre_lat:2.0f}: naive distances pick the wrong clinic for {wrong.mean():.1%} of villages{detour}")Near the equator, treating degrees as a flat grid happens to work: Nairobi to Mombasa is 441 km either way. At 60° N it doubles the Oslo–Bergen distance (606 km against a true 305 km), because that journey is mostly east–west. In the nearest-clinic problem, the same mistake sent 22.9% of synthetic Nordic villages to the wrong clinic, an average of 12.7 km further than necessary, while near the equator it changed nothing. A model can be correct in one country and wrong in another for reasons that have nothing to do with the model.
A map projection flattens the curved surface onto a plane, and every projection distorts something: areas, angles, distances or shapes. Web Mercator, used by most online maps, preserves local shapes and angles but stretches lengths by , so at 60° N areas look four times larger than they are; that is why Greenland looks the size of Africa on web maps, though Africa is about fourteen times larger. For computing areas, use an equal-area projection; for accurate distances and buffers over a region, a local projected system such as a UTM zone, in metres. The open-source stack most Python users rely on is GeoPandas (tables with geometries), Shapely (geometric operations) and pyproj (the Python interface to PROJ); databases such as PostGIS and many cloud warehouses offer the same operations in SQL. For large-scale joins ("which district is each of 50 million points in?"), spatial indexes do the work: R-trees, and hierarchical grids such as geohashes, Uber's H3 hexagons or Google's S2 cells.
Graphs and time-stamped events
Many datasets are really about relationships: who paid whom, which station a bike went to, which paper cites which, which supplier serves which factory. A graph represents them as nodes joined by edges, which may be directed and weighted (see graphs in Lumenary CS, and graph theory in Lumenary Math). In a table, a graph is simply an edge list, one row per edge; for computation it becomes an adjacency list or an adjacency matrix. The questions asked of graphs in data science are recurring ones: how connected is each node (its degree)? Which nodes are central? Which groups of nodes are tightly knit (communities)? What is the shortest route? Graph features, such as how many of an account's counterparties were flagged as fraudulent, often go straight into a tabular model.
Data with timestamps has its own rules. Store times in UTC, and convert to local time only for questions about human behaviour (rush hours, mealtimes), because local clocks jump with daylight-saving changes and differ between places. Keep both the time an event happened (event time) and the time it was recorded (processing time), because mobile apps and sensors upload late, and an analysis run at 09:00 may be missing a third of the events from 08:00. And remember that events are irregular: to compare them over time you usually count or aggregate them into regular bins, at which point the methods of time series apply.
The block puts both together: a synthetic week of bike-share trips in a city five hours behind UTC (Bogotá, say, which has no daylight saving), treated first as events and then as a graph. Many cities publish real trip data under open licences, in exactly this shape, if you want to repeat the analysis.
import numpy as np
import pandas as pd
# A week of synthetic bike-share trips in a city at UTC-5. Timestamps are stored in UTC, as they should be.
rng = np.random.default_rng(12)
stations = ["Hillside", "Riverside", "University", "Market", "Centre", "Rail station"]
homes, work = [0, 1, 2], [3, 4, 5]
rows = []
for day in range(7):
for _ in range(300): # morning: homes -> work, around 07:30 local
rows.append((day, rng.normal(7.5, 0.6), rng.choice(homes, p=[0.45, 0.35, 0.2]), rng.choice(work, p=[0.2, 0.5, 0.3])))
for _ in range(280): # evening: work -> homes, around 17:45 local
rows.append((day, rng.normal(17.75, 0.8), rng.choice(work, p=[0.25, 0.45, 0.3]), rng.choice(homes, p=[0.3, 0.4, 0.3])))
for _ in range(150): # errands through the day, anywhere to anywhere
a, b = rng.choice(6, 2, replace=False)
rows.append((day, rng.uniform(9, 21), a, b))
day, hour, start, end = map(np.array, zip(*rows))
local = (pd.Timestamp("2026-11-02") + pd.to_timedelta(day, unit="D") + pd.to_timedelta(hour, unit="h")).floor("min")
trips = pd.DataFrame({"start_utc": (local + pd.Timedelta(hours=5)).tz_localize("UTC"),
"start": np.array(stations)[start], "end": np.array(stations)[end]}).sort_values("start_utc")
print(trips.head(3).to_string(index=False))
# Hourly pattern: the busiest hours in UTC are not the busiest hours where the riders live.
utc_peak = trips["start_utc"].dt.hour.value_counts().nlargest(2).index.tolist()
local_time = trips["start_utc"].dt.tz_convert("-05:00")
local_peak = local_time.dt.hour.value_counts().nlargest(2).index.tolist()
print(f"\n{len(trips)} trips; busiest hours in UTC: {sorted(utc_peak)}; in local time: {sorted(local_peak)}")
# Net flow at the morning peak (07:00-09:59 local): who needs bikes brought in, and who needs them taken away?
morning = trips[local_time.dt.hour.between(7, 9)]
flow = pd.DataFrame({"departures": morning["start"].value_counts(), "arrivals": morning["end"].value_counts()}) / 7
flow["net change"] = flow["arrivals"] - flow["departures"]
print("\nmorning peak, bikes per day (average over the week):")
print(flow.reindex(stations).round(1).to_string())
# The trips as a weighted directed graph, and PageRank by power iteration.
W = pd.crosstab(trips["start"], trips["end"]).reindex(index=stations, columns=stations, fill_value=0).to_numpy(float)
P = W / W.sum(axis=1, keepdims=True) # where a bike leaving each station goes next
rank = np.full(6, 1 / 6)
for _ in range(100):
rank = 0.15 / 6 + 0.85 * rank @ P
print("\nstation weighted in-degree PageRank")
for s, indeg, r in sorted(zip(stations, W.sum(axis=0), rank), key=lambda x: -x[2]):
print(f"{s:14s} {indeg:12.0f} {r:14.3f}")Analysed in UTC, the busiest hours look like 12:00 and 22:00, a lunchtime and late-night pattern that does not exist; in local time they are 07:00 and 17:00, the commute. The morning-peak table is a net-flow analysis, and it is the daily problem of every bike-share operator: on an average morning, Hillside loses about 110 bikes and the Centre gains about 120, so vans must move bikes uphill before the next rush, or the empty stations turn customers away (the same censoring problem as the bakery in the data science workflow: an empty station records no demand).
The last table treats the week's trips as a weighted directed graph. PageRank, the algorithm Google's founders described in 1998 for ranking web pages, scores a node highly when it receives many edges from nodes that are themselves highly ranked. Here it has a physical reading: it approximates where a bike would spend its time if it kept being ridden along typical trips (with a 15% chance, at each step, of being moved anywhere by the operator). The power iteration computes the stationary distribution of a Markov chain. Here PageRank and the count of arrivals tell almost the same story (Centre first, Riverside second, Market last), as they usually do in a small, densely connected network. PageRank earns its keep on large, sparse networks, such as the web or a payment network, where who sends edges to a node matters more than how many arrive. For graphs with millions of edges, NetworkX (pure Python, very readable), igraph and graph databases provide these algorithms, and graph neural networks learn features from graph structure directly.
Graphs built from people's interactions, such as payments, messages and contacts, are among the most revealing data there are: who someone talks to can disclose their health, religion or politics without any of those being recorded. Treat them with at least the care you give to the individual records, and prefer aggregate analyses.
The professional view
In practice, data scientists rarely build representations for unstructured data from scratch any more. They choose a pretrained model or a standard library for each kind of data, turn items into vectors or features, and then use the tabular and statistical tools they already have. The skill lies in the choices: which representation keeps the information the question needs, how to evaluate it on your own data, and what it costs.
A rough guide, as of 2026. For text, start with TF-IDF and a linear model as a baseline (it is fast, cheap and explainable), then try sentence embeddings, and consider instructing a large language model when labels are scarce, checking its outputs against a labelled sample. For images and audio, fine-tune or extract embeddings from a pretrained model rather than training from scratch, and budget for labelling, storage and compute. For geospatial work, get coordinate systems right first and use GeoPandas or a spatial database; most errors are units, order and projection, not methods. For graphs, compute simple graph features and add them to a tabular model before reaching for graph neural networks. For events, agree on UTC, event time and late-data rules with the data engineers who build the pipelines (stream processing).
Each kind of data also has its own ethics, and they overlap: text, photos, voices, locations and social graphs all identify people. The rule of thumb is to collect and keep the least you need, aggregate early, and ask before sending data to any third party (responsible data science).
Common misconceptions
Misconception
"Unstructured data needs deep learning." Many text problems are solved well by TF-IDF and logistic regression, many image problems by a pretrained model's embeddings and a simple classifier, and many graph problems by a few counts. Start simple and measure what the complex model adds.
Misconception
"Latitude and longitude are just x and y." They are angles on a curved surface, stored in different orders by different tools. Distances need the haversine formula or a projection; at 60° N the flat-grid shortcut doubled the Oslo–Bergen distance.
Misconception
"Similar embeddings mean similar meaning." They mean similar to the model, given its training data. Two sentences can be close because they share a topic while saying opposite things ("the loan was approved" and "the loan was not approved"), and a model trained on one language or domain can be confidently wrong in another. Check on your own data.
Misconception
"Timestamps are unambiguous." A timestamp without a time zone is a guess, local clocks jump twice a year in many countries, and the time an event was recorded is not the time it happened. Store UTC, keep both times, and convert deliberately.
Exercises
1. Text, images and other kinds of data
Warm-upHumanIn a collection of 1,000 support messages to a mobile-money service, the word payment appears in 600 messages, reversed in 40, and Kisumu in 3. With TF-IDF, which word gets the largest weight when it appears once in a message?
2. Text, images and other kinds of data
Warm-upHumanA one-minute stereo recording is sampled at 44,100 samples per second per channel, with each sample stored as a 16-bit integer. How many bytes does the uncompressed audio take?
3. Text, images and other kinds of data
CoreHumanA clinic in Nairobi is at latitude −1.286, longitude 36.817. A dataset stores it as the GeoJSON point [36.817, -1.286], and an analyst's code reads the first number as latitude. What happens?
4. Text, images and other kinds of data
Warm-upHumanA sensor samples vibration 2,000 times per second. Frequencies above 1,000 Hz cannot be represented and fold back as false lower frequencies. What is the name of this limit, half the sampling rate? (One word, the person it is named after, is enough.)
5. Text, images and other kinds of data
CoreHumanA team wants to cluster 200,000 messages from farmers, in Swahili and English, by sending them to an external provider's embedding service. Which of these should they check before doing it? Select all that apply.
6. Text, images and other kinds of data
CoreComputationalWrite sessionise(events, gap_minutes=30). events is a DataFrame with columns user and time (datetimes), in any order. Return a copy, sorted by user and time, with a new column session: each user's events belong to the same session until there is a gap of more than gap_minutes between consecutive events of that user, and sessions are numbered 1, 2, 3, ... separately for each user.
The starter treats all users as one stream, so one user's activity can glue another user's sessions together.
7. Text, images and other kinds of data
CoreComputationalWrite within_km(lat, lon, places, km), which returns the names of the places within km kilometres of the point (lat, lon), by great-circle distance, sorted from nearest to farthest. places maps each name to a (lat, lon) pair in degrees. Use the haversine formula with km.
The starter uses the flat-grid shortcut, which the tests (set in a synthetic valley at about 61° N) will catch.
Further reading and resources
- Introduction to information retrieval · Christopher D. Manning, Prabhakar Raghavan and Hinrich Schütze · 2008The free online textbook on indexing, TF-IDF and the vector space model.
- Efficient estimation of word representations in vector space · Tomas Mikolov, Kai Chen, Greg Corrado and Jeffrey Dean · 2013The word2vec paper.
- Sentence-BERT: sentence embeddings using Siamese BERT-networks · Nils Reimers and Iryna Gurevych · 2019
- Neural machine translation of rare words with subword units · Rico Sennrich, Barry Haddow and Alexandra Birch · 2016Byte-pair encoding for tokenisation.
- Billion-scale similarity search with GPUs · Jeff Johnson, Matthijs Douze and Hervé Jégou · 2017
- Unique in the crowd: the privacy bounds of human mobility · Yves-Alexandre de Montjoye, César A. Hidalgo, Michel Verleysen and Vincent D. Blondel · 2013Four location points were enough to single out most people in a mobile-phone dataset.
- GeoPandas documentation
- PROJ, the coordinate transformation software
- NetworkX documentation