02 - Numpy Tutorial
02 - Numpy Tutorial
Numpy
due
first project
weekend !
next
( N 10 days)
What is numpy? How does it relate to pandas?
Matlab for
humpy is Python
-
matrix ( vector math library
-
written in C & Fortran
-
fast !
=
nd
array is
a data frame really a
fancy
-
so
"
"
for a dumpy array
wrapper
How do I create a numpy array?
for
array ( sequence , tuple
or list -
np existing
those
,
.
or 2-17 of data
np .
empty ( dimensions : ( 419) ) - unallocated
np . zeros
ones
( dins )
( dins )
-
init
initto
-60
I
I -
np . -
4×9 array
How do I create a random numpy array?
Uniform
" P - random . random ( dins ) : [ o , 1) ←
•
normal ( mean
,
Std
,
size -4,4 ) )
• randintllow , high ,size=C44))
random seed ( seed) Num
np
l
"
dims 1×4 here
.
-
"
,
y
low inclusive
2%
Oto -
high exclusive
"
"
selects your
pseudorandom
sequence
number
for same
seed ! )
( same
How do I slice arrays? Can I assign to them?
ncz.ci/onedementnf4[J-4IotheendnCz:4,o:2Tc-
2×2 ( exclusive) 4
n[ "
4) ← upto 2nd
column 4
NC 4) ← every
:D
:
> ← column
0[27 ME :S :
NC I
]
-
>
T T n[o : too :3
bottom 2 columns p
Oto 100
row rows
V ( exclusive ,
t 3rd
elements every
first two row
of last
What are the basic attributes of ndarray?
Crows)
nd shape
.
→ ( 5 , 4) shape
-
size
→ 20 ←
teitealments "
CD Cools)
- d type →
float 64
What are the common functions of ndarray?
'
nd sum C)
)
.
:
o
nd axis =
. sum
÷÷÷÷÷÷÷i÷÷÷ ¥¥¥¥
slice assignment
fine !
works
Is numpy really that much faster than base Python?
co ) : )
range (
Ii 7 )
nd shape
for i in .
for j in
range
( nd shape .
:
sum + = nd Ci , j)
print ( sum Ind . size → 3 see
on same 10 million
Hosted
element ZD array)
What the heck is Broadcasting? Mask or Boolean arrays? What does nd1*nd2 do?
broadcasts scalar 4
"
"
← numpy
nd C :D to a vector
of the right size
I
6. D
'
IetFito :¥II¥
]
€-49.74
O
idx =
[ I
,
4 ,
I ,
-
- -
-
Eid D →
]
↳
nd
Et Ce 79
as index to dumpy
dumpy array fine
works just
array
How does broadcasting make numpy “expressive” ? (Daily returns / Cumulative returns)
-
later
nd E nd s o ] =
O works !
elements to zero
sets all negative
.
a boolean array
( nd a O returns
selects the
,
it as index
using
True positions )
The Martingale project description is long! Nutshell?
? roulette wheel -
bet 41 on black ←
lose ? lose your bet
2x bet
when lose :
at
$ I
when win : start back
hit limit
stop
win % ? 36 red ,
black
)
"
18 "
wheel
- Cif fair
36
to wheel Cred , )
black both lose
→ add green
fair wheel
→ pays l: l as
( but you
if
lose > 50% of the time ! )
Experiment :
per episode
-
1000 spins
hit win / loss limit ? stop belting
Track after each
total winnings spin "
$ go
win limit always
not winnings
€¥¥¥i÷÷÷÷÷
empty your
spins wallet
Exp l : inf bankroll !
a) run to episodes npobntohepfoef Lahti
b) run 1000 episodes
¥
meant
Std
→
plot mean , ,
mean
-std
per roll #
→
plot median CH std - )
always , stop belting
.
1000 spins
Exp 2 :
4256 bankroll
)
.
2 ( med
→ same plots ,
mean