【课程】Introduction to Data Science in Python Week 1

week 1: Python Fundamentals

  • Python Dates and Times
  • Advanced Python Objects, map()
  • Advanced Python Lambda and List Comprehensions
  • Advanced Python Demonstration: The Numerical Python Library (Numpy)

Python Dates and Times

import datetime as dt
import time as tm
tm.time() #time returns the current time in seconds since the Epoch. (January 1st, 1970)
dtnow=dt.datetime.fromtimestamp(tm.time())
dtnow #convert the timestamp to datetime.
dtnow.year, dtnow.month, dtnow.day, dtnow.hour, dtnow.minute, dtnow.second #handy datetime attributes

#timedelta is a duration expressing the difference between two dates.
delta = dt.timedelta(days = 100)
today = dt.date.today()
today - deltaf #the data 100 days ago

Advanced Python Objects, map()

Here’s an example of mapping the min function between two lists.

store1 = [10.00, 11.00, 12.34, 2.34]
store2 = [9.00, 11.10, 12.34, 2.01]
cheapest = map(min, store1, store2)
cheapest
#the output is
<map at 0x7f17ec103a58>

for item in cheapest:
	print(item)
#the output is 
9.00
11.00
12.34
2.01

Eg. Here is a list of faculty teaching this MOOC. Can you write a function and apply it using map() to get a list of all faculty titles and last names (e.g. [‘Dr. Brooks’, ‘Dr. Collins-Thompson’, …]) ?

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

def split_title_and_name(person):
    return #Your answer here

list(map(#Your answer here))

Here’s a solution:

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

def split_title_and_name(person):
    title = person.split()[0]
    lastname = person.split()[-1]
    return '{} {}'.format(title, lastname)

list(map(split_title_and_name, people))

Advanced Python Lambda and List Comprehensions

Here’s an example of lambda that takes in three parameters and adds the first two.

my_function = lambda a, b, c : a+b

The following two are the same by applying list comprehension:

#Without list comprehension
my_list = []
for number in range(0, 1000):
    if number % 2 == 0:
        my_list.append(number)
my_list
#With list comprehension
my_list = [number for number in range(0,1000) if number % 2 == 0]
my_list

Eg. Convert this function into a lambda:

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

def split_title_and_name(person):
    return person.split()[0] + ' ' + person.split()[-1]

#option 1
for person in people:
   print(split_title_and_name(person) == (lambda person:???))

#option 2
#list(map(split_title_and_name, people)) == list(map(???))

Here’s a solution

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

def split_title_and_name(person):
    return person.split()[0] + ' ' + person.split()[-1]

#option 1
for person in people:
    print(split_title_and_name(person) == (lambda x: x.split()[0] + ' ' + x.split()[-1])(person))

#option 2
list(map(split_title_and_name, people)) == list(map(lambda person: person.split()[0] + ' ' + person.split()[-1], people))

Advanced Python Demonstration: The Numerical Python Library (Numpy)

Create a list and convert it to a numpy array

mylist = [1, 2, 3]
x = np.array(mylist)

Or just pass in a list directly

y = np.array([4, 5, 6])
y

Arange returns evenly spaced values within a given interval.

n = np.arange(0, 30, 2) # start at 0 count up by 2, stop before 30

linspace returns evenly spaced numbers over a specified interval.

o = np.linspace(0, 4, 9) # return 9 evenly spaced values from 0 to 4

Distinguish between * and repeat

import numpy as np
np.array([1, 2, 3] * 3)
>>array([1, 2, 3, 1, 2, 3, 1, 2, 3])

np.repeat([1, 2, 3], 3)
>>array([1, 1, 1, 2, 2, 2, 3, 3, 3])

argmax and argmin return the index of the maximum and minimum values in the array.

你可能感兴趣的:(课程,python,numpy)