Skip to content

Data

AP Computer Science Principles Topic 2 8:13 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
Play a song. 放一首歌。
What reaches your ear is a wave — smooth, continuous, never jumping from one value to the next. 传到你耳朵里的是一段波——平滑、连续,从不跳跃。
But your phone cannot store a wave. It can only store numbers. 但你的手机存不了一段波,它只能存数字。
So it measures the wave's height, thousands of times every second, and writes each measurement down as a number. 于是它每秒测量这段波的高度几千次,把每一次测量都写成一个数。
That is sampling: something smooth turned into a list of values. 这就是采样:把连续的东西变成一串数值。
Every photo, every message and every video on that phone came through the same narrow door. 手机里的每一张照片、每一条消息、每一段视频,都是从这同一道窄门进来的。
This is Big Idea Two: data. 这是大概念二:数据。
How a computer writes everything down in ones and zeros, what it loses when the bits run out, how compression shrinks a file, and how a program turns a pile of raw data into an answer you can defend. 计算机如何用 0 和 1 把一切记录下来, 当位数不够时它会丢失什么,压缩如何让文件变小, 以及程序如何把一堆原始数据变成一个你能站得住脚的答案。
Let's begin. 让我们开始吧。
Start with the smallest piece. 先从最小的一块开始。
A bit is one binary digit — a zero or a one, and nothing else. 位(bit)就是一个二进制数字——要么 0,要么 1,没有别的。
Why only two? 为什么只有两个?
Because a wire is either on or off, and two states are the easiest thing in the world to build reliably. 因为一根导线要么通电要么断电,两种状态是最容易可靠实现的。
Put eight bits together and you have a byte. 把八个位放在一起,就得到一个字节。
Now the rule the exam wants: every extra bit doubles how much you can say. 下面是考试要考的规律:每多一个位,你能表达的东西就翻一倍。
One bit gives two patterns, two bits give four, three bits give eight. 一个位有两种组合,两个位有四种,三个位有八种。
In general, n bits give two to the power n different values — so a byte gives two hundred and fifty-six. 一般来说,n 个位给出 2 的 n 次方种取值——所以一个字节有二百五十六种。
So how do we write an actual number using only zeros and ones? 那么,怎样只用 0 和 1 写出一个真正的数呢?
Exactly the way you count in decimal, except each place is worth twice the place on its right instead of ten times. 方法和你数十进制完全一样,只是每一位的权重是右边一位的两倍,而不是十倍。
One, then ten, then eleven, then one hundred. 1,然后 10,然后 11,然后 100。
Watch the pattern as it counts: the rightmost bit flips at every single step, the next flips half as often, and the one after that half as often again. 看它数数时的规律: 最右边那一位每一步都翻转,下一位翻转的频率只有它的一半, 再往左一位又减半。
That is all binary counting is. 二进制计数就这么简单。
Now the conversion the exam always asks for, in both directions. 现在是考试一定会考的转换,而且两个方向都要会。
To read binary, write the place values above the bits: eight, four, two, one. 读二进制时,在每一位上方写出位权:8、4、2、1。
For one, one, zero, one, add the places that carry a one — eight plus four plus one is thirteen. 对于 1、1、0、1,把带 1 的那些位权加起来——8 加 4 加 1 等于 13。
Going the other way, take nineteen and keep subtracting the largest power of two that still fits. Sixteen leaves three; two leaves one; one leaves nothing. 反过来,取 19,不断减去还能放得下的最大的 2 的幂: 减 16 剩 3,减 2 剩 1,减 1 剩 0。
So the ones sit at sixteen, two and one, and nineteen in binary is one, zero, zero, one, one. 所以 1 落在 16、2 和 1 这几位上,19 的二进制就是 1、0、0、1、1。
Always check by adding the places back up. 最后一定要把位权加回去检验一次。
Long strings of bits are painful for people to read, so we group them. 一长串的位对人来说很难读,所以我们把它们分组。
Four bits make exactly one hexadecimal digit, counting zero to nine and then A to F for ten up to fifteen. 每四个位正好对应一个十六进制数字:从 0 数到 9,然后用 A 到 F 表示 10 到 15。
That turns eight bits into two neat characters. 这样八个位就变成了两个整齐的字符。
Be clear about what this is: it is not a new kind of data. 要弄清楚这是什么: 它并不是一种新的数据。
The machine still stores nothing but bits. 机器存的仍然只有位。
Hexadecimal is only a shorter way for a human to write those same bits down. 十六进制只是让人把同样这些位写得更短的一种方式。
But a computer never has infinite bits, and that costs you in two different ways. 但计算机永远不会有无限多的位,这会以两种方式让你付出代价。
First, overflow. 第一是溢出。
Eight bits can hold zero up to two hundred and fifty-five. 八个位能表示 0 到 255。
Ask them to hold two hundred and sixty and the value simply cannot fit, so what comes back is wrong — and often nothing warns you. 让它装 260,这个值根本放不下,返回的结果就是错的——而且常常没有任何警告。
Second, round-off, or rounding, error. 第二是舍入误差。
There are infinitely many decimal numbers but only a finite set of bit patterns, so most decimals are approximated — stored as a very close value instead of the exact one. 十进制小数有无穷多个,而位的组合只有有限种, 所以大多数小数只能以一个非常接近的近似值被存下来,而不是精确值。
Each error is tiny, but added up many times it becomes visible. 每一次误差都极小,但累加很多次之后就会变得明显。
Everything else is built on those same numbers. 其余的一切都建立在同样这些数字之上。
Text is a lookup table: every character is given a number — capital A is sixty-five — and that number is stored as bits. 文本是一张对照表:每个字符都被指定一个数字——大写 A 是 65—— 这个数字再以位的形式存下来。
An image is a grid of pixels, and each pixel is a small set of numbers giving its colour. 图像是一格一格的像素, 每个像素是一小组表示颜色的数字。
Sound is the sampling we met at the start: the height of the wave, many thousands of times a second. 声音就是我们开头见过的采样: 每秒记录几千次波的高度。
Three completely different meanings, one single storage. 三种完全不同的含义,同一种存储方式。
Underneath, it is all binary. 在底层,全都是二进制。
Files get big, so we compress them — we store exactly the same thing using fewer bits. 文件会变大,所以我们要压缩它——用更少的位存下完全相同的东西。
The simplest trick is to stop repeating yourself. 最简单的办法就是不要重复自己。
If a row of a picture is the same colour forty times over, do not write that colour forty times. 如果一张图片的某一行连续四十个像素颜色相同, 就不要把那个颜色写四十遍。
Write the colour once, and then write forty. 写一次颜色,再写一个四十。
Undo it and you get the original back, every bit of it, because the repetition was never information in the first place. 还原时你能一位不差地拿回原件,因为那些重复本来就不携带信息。
That gives us the two families, and the exam wants both. 这就给出了两大类,而且考试两类都要考。
Lossless compression can rebuild the original bit for bit, because nothing was thrown away. 无损压缩能把原件一位不差地还原,因为什么都没有被丢掉。
It is what you must use for text, for programs, for a spreadsheet — anywhere a single wrong bit matters. 文本、程序、表格——凡是一个位错了就出问题的地方,都必须用无损压缩。
Lossy compression deliberately throws away detail your eye or ear will not miss, which shrinks the file very much further. 有损压缩则故意丢掉你的眼睛或耳朵察觉不到的细节,能把文件缩得小得多。
That is what photos, music and video use. 照片、音乐和视频用的就是它。
The choice is size against fidelity, and lossy is permanent: once the detail is gone, no amount of decompressing brings it back. 取舍在于大小与保真度之间,而且有损是不可逆的:细节一旦丢掉,再怎么解压也回不来。
Here is real lossless compression at work. 这是真正的无损压缩在工作。
Instead of spending the same number of bits on every character, first count how often each one appears. Then give the common characters short codes and the rare ones long codes. 与其给每个字符花同样多的位,不如先数一数每个字符出现的频率, 然后给常见字符短的编码,给罕见字符长的编码。
The total shrinks, sometimes by half. 总长度就变小了,有时能减半。
And because no short code is the beginning of a longer code, you can always read the stream back with no ambiguity at all. 而且因为没有哪个短编码是某个长编码的开头, 你总能毫无歧义地把这串数据读回来。
Nothing was lost — it was only rewritten more cleverly. 什么都没丢——只是被更聪明地重写了一遍。
Now, data is not the same as information. 再说,数据不等于信息。
Data is the raw record; information is what you can actually answer with it. 数据是原始的记录;信息是你真正能用它回答出来的东西。
Extracting information from data is work. 从前者到后者是要下功夫的。
Real data is dirty — missing entries, misspelled names, two different formats for the same date — so first it is cleaned. 真实的数据是脏的——缺失的条目、拼错的名字、 同一个日期有两种格式——所以第一步是清洗。
Then it is filtered, keeping only the rows your question needs, and transformed into the shape that answers it. 然后是过滤,只保留你的问题需要的那些行, 再把剩下的转换成能回答问题的形式。
Metadata is data about data, like a photo's date and location, and it is what makes a huge collection searchable at all. 元数据是关于数据的数据,比如照片的日期和位置, 正是它让一个庞大的集合变得可以检索。
And here is the warning that carries marks. 下面是能拿分的一个警告。
Two things can rise and fall together without either one causing the other. 两件事可以一起升降,却谁也不是谁的原因。
Ice-cream sales and drowning accidents both climb every summer, but ice cream does not drown anyone. Hot weather drives both of them. 冰淇淋销量和溺水事故每年夏天都会上升,但冰淇淋不会淹死任何人, 是炎热的天气同时推高了两者。
That is a correlation, not a cause: one thing causes another only if changing it changes the other. 这叫相关性,不是因果关系: 只有当改变一件事会改变另一件事时,它才算原因。
A very large data set makes correlations easier to find, which also makes it easier to claim a cause you have not actually shown. 数据集越大,越容易找到相关性,也就越容易宣称一个你其实并没有证明的因果。
On the exam, say plainly that correlation does not prove causation — and name a third factor that could explain both. 在考试里,要明确地说:相关不等于因果——并且指出一个能同时解释两者的第三个因素。
This is where a program earns its keep. 这正是程序发挥价值的地方。
Filtering, cleaning and visualizing are all far faster in code: it can filter a million rows in a second, clean them by a rule instead of by hand, and draw a chart that makes a pattern obvious in an instant — patterns a small table would never have shown you. 它能在一秒内过滤一百万行, 按规则而不是靠人手去清洗,并画出一张让规律瞬间显现的图—— 这些规律在一张小表格里你永远看不出来。
Make that chart interactive and other people can explore the data and reach their own conclusions. 再把图做成可交互的,别人就能自己探索数据,得出他们自己的结论。
Join two data sets together and you can see more still, which is exactly why combining sources raises a privacy concern: two pieces that are harmless apart can identify a real person once they are put side by side. 把两个数据集连接起来,你能看到更多,而这也正是合并数据源会引发隐私问题的原因: 两条单独看无害的信息,一旦并排放在一起,就可能识别出一个真实的人。
Three habits for this unit. 这一单元有三个习惯。
First, practise the conversions until they are quick — binary to decimal, decimal to binary, and hexadecimal when a question asks for it. 第一,把转换练到又快又准—— 二进制转十进制、十进制转二进制,题目要求时还有十六进制。
Second, be able to say why finite bits cause overflow and round-off, and which of the two a given scenario is showing you. 第二,要能说清楚为什么有限的位数会带来溢出和舍入误差, 以及某个具体情景展示的是哪一种。
Third, when a question hands you a correlation, never call it a cause; name a third factor that could explain both sides of it. 第三,当题目给你一个相关性时,绝不要把它当成因果; 指出一个能同时解释两边的第三个因素。

Log in or create account

IGCSE, A-Level & AP