Intermediate Python for Data Science Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Give an example for plotting

A

The most basic plot is the line plot. A general recipe is given here.

import matplotlib.pyplot as plt

plt.plot(x,y)

plt.show()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you ‘log’ the scale of an axis for plotting?

A

plt.xscale(‘log’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Exapmle of how to change Yticks

A

Filip has demonstrated how you could control the y-ticks by specifying two arguments:

plt.yticks([0,1,2], [“one”,”two”,”three”])

In this example, the ticks corresponding to the numbers 0, 1 and 2 will be replaced by one, two and three, respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Dictionaries can contain dictionaries.

A

Remember lists? They could contain anything, even other lists. Well, for dictionaries the same holds. Dictionaries can contain key:value pairs where the values are again dictionaries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Good to Remember

A

In the sample code on the right, the same cars data is imported from a CSV files as a Pandas DataFrame. To select only the cars_per_cap column from cars, you can use:

cars[‘cars_per_cap’]

cars[[‘cars_per_cap’]]

The single bracket version gives a Pandas Series, the double bracket version gives a Pandas DataFrame.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

To Remember

A

Square brackets can do more than just selecting columns. You can also use them to get rows, or observations, from a DataFrame. The following call selects the first five rows from the cars DataFrame:

cars[0:5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To Remember

A

Each pair of commands here gives the same result.

cars. loc[‘RU’]
cars. iloc[4]
cars. loc[[‘RU’]]
cars. iloc[[4]]
cars. loc[[‘RU’, ‘AUS’]]
cars. iloc[[4, 1]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Using the ‘for’ loop

A

fam = [1.73, 1.68, 1.71, 1.89]

for height in fam :

print(height)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

example of how to use the ennumerate ()

A

fam = [1.73, 1.68, 1.71, 1.89]

for index, height in enumerate(fam) :

print(“index “ + str(index) + “: “ + str(height))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to iterate over a dictionary?

A

world = { “afghanistan”:30.55, “albania”:2.77, “algeria”:39.21 }

for key, value in world.items() :

print(key + “ – “ + str(value))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you iterate over a 2D array?

A

for x in np.nditer(my_array) : …

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

matplotlib : To Remember

A

Remember how you could use matplotlib to build a line plot?

import matplotlib.pyplot as plt

plt.plot(x, y)

plt.show()

The first list you pass is mapped onto the x axis and the second list is mapped onto the y axis.

If you pass only one argument, Python will know what to do and will use the index of the list to map onto the x axis, and the values in the list onto the y axis.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly