Data Science with Python Certification Introduction to Python for Data Science 3 — Questions and Answers
Question 1: Which NumPy function is used to create an array of evenly spaced values over a specified interval?
- np.arange()
- np.linspace() (Correct answer)
- np.range()
- np.space()
Correct answer: np.linspace()
`np.linspace(start, stop, num)` returns `num` evenly spaced values between `start` and `stop` inclusive.
Question 2: What does the `shape` attribute of a NumPy array return?
- The total number of elements
- A tuple of the array's dimensions (Correct answer)
- The number of axes
- The data type of elements
Correct answer: A tuple of the array's dimensions
The `shape` attribute returns a tuple representing the size of each dimension of the array.
Question 3: Which pandas method is used to read a CSV file into a DataFrame?
- pd.load_csv()
- pd.read_csv() (Correct answer)
- pd.open_csv()
- pd.import_csv()
Correct answer: pd.read_csv()
`pd.read_csv()` is the standard pandas function for reading comma-separated value files into a DataFrame.
Question 4: In a pandas DataFrame, what does `df.head(3)` return?
- The last 3 rows
- The first 3 rows (Correct answer)
- The first 3 columns
- A summary of 3 statistics
Correct answer: The first 3 rows
`df.head(n)` returns the first `n` rows of the DataFrame, defaulting to 5 if no argument is passed.
Question 5: What is broadcasting in NumPy?
- Sending array data over a network
- Performing element-wise operations on arrays of different shapes (Correct answer)
- Copying an array to multiple variables
- Converting a 1D array to 2D
Correct answer: Performing element-wise operations on arrays of different shapes
Broadcasting allows NumPy to perform arithmetic operations on arrays of different shapes by virtually expanding the smaller array.
Question 6: Which of the following correctly selects rows in a pandas DataFrame where column 'age' is greater than 30?
- df['age' > 30]
- df[df['age'] > 30] (Correct answer)
- df.select(age > 30)
- df.filter('age > 30')
Correct answer: df[df['age'] > 30]
Boolean indexing with `df[df['age'] > 30]` filters rows where the condition evaluates to True.
Question 7: What is the primary purpose of `matplotlib.pyplot.show()` in a script?
- Saves the plot to a file
- Renders and displays the current figure (Correct answer)
- Clears the current figure
- Returns the figure object
Correct answer: Renders and displays the current figure
`plt.show()` renders all open figures and displays them, blocking execution until the windows are closed.
Which NumPy function is used to create an array of evenly spaced values over a specified interval?