1
00:00:00,000 --> 00:00:04,240
An array stores values with a shared data
type and an explicit shape.

2
00:00:04,720 --> 00:00:08,080
It does not know which axis represents a
gene or a sample.

3
00:00:08,480 --> 00:00:12,960
Our matrix uses three gene rows and four
sample columns, with invented

4
00:00:13,040 --> 00:00:14,160
expression values.

5
00:00:14,560 --> 00:00:17,640
Every mean and subtraction depends on that
convention.

6
00:00:17,840 --> 00:00:22,000
A correct shape can still hide an
incorrect calculation when the matrix is

7
00:00:22,240 --> 00:00:22,680
square.

8
00:00:22,880 --> 00:00:26,960
We will follow the values through
reductions, broadcasting, selection, and

9
00:00:27,280 --> 00:00:31,440
assignment, then use a numerical check to
expose that silent error.

10
00:00:31,767 --> 00:00:35,820
Inspect the shape and data type before
choosing an operation.

11
00:00:35,874 --> 00:00:40,567
The shape three by four describes storage
dimensions, while our gene and sample

12
00:00:40,647 --> 00:00:42,887
labels describe their meaning.

13
00:00:43,047 --> 00:00:46,860
Calculate along the axis that contains the
values you want to combine.

14
00:00:46,994 --> 00:00:51,687
Reducing axis zero combines gene rows;
reducing axis one combines

15
00:00:51,887 --> 00:00:53,127
sample columns.

16
00:00:53,207 --> 00:00:56,927
Check the intended result with a known
value or an invariant.

17
00:00:56,967 --> 00:01:01,127
After centering each gene across its
samples, each gene's mean should be

18
00:01:01,487 --> 00:01:02,754
approximately zero.

19
00:01:02,780 --> 00:01:06,887
A calculation that runs is not sufficient
evidence for that claim.

20
00:01:07,133 --> 00:01:11,106
The input matrix contains three rows and
four columns.

21
00:01:11,160 --> 00:01:16,066
Its first gene has values five point one,
six point three, two point two,

22
00:01:16,120 --> 00:01:17,493
and two point eight.

23
00:01:17,533 --> 00:01:22,093
Axis zero combines the three values in
each sample column, leaving four

24
00:01:22,533 --> 00:01:23,013
means.

25
00:01:23,053 --> 00:01:27,973
Axis one combines the four values in each
gene row, leaving three means.

26
00:01:28,013 --> 00:01:30,866
The first gene's mean is four point one.

27
00:01:31,000 --> 00:01:35,453
Notice that the axis argument names the
dimension being reduced, not the dimension

28
00:01:35,533 --> 00:01:37,573
whose labels remain in the answer.

29
00:01:37,613 --> 00:01:40,973
State that meaning before you memorize an
axis number.

30
00:01:41,233 --> 00:01:45,313
Keep the reduced dimension with keepdims
equals true.

31
00:01:45,353 --> 00:01:50,273
The row means then have shape three by
one, and the expression matrix has shape

32
00:01:50,353 --> 00:01:51,686
three by four.

33
00:01:51,820 --> 00:01:56,593
Compare dimensions from the right: one can
expand across four, and three matches

34
00:01:56,673 --> 00:01:57,433
three.

35
00:01:57,473 --> 00:02:00,380
Subtract each mean across its own gene
row.

36
00:02:00,406 --> 00:02:04,993
The result still has three rows and four
columns, with row means approximately

37
00:02:05,073 --> 00:02:05,900
zero.

38
00:02:06,086 --> 00:02:10,033
Broadcasting does not need to build four
stored copies of the mean column, although

39
00:02:10,073 --> 00:02:12,833
the subtraction still creates its result
array.

40
00:02:13,067 --> 00:02:17,867
Remove keepdims and the three row means
form a one dimensional array of length

41
00:02:17,947 --> 00:02:18,747
three.

42
00:02:18,867 --> 00:02:23,387
That shape cannot subtract from the
original three by four matrix, so NumPy

43
00:02:23,467 --> 00:02:24,707
raises an error.

44
00:02:24,747 --> 00:02:28,707
On a square three by three matrix, the
same expression runs.

45
00:02:28,747 --> 00:02:33,347
The three computed gene means align with
columns instead of their own rows.

46
00:02:33,387 --> 00:02:37,947
NumPy does not recompute column means; it
applies the supplied numbers in the wrong

47
00:02:38,507 --> 00:02:39,147
direction.

48
00:02:39,227 --> 00:02:40,854
Check row means afterward.

49
00:02:40,880 --> 00:02:45,787
They are approximately zero point nine
three three, minus zero point six, and

50
00:02:45,947 --> 00:02:49,467
minus zero point three three three, rather
than zero.

