Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge 2: Array Manipulation | NumPy
Data Science Interview Challenge

book
Challenge 2: Array Manipulation

NumPy offers a comprehensive set of tools for modifying and reshaping arrays. The key benefits include:

  • Diverse Capabilities: From reshaping to splitting, NumPy offers a wide range of functions to modify arrays as per requirements.

  • In-place Operations: Many NumPy functions can perform modifications in-place, meaning without creating a copy of the array, ensuring efficient memory usage.

  • Intuitive Syntax: Array manipulations often have a very readable and straightforward syntax, making the code self-explanatory.

The ability to seamlessly manipulate arrays is a cornerstone of many numerical and data science tasks, making these functions indispensable.

Compito

Swipe to start coding

Manipulate arrays to change their shape and get desired elements.

  1. Extract the diagonal elements.
  2. Retrieve the elements in the second row.
  3. Extract the 2nd and 3rd rows and change their shape to (5, 2).

Soluzione

import numpy as np

# Given the following array
arr = np.arange(0, 25).reshape(5, 5)
print(arr, '\n')

# 1. Extract the diagonal elements.
diagonal_elements = np.diag(arr)
print(diagonal_elements)

# 2. Retrieve the elements in the second row.
second_row = arr[1]
print(second_row)

# 3. Extract the 2nd and 3rd rows and change their shape to (5, 2).
reshaped_rows = arr[1:3].reshape(5, 2)
print(reshaped_rows)

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
single

single

import numpy as np

# Given the following array
arr = np.arange(0, 25).reshape(5, 5)

# 1. Extract the diagonal elements.
diagonal_elements = ___
print(diagonal_elements)

# 2. Retrieve the elements in the second row.
second_row = ___
print(second_row)

# 3. Extract the 2nd and 3rd rows and change their shape to (5, 2).
reshaped_rows = ___
print(reshaped_rows)

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

We use cookies to make your experience better!
some-alt