0% found this document useful (0 votes)
74 views

02 - Numpy Tutorial

Numpy is a library for working with numeric data and matrices in Python. It provides multidimensional arrays and matrix math functions. Pandas uses Numpy arrays internally to store data. Common functions for Numpy arrays include sum, mean, slicing, and broadcasting operations between arrays of different shapes. Numpy operations are often much faster than equivalent base Python code due to being written in lower-level languages like C and Fortran.

Uploaded by

hemant kurmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

02 - Numpy Tutorial

Numpy is a library for working with numeric data and matrices in Python. It provides multidimensional arrays and matrix math functions. Pandas uses Numpy arrays internally to store data. Common functions for Numpy arrays include sum, mean, slicing, and broadcasting operations between arrays of different shapes. Numpy operations are often much faster than equivalent base Python code due to being written in lower-level languages like C and Fortran.

Uploaded by

hemant kurmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

4 Martingale

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 !
=

frame values is a dumpy


pandas .
Data .

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

nd meant ) → 0.005 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 -

knows nothing of roulette


given
boolean odds

input odds of win → win / lose output

Run trials Cqsisod


ed of experiment .

episode ? one gambler in Vegas


? martingale win ? keep your bet , plus
strategy gain equal amount to bet

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

→ can 't spend what you


don't have
Heft
)
( bet what is

You might also like