NumPy Arrays and Linear Algebra
27 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will the output of a.sum(axis=0) be for the array a = [[1, 2, 3, 4], [5, 6, 7, 8]]?

  • [3, 7, 11]
  • [36]
  • [10, 26]
  • [ 6, 8, 10, 12] (correct)
  • Which function would you use to find the maximum element in a NumPy array along a specific axis?

  • max() (correct)
  • argmax()
  • mean()
  • sum()
  • Which of the following methods would return the standard deviation of an entire NumPy array if no axis is specified?

  • sort()
  • mean()
  • std() (correct)
  • prod()
  • What does the lstsq() function in NumPy's linalg submodule return?

    <p>The answer that minimizes the square error.</p> Signup and view all the answers

    What will the output of np.linalg.solve(A, b) be for the given matrices if A is invertible?

    <p>The solution vector x of the equation Ax = b.</p> Signup and view all the answers

    Which function is used to compute the dot product of two matrices in NumPy?

    <p>dot()</p> Signup and view all the answers

    What is the purpose of the inv() function in the linalg submodule?

    <p>To return the inverse of a matrix.</p> Signup and view all the answers

    Which function would you use to compute the eigenvalues and eigenvectors of a given matrix in NumPy?

    <p>eig()</p> Signup and view all the answers

    If A is a square matrix, what does the det() function compute?

    <p>The determinant of the matrix.</p> Signup and view all the answers

    What will happen if argmax() is called on a NumPy array?

    <p>It will return the index of the maximum element in the array.</p> Signup and view all the answers

    What happens when a value in a view array is modified?

    <p>Both the view and original array are affected.</p> Signup and view all the answers

    How can you determine if an array is a view?

    <p>Check if the base attribute is None.</p> Signup and view all the answers

    Which method can be used to create a copy of a view array?

    <p>copy()</p> Signup and view all the answers

    Which of the following operations does not create a view?

    <p>Using the copy method</p> Signup and view all the answers

    What will the line 'v = M[0,:]' return?

    <p>The first row of the array M.</p> Signup and view all the answers

    What does the expression 'M.base' return if M is an original array?

    <p>None.</p> Signup and view all the answers

    Which of the following correctly demonstrates changing a view without altering the original array?

    <p>Use v = M.copy() and then modify v.</p> Signup and view all the answers

    When comparing arrays with '==' operator, what is a requirement for the arrays?

    <p>They must be of the same shape or broadcastable.</p> Signup and view all the answers

    Which of the following best describes an array view?

    <p>An array that shares data with another array.</p> Signup and view all the answers

    What are functions that act element-wise on NumPy arrays called?

    <p>Universal functions</p> Signup and view all the answers

    Which function would give you the ceiling values of a NumPy array?

    <p>np.ceil</p> Signup and view all the answers

    Using the vectorize decorator in NumPy allows you to convert which type of function?

    <p>A function that acts on a single value</p> Signup and view all the answers

    What does the np.tan function do when applied to an array?

    <p>Computes the tangent of each element in radians</p> Signup and view all the answers

    What would be the result of using np.exp on the array [1, 2, 3]?

    <p>[2.71828183, 7.3890561, 20.08553692]</p> Signup and view all the answers

    Which of the following functions will convert an array to its floor values?

    <p>np.floor</p> Signup and view all the answers

    The output of np.round([1.5, 2.5, 3.5]) will be?

    <p>[2.0, 3.0, 4.0]</p> Signup and view all the answers

    Which of the following is an example of a universal function in NumPy?

    <p>np.sqrt</p> Signup and view all the answers

    Study Notes

    NumPy Arrays

    • NumPy arrays (ndarrays) offer various methods for operations on arrays, including mathematical functions.
    • Universal functions (ufuncs) operate element-wise on arrays; many are already defined in NumPy (e.g., cos, tan, exp, log, ceil, floor, round).

    Creating Universal Functions

    • Python functions can be converted into universal functions using np.vectorize.
    • Alternatively, a decorator @np.vectorize can be applied directly above the function definition.

    Array Methods

    • NumPy arrays (ndarray) have methods for specific operations (e.g., sum, min, max, mean, std, prod).
    • These methods often operate along a specified axis.
    • Function equivalents with the same name typically exist within the NumPy module. For instance, the sum function can also calculated by ndarray.sum()

    Linear Algebra Functions

    • NumPy's linalg submodule provides linear algebra operations.
    • The solve function can solve systems of equations with invertible matrices ($Ax = b$).
    • For non-invertible matrices, lstsq (least squares) minimizes the square error.

    Array Views

    • Array views share data with the original array.
    • Changes in a view affect the original array.
    • The base attribute of an array can distinguish between original arrays and views.
    • Operations like reshaping and transposing make views of the original array.

    Boolean Operations

    • Boolean operations on arrays yield boolean arrays (e.g., comparisons using operators like ==, !=, <, >).
    • The all function checks if all elements are True; any checks if at least one element is True.

    Integer Array Indexing

    • Integer arrays select elements from arrays;
    • Colons (:) in indices access all elements in the corresponding dimension.

    Boolean Array Indexing

    • Boolean array indexing selects elements based on a boolean condition.
    • Boolean selection can be applied on multiple dimensions using boolean arrays with dimensions that correspond to the array shape.

    Assignment by Array Indexing

    • The selected elements in arrays using indexing can be updated with new values (broadcastable).

    The where Function

    • np.where(condition, x, y) can choose elements based on a condition.
    • If x and y are not given, the function returns indices where a condition is True.
    • If both x and y are given, the output returns values from x where a condition is True and y otherwise.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Lecture 11: NumPy Arrays PDF

    Description

    This quiz covers essential concepts related to NumPy arrays, including their creation, universal functions, array methods, and linear algebra functions. Test your understanding of how to perform various mathematical operations using NumPy and its capabilities for handling array data effectively.

    More Like This

    Pandas Data Structures and Dimensions Quiz
    12 questions
    BMAN73701 Week 3 Numerical Analysis Quiz
    41 questions
    Week 2: Introduction to NumPy
    37 questions
    Use Quizgecko on...
    Browser
    Browser