Podcast
Questions and Answers
What will be the output when slicing the array from index 1 to index 5?
What will be the output when slicing the array from index 1 to index 5?
If an array is sliced from index 4 to the end, which part of the array will be returned?
If an array is sliced from index 4 to the end, which part of the array will be returned?
When creating a DataFrame from two Series, what structure does the resultant DataFrame take?
When creating a DataFrame from two Series, what structure does the resultant DataFrame take?
How would you obtain every other element from index 1 to index 5 in a NumPy array?
How would you obtain every other element from index 1 to index 5 in a NumPy array?
Signup and view all the answers
What is the term for a single column in a Pandas DataFrame?
What is the term for a single column in a Pandas DataFrame?
Signup and view all the answers
What happens when you slice an array using negative indexes?
What happens when you slice an array using negative indexes?
Signup and view all the answers
What command is used to install Pandas in Python?
What command is used to install Pandas in Python?
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?
When slicing a 2D array with the notation arr[0:2, 1:4], what part of the array do you retrieve?
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?
Which of the following correctly demonstrates how to create a Pandas DataFrame from a dictionary of cars and their corresponding passing values?
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]
?
What is the output of the following Pandas Series creation: pd.Series(a, index = ['x', 'y', 'z'])
where a = [1, 7, 2]
?
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]
?
How can you create a one-dimensional NumPy array given a list of numeric values like [12.23, 13.32, 100, 36.32]
?
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}
?
What is the result of the following operation: myvar = pd.Series(calories, index=['day1', 'day2'])
where calories = {'day1': 420, 'day2': 380, 'day3': 390}
?
Signup and view all the answers
Which command correctly initializes an 8x8 matrix filled with a checkerboard pattern in NumPy?
Which command correctly initializes an 8x8 matrix filled with a checkerboard pattern in NumPy?
Signup and view all the answers
What command is used to create a 1-D array with the values 1, 2, 3, 4, 5?
What command is used to create a 1-D array with the values 1, 2, 3, 4, 5?
Signup and view all the answers
How do you correctly slice a NumPy array to obtain elements from index 1 to 3?
How do you correctly slice a NumPy array to obtain elements from index 1 to 3?
Signup and view all the answers
Which of the following is the correct way to import NumPy using an alias?
Which of the following is the correct way to import NumPy using an alias?
Signup and view all the answers
Which command correctly creates a 3-D NumPy array?
Which command correctly creates a 3-D NumPy array?
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]]])?
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]]])?
Signup and view all the answers
What is the shape of the array created by arr = np.array([[1, 2], [3, 4], [5, 6]])?
What is the shape of the array created by arr = np.array([[1, 2], [3, 4], [5, 6]])?
Signup and view all the answers
Which function is used to create a 2-D array containing two distinct 1-D arrays?
Which function is used to create a 2-D array containing two distinct 1-D arrays?
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]?
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]?
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.
Related Documents
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.