Sei sulla pagina 1di 18

Lecture 10

Python and Data Science


Previous Session
• Python basics
• Object Oriented Programming
Today’s Lecture

Data Science and Python


Data Science and Python
• In this era of internet technology, we are used
to handle, manipulate and even
unintentionally give access to almost un-
limited scale of information to others.
Book for 2nd Module (Data Sciences)
Data Science and Python
• Data science is a "concept to unify statistics,
data analysis and their related methods" in
order to "understand and analyze actual
phenomena" with data
Data Science and Python
Understanding Data Relationships
users = [ friendships = [
{ "id": 0, "name": "Hero" }, (0, 1),
(0, 2),
{ "id": 1, "name": "Dunn" }, (1, 2),
{ "id": 2, "name": "Sue" }, (1, 3),
{ "id": 3, "name": "Chi" }, (2, 3),
{ "id": 4, "name": "Thor" }, (3, 4),
{ "id": 5, "name": "Clive" }, (4, 5),
(5, 6),
{ "id": 6, "name": "Hicks" }, (5, 7),
{ "id": 7, "name": "Devin" }, (6, 8),
{ "id": 8, "name": "Kate" }, (7, 8),
{ "id": 9, "name": "Klein" } (8, 9)
]
]
Understanding Data Relationships
Getting Started!
Lets add an friends list for each user to
track each user’s friends

for user in users:


user["friends"] = []
0, 1
for i, j in friendships:
users[i]["friends"].append(users[j])
users[j]["friends"].append(users[i])
Understanding Connections
Processing Data
Lets find out the total number of connections by
adding the length of all the friends list

def number_of_friends(user):
return len(user["friends"])

total_connections =
sum(number_of_friends(user) for user in users)
Processing Data
num_users = len(users)
# length of the users list

#Total connections
avg_connections = total_connections /
num_users
Finding Mutual Friends
def friends_of_friend_ids_bad(user):
return [foaf["id"] for friend in user["friends"] for foaf in friend["friends"] ]
Map
• Takes an array and a processing function and returns a
new array.
Example>

def process(num):
return num + 2;

list = [2, 3, 4, 5, 6]
map(process, list)

#4, 5, 6, 7, 8
Lambda Functions
• Lambda functions are small anonymous
functions without name.
• They are used when they are supposed to be
used in a certain way.
Example>
Normal Function Lambda Function

def sum(num): lambda x: x + 2


return num + 2
Using Lambda Functions
list = [2, 3, 4, 5, 6]
map(lambda x: x + 2, list)

#4, 5, 6, 7, 8
Today’s Assignment
• From the given dataset, extract the towns
with the most property crimes.

(Data available in Student’s drive)

Potrebbero piacerti anche