NumPy Tutorial: Arrays, Properties & Operations

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which NumPy function is used to create an array with random values between 0 and 1?

  • np.array()
  • np.randint()
  • np.arange()
  • np.random.rand() (correct)

The np.arange() function includes the end value in the generated array.

False (B)

What attribute of a NumPy array returns its dimensions?

shape

To change the dimensions of an NumPy array, you can use the ______ method.

<p>reshape</p>
Signup and view all the answers

When using the sum() function on a NumPy array, what does the axis parameter control?

<p>The direction along which the sum is calculated. (D)</p>
Signup and view all the answers

The np.polyld() function can only be used with NumPy arrays containing integer values.

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

What function is used to write a NumPy array to a text file?

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

To read data from a text file into a NumPy array, use the ______ function.

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

What function should you use to find the number of elements in a NumPy array?

<p>np.size (A)</p>
Signup and view all the answers

The np.fromfile() method can save a NumPy array to a file.

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

Given arr = np.arange(1, 10), what values will arr contain?

<p>[1, 2, 3, 4, 5, 6, 7, 8, 9] (C)</p>
Signup and view all the answers

What is the outcome of running arr = np.array([1, 2, 3, 4]); arr.reshape(2, 2)?

<p>A 2D array with shape (2, 2): <code>[[1, 2], [3, 4]]</code> (C)</p>
Signup and view all the answers

With arr = np.array([1, 5, 2, 9, 3]), what NumPy function returns the maximum value present in arr?

<p>amax() or max()</p>
Signup and view all the answers

By default, NumPy's savetxt() function saves data in binary format.

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

In np.random.randint(1, 12, size=(3, 4)), the array created will contain integers ranging from __ to __.

<p>1, 11</p>
Signup and view all the answers

Match the following NumPy functions with their descriptions:

<p>np.array() = Creates an array from a list or tuple np.arange() = Creates an array with a range of numbers np.reshape() = Changes the shape of an array np.sum() = Calculates the sum of array elements</p>
Signup and view all the answers

Given arr = np.array([[1, 2, 3], [4, 5, 6]]), what will arr.sum(axis=0) return?

<p>[5, 7, 9] (C)</p>
Signup and view all the answers

NumPy's reshape() method modifies the array in place.

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

If arr = np.array([1, 2, 3, 4, 5]), how can you extract the elements from index 1 up to (but not including) index 4?

<p>arr[1:4]</p>
Signup and view all the answers

To represent the polynomial $x^2 - 2x + 3$ using NumPy, you would use np.polyld(np.array([1, -2, 3])). The coefficients are listed in order of ______ power.

<p>decreasing</p>
Signup and view all the answers

Flashcards

np.array()

Creates a NumPy array with the specified elements.

np.arange(start, stop)

Creates a NumPy array with a range of numbers.

np.random.rand(rows, cols)

Creates an array with random values between 0 and 1.

np.random.randint(low, high, size)

Creates an array with random integers within a specified range.

Signup and view all the flashcards

array.size

Returns the number of elements in the array.

Signup and view all the flashcards

array.shape

Returns the dimensions of the array (rows, columns).

Signup and view all the flashcards

array.reshape(rows, cols)

Changes the shape of an array without changing its data.

Signup and view all the flashcards

array[start_row:end_row, start_col:end_col]

Accesses specific elements or subarrays within an array.

Signup and view all the flashcards

array.sum()

Calculates the sum of array elements.

Signup and view all the flashcards

array.sum(axis=0 or 1)

Calculates the sum along a specified axis (0 for columns, 1 for rows).

Signup and view all the flashcards

array.max()

Finds the maximum value in an array.

Signup and view all the flashcards

np.polyld(array)

Defines a polynomial function from an array of coefficients.

Signup and view all the flashcards

np.savetxt()

Saves an array to a text file.

Signup and view all the flashcards

np.loadtxt()

Loads data from a text file into an array.

Signup and view all the flashcards

array.tofile()

Saves an array to a binary file.

Signup and view all the flashcards

array.fromfile()

Loads data from a binary file into an array.

Signup and view all the flashcards

Study Notes

NumPy Library

  • NumPy is imported as np.

Array Creation

  • np.array([1,2,3,4]) creates an array with the specified values.
  • np.arange(1,10) generates a range of numbers from 1 to 9.
  • np.random.rand(3,4) produces a 3x4 array with random values between 0 and 1.
  • np.random.randint(1,12, size=(3,4)) creates a 3x4 array with random integers from 1 to 11.

Array Properties and Manipulation

  • arr4.size returns the total number of elements in the array (e.g., 12).
  • arr4.shape returns the dimensions of the array as a tuple (e.g., (3,4)).
  • ar4.reshape(4,3) changes the dimensions of the array to 4x3.
  • np.random.randint(1,25, size=(2,3,4)) creates a 3D array, with order 3D -> 2D -> 1D.
  • np.arange(1,25).reshape(2,3,4) reshapes a range into a 2x3x4 array.
  • ar5[0,0:2,1:3] accesses a specific slice of the array.

Array Operations

  • ar5.sum() calculates the sum of all elements in the array.
  • ar5.sum(axis=0) sums the values vertically.
  • ar5.sum(axis=1) sums the values horizontally.
  • ar5.max() finds the maximum value in the array.
  • ar5.max(axis = 0) finds the maximum values along axis 0.
  • ar5.max(axis = 1) finds the maximum values along axis 1.

Mathematical Functions

  • ar6 = np.array([1,-2,3]) creates an array of coefficients.
  • f = np.polyld(ar6) defines a polynomial function using the coefficients in ar6 (e.g., x**2 - 2x + 3).

File I/O

  • np.savetxt() saves array data to a file, with parameters for path, filename, data, format, and delimiter.
  • np.loadtxt() loads array data from a file, with parameters for path and filename.
  • ar5.fromfile() reads data from a file to create an array.
  • ar5.tofile() writes array data to a file.

Studying That Suits You

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

Quiz Team

Related Documents

Numpy Tutorial

More Like This

Numpy Mastery Quiz
5 questions

Numpy Mastery Quiz

UnequivocalGreenTourmaline avatar
UnequivocalGreenTourmaline
Numpy.ones(): Creating Arrays of Ones
6 questions
NumPy Array Indexing and Slicing
6 questions
NumPy Arrays in Python
18 questions
Use Quizgecko on...
Browser
Browser