51
00:02:49,700 --> 00:02:54,607
Integer storage cannot retain the
fractional part of nine point nine nine.

52
00:02:54,713 --> 00:02:57,900
Assigning it into this integer array
stores nine.

53
00:02:57,940 --> 00:03:02,340
Convert the array to floating storage
before assigning the fractional value, and

54
00:03:02,460 --> 00:03:04,820
nine point nine nine remains.

55
00:03:04,900 --> 00:03:08,740
Fixed width integers have another
boundary: an eight bit signed array

56
00:03:08,860 --> 00:03:13,460
containing one hundred twenty wraps to
minus one hundred twenty six when ten is

57
00:03:13,540 --> 00:03:14,473
added.

58
00:03:14,607 --> 00:03:17,500
Python's built in integer gives one
hundred thirty.

59
00:03:17,540 --> 00:03:22,073
Inspect the actual dtype before
interpreting an arithmetic result,

60
00:03:22,287 --> 00:03:25,700
particularly when counts can exceed a
narrow integer range.

61
00:03:25,933 --> 00:03:28,413
Compare each count with one hundred.

62
00:03:28,493 --> 00:03:33,400
The five counts are one hundred twenty,
four hundred fifty five, eighty nine,

63
00:03:33,426 --> 00:03:35,653
twelve hundred, and thirty three.

64
00:03:35,693 --> 00:03:39,773
The Boolean mask records true, true,
false, true, and

65
00:03:39,853 --> 00:03:40,760
false.

66
00:03:40,786 --> 00:03:45,586
It has the same shape as the input, and
summing it counts the selected positions.

67
00:03:45,720 --> 00:03:49,173
Retrieve the counts with that mask to
obtain three values.

68
00:03:49,213 --> 00:03:53,613
Combine multiple array conditions with the
elementwise operators and parentheses

69
00:03:53,693 --> 00:03:55,333
around each comparison.

70
00:03:55,373 --> 00:03:59,773
Python's and operator instead asks for the
truth value of an entire

71
00:04:00,253 --> 00:04:00,813
operand.

72
00:04:01,033 --> 00:04:05,753
A basic slice usually shares numeric
storage with its original array.

73
00:04:05,953 --> 00:04:10,233
Changing the first element of the slice
from index one through index three

74
00:04:10,353 --> 00:04:15,086
therefore changes the original value at
index one to nine hundred ninety nine.

75
00:04:15,140 --> 00:04:18,046
An integer index array retrieves a copy.

76
00:04:18,180 --> 00:04:22,606
Changing that copy to eight hundred eighty
eight leaves the original unchanged.

77
00:04:22,740 --> 00:04:26,673
Direct assignment to an indexed selection
on the original still changes the

78
00:04:26,713 --> 00:04:27,633
original.

79
00:04:27,673 --> 00:04:31,353
Distinguish retrieving a separate object
from assigning through the original

80
00:04:31,433 --> 00:04:36,153
expression, and use shares memory when you
need to inspect that relationship.

81
00:04:36,400 --> 00:04:40,720
An ordinary sum propagates a missing
floating value represented by

82
00:04:40,880 --> 00:04:41,760
NaN.

83
00:04:41,840 --> 00:04:46,560
A sum that excludes NaN instead adds the
observed entries, which answers

84
00:04:46,720 --> 00:04:49,480
a different question about incomplete
measurements.

85
00:04:49,520 --> 00:04:54,440
An all missing vector has a NaN sum of
zero when you use nansum.

86
00:04:54,480 --> 00:04:58,560
That zero does not prove that any
observation was measured, and it cannot

87
00:04:58,680 --> 00:05:01,307
serve as a valid normalization
denominator.

88
00:05:01,333 --> 00:05:06,320
The written exercise treats an all zero
sample as undefined and fills its fraction

89
00:05:06,400 --> 00:05:09,760
column with NaN before applying a division
mask.

90
00:05:10,000 --> 00:05:14,453
Use a nonsquare matrix to make the two
reduction lengths different.

91
00:05:14,587 --> 00:05:18,880
Include a square matrix to test the case
where the wrong axis still produces a

92
00:05:19,040 --> 00:05:20,040
valid shape.

93
00:05:20,080 --> 00:05:24,960
Compare actual values with an independent
expected answer, and use a numerical

94
00:05:25,040 --> 00:05:27,200
tolerance for floating results.

95
00:05:27,280 --> 00:05:31,973
Check ownership when the calculation
promises to leave its input unchanged.

96
00:05:32,027 --> 00:05:36,640
The full lesson includes two exercises
with concealed answers, one for sample

97
00:05:36,760 --> 00:05:39,467
fractions and another for array copies.

98
00:05:39,493 --> 00:05:43,920
Solve them before opening the explanation,
and state the row and column meanings in

99
00:05:44,000 --> 00:05:44,880
your answer.
