NumPy Basics for Python Programming

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] (A)</p> Signup and view all the answers

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

<p>Series (A)</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 (D)</p> Signup and view all the answers

What command is used to install Pandas in Python?

<p>pip install pandas (C)</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 (A)</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]}) (A)</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 (C)</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]) (B)</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 (C)</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 (D)</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]) (D)</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] (C)</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 (D)</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]]]) (A)</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 (B)</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) (B)</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]]) (A)</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] (A)</p> Signup and view all the answers

Flashcards

NumPy

A Python library for numerical computations, particularly working with arrays.

NumPy array

The fundamental data structure in NumPy, an n-dimensional array of a given type.

1-D array

A single row or column of numeric data.

2-D array

A grid or matrix of numbers.

Signup and view all the flashcards

3-D array

Represents data organized in multiple layers or cubes.

Signup and view all the flashcards

Array Slicing

Extracting portions of an array using specific indices (ranges).

Signup and view all the flashcards

ndarray

The official name and object for a NumPy array.

Signup and view all the flashcards

Import NumPy

How to load NumPy into a Python program.

Signup and view all the flashcards

Pandas Series

A one-dimensional labeled array capable of holding any data type (integers, strings, floating point numbers, etc.)

Signup and view all the flashcards

Series Indexing

Using custom labels to access elements in a Pandas Series, instead of the default numerical indices.

Signup and view all the flashcards

Creating a Pandas Series

Constructing a Pandas Series from a list, dictionary, or other data source specifying custom labels for the provided data.

Signup and view all the flashcards

Series element access

Retrieving a specific element from a Pandas Series using its index label.

Signup and view all the flashcards

NumPy Array Slicing

Selecting specific portions of a NumPy array using start, stop, and step values. For example, to get elements from index 1 to 4 (exclusive), use arr[1:5] in a one dimensional array

Signup and view all the flashcards

Array Slicing: Start Index Omitted

If the starting index is omitted in a NumPy array slice, it starts at the beginning of the array, e.g. arr[:4] selects indices 0 to 3

Signup and view all the flashcards

Array Slicing: End Index Omitted

If the end index is omitted in a NumPy array slice, it goes to the end of the array. For example, arr[4:] selects from index 4 up to the last element in a one dimensional array

Signup and view all the flashcards

Array Slicing: Negative Indices

NumPy array slicing allows the use of negative indices to count from the end of the array. arr[-3:-1] specifies the elements from index 3 (from the end) up to (but not including) index 1 (from the end).

Signup and view all the flashcards

Array Slicing: Step Value

A step value in NumPy slicing lets you choose elements that skip values e.g arr[1:5:2] picks elements from index 1 to 4, but takes every other element.

Signup and view all the flashcards

Multi-dimensional Array Slicing

Slicing multi-dimensional NumPy arrays involves specifying the indices for each dimension in each dimension separately. For example arr[1,1:4] slices a portion from row 1

Signup and view all the flashcards

Pandas DataFrame

A Pandas DataFrame is a two-dimensional data structure organized in rows and columns. It is similar to a table in a spreadsheet but can be created and interacted with by Python code.

Signup and view all the flashcards

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

More Like This

NumPy Creating Arrays Quiz
13 questions
Numpy Data Types Overview
19 questions

Numpy Data Types Overview

TerrificBlueLaceAgate avatar
TerrificBlueLaceAgate
NumPy Array Indexing and Slicing
6 questions
NumPy Arrays in Python
18 questions
Use Quizgecko on...
Browser
Browser