Download lecture_02

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Visualizing Data
Outline
•
•
•
•
•
•
Sorting
List Comprehension
Randomness
OO Programming
Functional Tools
Viz in Python
whoa, trippy visuals bro..
1
Sorting in Python
>>> x = [8,6,7,5,3,0,9]
• Python list has sort method
>>> y = sorted(x)
• sorts in place
• Can also use sorted method
• won’t modify list
• Sorts in increasing order
• by default
>>> print x
[8, 6, 7, 5, 3, 0, 9]
>>> print y
[0, 3, 5, 6, 7, 8, 9]
>>> x.sort()
>>> print x
[0, 3, 5, 6, 7, 8, 9]
2
Outline
•
•
•
•
•
•
Sorting
List Comprehension
Randomness
OO Programming
Functional Tools
Viz in Python
whoa, trippy visuals bro..
3
List Comprehension
• Very “Pythonic”
• Succinct code:
>>> x2 = [ x**2 for x in range(5) ]
>>> print x2
[0, 1, 4, 9, 16]
>>>
4
List Comprehension
• Can add if statement
>>> evens = [x for x in range(5) if x % 2 == 0]
>>> print evens
[0, 2, 4]
>>>
5
List Comprehension
• Can add if / else statement
>>> nums = range(2,8)
>>> evenOdd0 = [x if x%2 == 0 else 0 for x in nums]
>>> nums
[2, 3, 4, 5, 6, 7]
>>> evenOdd0
[2, 0, 4, 0, 6, 0]
6
List Comprehension
• Use _ (underscore) if you don’t care about the value
>>> y = [ 'howdy' for _ in range(5) ]
>>> print y
['howdy', 'howdy', 'howdy', 'howdy', 'howdy']
>>>
7
List Comprehension
• Can make a dictionary
>>> d = {x : x**2 for x in range(5)}
>>> print d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>>
8
Outline
•
•
•
•
•
•
Sorting
List Comprehension
Randomness
OO Programming
Functional Tools
Viz in Python
whoa, trippy visuals bro..
9
Random Values in Python
• Used very often in data science
• Use the random module provided by Python
• Many useful functions
•
•
•
•
•
•
10
random( )
seed( )
randrange( )
shuffle( )
choice( )
sample( )
>>> import random
Random Values in Python
• random( )
• Generates random decimal value
• Uniform b/t 0 and 1
>>> import random
>>> randos = [random.random() for _ in range(5)]
>>> print randos
[0.4211796536240664, 0.7845286543311286,
0.3737666933857522, 0.8522130532650195,
0.19951748918740808]
>>>
11
Random Values in Python
• seed( )
• Used to seed random number generator
>>> random.seed(0)
>>> random.random()
0.8444218515250481
>>> random.seed(0)
>>> random.random()
0.8444218515250481
>>>
12
Random Values in Python
• randrange( )
• Takes 1 or 2 arguments
• Chooses randomly from a range
>>> import random
>>> r = [ random.randrange(100) for _ in range(5) ]
>>> print r
[51, 40, 78, 30, 47]
>>>
13
Random Values in Python
• shuffle( )
• Randomly reorders elements in a list
• Shuffle is done in place
>>> x = range(5)
>>> print x
[0, 1, 2, 3, 4]
>>> random.shuffle(x)
>>> print x
[1, 0, 2, 4, 3]
14
>>>
Random Values in Python
• choice( )
• Randomly picks one element from a list
>>> bff = random.choice(['Justin','Brian','Alan'])
>>> print bff
Alan
15
Random Values in Python
• sample( )
• Randomly chooses samples without replacement
• NO DUPLICATES!
>>> nums = random.sample(range(100), 6)
>>> print nums
[80, 54, 1, 71, 39, 82]
>>>
16
Outline
•
•
•
•
•
•
Sorting
List Comprehension
Randomness
OO Programming
Functional Tools
Viz in Python
whoa, trippy visuals bro..
17
OO Programming
• May see Python Classes and Objects
>>> class Car:
...
make="Mercedes"
...
def __init__(self):
...
print "constructor!"
...
def fx(self):
...
print self.make
...
>>> c = Car()
constructor!
18
>>> c.fx()
Mercedes
__init__ is the constructor
ALL class methods take self as
first parameter
I know, it’s gross..
Outline
•
•
•
•
•
•
Sorting
List Comprehension
Randomness
OO Programming
Functional Tools
Viz in Python
whoa, trippy visuals bro..
19
Functional Tools
• Python has built-in functions that can be useful
•
•
•
•
20
map
filter
reduce
enumerate
Functional Tools
• map – applies function to all items in a list
• Takes at least two parameters
• 1 ) function
• 2+) iterable items (e.g., list)
>>> L = range(5)
>>> L = range(5)
>>> def f(y): return y**2
>>> def f(y): return y**2
>>> x2 = map(f, L)
>>> xs = [f(x) for x in L]
>>> print x2
[0, 1, 4, 9, 16]
>>> print xs
[0, 1, 4, 9, 16]
21
Functional Tools
• map – applies function to all items in a list
• Takes at least two parameters
• 1 ) function
• 2+) iterable items (e.g., list)
>>> def M(x,y): return x*y
>>> X = [1,2,3,4]
>>> Y = [5,6,7,8]
>>> Z = map(M,X,Y)
22
>>> print Z
[5,12,21,32]
Functional Tools
• filter- creates list of objects for which function returns true
• Takes two parameters
• 1 ) function (returns True or False)
• 2) iterable item (e.g., list)
>>> def isOdd(x): return x%2==1
>>> X = range(5)
>>> print X
[0, 1, 2, 3, 4]
>>> filter(isOdd,X)
[1, 3]
23
Functional Tools
• filter- creates list of objects for which function returns true
• Takes two parameters
• 1 ) function (returns True or False)
• 2) iterable item (e.g., list)
>>> def isEven(x): return x%2==0
>>> X = range(5)
>>> X
[0, 1, 2, 3, 4]
>>> filter(isEven,X)
[0, 2, 4]
24
Functional Tools
• reduce- perform computation on a list, returns result
• Takes two parameters
• 1 ) function
• 2) iterable item (e.g., list)
>>> def mySum(x,y): return x+y
...
>>> X = range(5)
>>> sum(X)
10
>>> reduce(mySum,X)
10
25
Functional Tools
• enumerate – get index and element in a list
>>> X = range(10,15)
>>> random.shuffle(X)
>>> X
[14, 12, 11, 10, 13]
26
>>> for i,x in enumerate(X):
...
print i,x
0 14
1 12
2 11
3 10
4 13
Functional Tools
• Favor enumerate over range( len(..) )
>>> X = range(10,15)
>>> random.shuffle(X)
>>> X
[14, 12, 11, 10, 13]
27
>>> for i in range(len(X)):
...
print i,X[i]
...
0 14
1 12
not very Pythonic...
2 11
3 10
4 13
Outline
•
•
•
•
•
•
Sorting
List Comprehension
Randomness
OO Programming
Functional Tools
Viz in Python
whoa, trippy visuals bro..
28
Data Visualization in Python
• Visualizing data is fundamental to data science
• Exploring data (looking for patterns)
• Sharing pertinent information
• Creating visualizations is easy
• Creating great visualizations is hard
• Literally books written on subject
29
Data Visualization in Python
• Will focus on matplotlib.pyplot module
•
•
•
•
•
•
•
30
plot( )
show( )
title( ), ylabel( ), xlabel( ), legend( ), axes( ), ...
hist( )
bar( )
scatter( )
savefig( )
plot( )
>>> import matplotlib.pyplot as plt
>>> X = [x**2 for x in range(10)]
>>> plt.plot(X)
>>> plt.xlabel('x')
>>> plt.ylabel('x**2')
>>>31 plt.title('awesome title')
>>> plt.show()
plot( )
>>> import matplotlib.pyplot as plt
>>> X = [x**2 for x in range(10)]
>>>32 plt.plot(range(10),X,’go’)
>>> plt.show()
hist( )
>>>
>>>
>>>
>>>
33
>>>
data = [random.random() for _ in range(10000)]
plt.hist(data)
plt.xlabel('bins')
plt.ylabel('count')
plt.show()
bar( )
NOTE: lookup
xtick(..) if you want
custom x labels
>>>
>>>
>>>
34
>>>
>>>
x = range(10)
y = [random.random() for _ in x]
plt.bar(x,y)
plt.title('bar chart')
plt.show()
scatter( )
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
35
>>>
x1 = range(10)
x2 = range(10)
random.shuffle(x1)
random.shuffle(x2)
y1 = [random.random() for _ in x1]
y2 = [random.random() for _ in x2]
plt.scatter(x1,y1,color='g')
plt.scatter(x2,y2,color='r')
plt.legend(['green','red'])
plt.show()
Related documents