Python
Scientific Computing & Data Science
49 flashcards · answers and spaced-repetition review in the KnowCard app
You need to add two large sequences of numbers element by element, fast. Why is a NumPy ndarray faster than a Python list for this?
You want to compute x*3 - 4 for a million numbers in a. What's the idiomatic NumPy way, and what's the mistake to avoid?
What does this print?
```
import numpy as np
a = np.array([1, 2, 3])
print(a + 10)
```
What does this print?
```
import numpy as np
m = np.ones((3, 2))
v = np.array([10, 20])
print((m + v)[0])
```
You try np.array([1,2,3]) + np.array([10,20]). What happens and why?
What does this print?
```
import numpy as np
a = np.array([1, 2, 3])
a[0] = 7.9
print(a)
```
What does this print?
```
import numpy as np
a = np.array([100, 100], dtype=np.int8)
print(a[0] + a[1])
```
What does this print?
```
import numpy as np
a = np.array([1, 2, 3, 4])
b = a[1:3]
b[0] = 99
print(a)
```
What does this print?
```
import numpy as np
a = np.array([-2, 5, -1, 3])
print(a[a > 0])
```
For a 2D array a, what's the difference between a.sum(axis=0) and a.sum(axis=1)?
What does df["City"] return for a pandas DataFrame — a DataFrame or a Series? And what about df[["City", "State"]]?
A pandas DataFrame df has the default integer index 0..3. What's the core difference between df.loc[1] and df.iloc[1]?
A DataFrame df has a custom index ["i1", "i2", "i3", "i4"]. You write df.loc[1] to get the second row. What happens?
What does this print?
```
import pandas as pd
s = pd.Series([10, 20, 30, 40])
print(len(s.loc[1:3]))
```
You write df[df["Pop"] > 100]["State"] = "X" to update matching rows, and pandas raises SettingWithCopyWarning while the data doesn't change. Why, and what's the correct call?
In pandas, how do you select rows where Population > 2,000,000 AND State is not Texas? What's the subtle bug developers hit?
Your DataFrame has some NaN cells. What do df.dropna() and df.fillna(0) each do, and what's the catch about the original frame?
You assign a new row to a DataFrame via a Series that's missing one of the columns. What value goes into the missing column?
You extracted a sub-block b = a[1:4, 1:4] from a 2D array to experiment on, then modified b. Later a is unexpectedly changed too. What went wrong and how do you fix it?
What's the purpose of groupby in pandas, and what does a bare df.groupby("State") actually return?
You run a script with plt.plot(X, Y) and nothing appears on screen. What's missing?
You want to draw two curves on the SAME matplotlib figure with a legend. What's the call pattern, and what's the common mistake?
pandas lets you write df.City to get a column instead of df["City"]. When does the attribute form fail?
With pandas .loc, how do you select specific rows AND columns at once, and how do you say "all rows"?
By default a pandas DataFrame gets a 0..N row index. What are the two ways to give it a meaningful custom index instead?
You give a pandas DataFrame a custom index but some label values repeat. What's the risk when you look up by that label?
Among NumPy's reshape, transpose, flatten, and ravel, which return a view sharing data and which return a copy?
You need the position (index) of the largest value in a NumPy array, not the value itself. Which method, and how does it differ from max?
pandas has two main data structures, Series and DataFrame. How do they differ?
What's the simplest way to build a pandas DataFrame from column data, and what index do you get?
Given a DataFrame df, how do you get its row count, its per-column data types, and its column labels?
In pandas, df.drop(0) removed a row but df still shows it afterward. And how would you drop a column instead?
You pd.concat([df, df2]) two DataFrames but the row index now has duplicate numbers. What parameter fixes it, and how do you concatenate side-by-side instead?
How do you save a pandas DataFrame to CSV and read one back, and what's the gotcha with Excel files?
Someone lists NumPy, SciPy, matplotlib, and pandas as the scientific-Python core. What is each one for, and where do they live?
What are the conventional import aliases for the scientific-Python stack that examples assume?
You write import scipy as sp then call sp.integrate.quad(...) and get an AttributeError. Why?
You have a NumPy array a and call math.sin(a). What happens, and what should you use instead?
What does this print?
import numpy as np
print(np.linspace(-1, 1, 5))
You wrote a function with an if x > 0 branch and want to apply it to a whole NumPy array, but the branch can't handle arrays. What's the fix?
You call result = scipy.integrate.quad(f, 0, 4) and then try to use result as a number, but it behaves like a tuple. What does quad actually return?
What does plt.plot([5, 3, 8, 1]) draw when passed a single list of numbers?
In a matplotlib legend you write label="$f'$" with surrounding dollar signs. What do the dollar signs do?
What does this print?
import numpy as np
b = np.array([[1, 2], [3, 4], [5, 6]])
print(b.shape)
You allocate a = np.empty((3, 3)) and print it, expecting zeros, but get strange numbers. Why?
Besides np.array(...), name the NumPy functions to build (a) an all-zeros/ones array, (b) an identity-style matrix, (c) an array with given values on the diagonal.
For a 2D NumPy array a, how do you read/write a single element, and how do you assign one value to a whole sub-block?
You have a NumPy array a and want its number of dimensions, total element count, and total bytes used. Which attributes give each?
How do you make a bar chart directly from a pandas DataFrame, and what happens to the x-axis if you don't specify one?
Start learning today
Free to start — download the app or use it in your browser.
