NumPy Basics for Python Programming
21 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 be the output when slicing the array from index 1 to index 5?

  • [2 3 4 5] (correct)
  • [3 4 5 6]
  • [1 2 3 4]
  • [4 5 6 7]
  • If an array is sliced from index 4 to the end, which part of the array will be returned?

  • [4 5 6 7]
  • [1 2 3 4]
  • [6 7]
  • [5 6 7] (correct)
  • When creating a DataFrame from two Series, what structure does the resultant DataFrame take?

  • A single column
  • A two-dimensional array
  • A single-dimensional array
  • A multi-dimensional table (correct)
  • How would you obtain every other element from index 1 to index 5 in a NumPy array?

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

    What is the term for a single column in a Pandas DataFrame?

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

    What happens when you slice an array using negative indexes?

    <p>Starts from the end of the array</p> Signup and view all the answers

    What command is used to install Pandas in Python?

    <p>pip install pandas</p> Signup and view all the answers

    When slicing a 2D array with the notation arr[0:2, 1:4], what part of the array do you retrieve?

    <p>A sub-array of specified rows and columns</p> Signup and view all the answers

    Which of the following correctly demonstrates how to create a Pandas DataFrame from a dictionary of cars and their corresponding passing values?

    <p>pd.DataFrame({'cars': ['BMW', 'Volvo', 'Ford'], 'passings': [3, 7, 2]})</p> Signup and view all the answers

    What is the output of the following Pandas Series creation: pd.Series(a, index = ['x', 'y', 'z']) where a = [1, 7, 2]?

    <p>x 1 y 7 z 2 dtype: int64</p> Signup and view all the answers

    How can you create a one-dimensional NumPy array given a list of numeric values like [12.23, 13.32, 100, 36.32]?

    <p>np.array([12.23, 13.32, 100, 36.32])</p> Signup and view all the answers

    What is the result of the following operation: myvar = pd.Series(calories, index=['day1', 'day2']) where calories = {'day1': 420, 'day2': 380, 'day3': 390}?

    <p>day1 420 day2 380 dtype: int64</p> Signup and view all the answers

    Which command correctly initializes an 8x8 matrix filled with a checkerboard pattern in NumPy?

    <p>np.indices((8,8)).sum(axis=0) % 2</p> Signup and view all the answers

    What command is used to create a 1-D array with the values 1, 2, 3, 4, 5?

    <p>arr = np.array([1, 2, 3, 4, 5])</p> Signup and view all the answers

    How do you correctly slice a NumPy array to obtain elements from index 1 to 3?

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

    Which of the following is the correct way to import NumPy using an alias?

    <p>import numpy as np</p> Signup and view all the answers

    Which command correctly creates a 3-D NumPy array?

    <p>arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])</p> Signup and view all the answers

    What will be the output of accessing the element at arr[0, 1, 2] for the array defined as arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])?

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

    What is the shape of the array created by arr = np.array([[1, 2], [3, 4], [5, 6]])?

    <p>(3, 2)</p> Signup and view all the answers

    Which function is used to create a 2-D array containing two distinct 1-D arrays?

    <p>np.array([[1, 2, 3], [4, 5, 6]])</p> Signup and view all the answers

    Using NumPy, how can you change the first row of a 2-D array arr = np.array([[1, 2, 3], [4, 5, 6]]) to [7, 8, 9]?

    <p>arr[0] = [7, 8, 9]</p> Signup and view all the answers

    Study Notes

    NumPy

    • NumPy is short for "Numerical Python"
    • It's a Python library used for working with arrays.

    NumPy Installation

    • Install using pip:
      • C:\Users\Your Name>pip install --upgrade pip
      • C:\Users\Your Name>pip install numpy

    Importing NumPy

    • Import using import numpy as np
    • Create an array: arr = np.array([1, 2, 3, 4, 5])

    NumPy as np

    • NumPy is typically imported as np
    • Example: import numpy as np

    Creating NumPy Arrays from tuples

    • Using tuples to create NumPy arrays:
      • arr = np.array((1, 2, 3, 4, 5))

    1-D Arrays

    • Create a 1-D array:
      • arr = np.array([1, 2, 3, 4, 5])

    Accessing Array Elements

    • Accessing elements:
      • arr[0] (first element from array)
      • arr[1] (second element from array)

    2-D Arrays

    • Create a 2-D array:
      • arr = np.array([[1, 2, 3], [4, 5, 6]])

    Accessing 2-D Array elements

    • Accessing specific elements:
      • arr[0, 1] (2nd element in the first row)

    3-D Arrays

    • Create a 3-D array:
      • arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

    Accessing 3-D Array elements

    • Accessing specific elements:
      • arr[0, 1, 2] (third element in the second array from the first array)

    NumPy Array Slicing

    • Slicing extracts elements from a NumPy array
    • Basic slicing: arr[start:end]
      • arr[1:5] slices items from index 1 up to (but not including) 5.
    • Slicing with step: arr[start:end:step]
      • arr[1:5:2] slices elements from index 1 to 5 with a step of 2.
    • Slicing to end of array: arr[start:]
      • arr[4:] starts at index 4 and goes to the end
    • Slicing from beginning: arr[:end]
      • arr[:4] starts at beginning and goes up to (but not including) 4.
    • Different slicing approaches with 2-D arrays.
      • Slicing from indexes or a range of indexes of 2-D arrays.

    Pandas

    • Pandas is a Python library for data analysis
    • It's used for manipulating and analyzing data sets.

    Pandas Installation

    • Install using pip:
      • pip install --upgrade pip
      • pip install pandas

    Pandas DataFrames

    • DataFrames are multi-dimensional tabular data structures.
    • Series are one-dimensional arrays.
    • Example DataFrame creation from a dictionary: myvar = pd.DataFrame(data)

    Pandas Series

    • A Pandas Series is like a column in a table.
    • It's a one-dimensional array holding data of any type.

    Creating Pandas Series

    • Create a Series from a list: myvar = pd.Series([1, 7, 2])
    • Create a Series from a dictionary: myvar = pd.Series({"day1": 420, "day2": 380})

    Accessing Series elements

    • Accessing elements: print(myvar[0])

    Pandas Labels

    • Labels can be used to access values.
    • Default labels are index numbers.
    • User defined labels are possible: myvar = pd.Series([1, 7, 2], index=["x", "y", "z"])
    • Key value pairs can also be used to create a Series

    NumPy to One-Dimensional array

    • Convert a list to a one-dimensional NumPy array: np.array([12.23, 13.32, 100, 36.32])

    NumPy to Array Range

    • Create a NumPy array with values from 12 to 38: np.arange(12, 39)

    NumPy Checkerboard Pattern

    • Create an 8x8 matrix with checkerboard pattern using NumPy.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Python Pandas & NumPy PDF

    Description

    This quiz covers the essentials of NumPy, a powerful library for numerical operations in Python. Learn how to install NumPy, create arrays, and access their elements in both 1-D and 2-D formats.

    More Like This

    Numpy.ones(): Creating Arrays of Ones
    6 questions
    Numpy Data Types Overview
    19 questions

    Numpy Data Types Overview

    TerrificBlueLaceAgate avatar
    TerrificBlueLaceAgate
    NumPy Array Indexing and Slicing
    6 questions
    Use Quizgecko on...
    Browser
    Browser