Programming Python for Business Analytics Week 3
41 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

Which library is primarily used for numerical analysis in Python?

  • NumPy (correct)
  • Scikit-learn
  • Pandas
  • Matplotlib

NumPy allows for efficient computations with large multi-dimensional arrays.

True (A)

What is the time taken by np.sum() to sum 10 million numbers?

0.0009 seconds

Scikit-learn is built on top of ___.

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

Match the following Python libraries with their primary functions:

<p>NumPy = Numerical analysis Pandas = Data manipulation and analysis Matplotlib = Data visualization Scikit-learn = Machine learning</p> Signup and view all the answers

What is a key feature of NumPy?

<p>Fast computation of large multi-dimensional arrays (B)</p> Signup and view all the answers

What Python shortcut is used for importing the NumPy library?

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

What method is used to calculate the total in the provided code snippet?

<p>np.sum() (D)</p> Signup and view all the answers

Broadcasting in NumPy allows operations between arrays of different shapes without any issues.

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

Name one mathematical operation that is considered an aggregation.

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

The result of multiplying array X by array a is ____, where X is np.array([[1,2,3],[4,5,6]]) and a is np.array([0,1,0]).

<p>array([[0, 2, 0], [0, 5, 0]])</p> Signup and view all the answers

Match the following aggregation functions with their description:

<p>sum = Calculates the total of elements max = Finds the maximum value mean = Calculates the average of elements min = Finds the minimum value</p> Signup and view all the answers

What error occurs when trying to add two arrays of differing shapes without appropriate reshaping?

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

The output of reshaping an array to (2,2) with elements [0, 10, 20, 30] will be an array containing two rows and two columns.

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

What is the result of the operation x.reshape((4,1)) when x = np.array([0,10,20,30])?

<p>array([[ 0], [10], [20], [30]])</p> Signup and view all the answers

The total of outer product can be calculated using the formula: total = σ_{i=1}^{n} x_i ⋅ σ_{j=1}^{m} y_j or using _____ instead of for-loops.

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

Match the following operations with their result:

<p>x + y = ValueError due to shape mismatch x.reshape((4, 1)) = Array with shape (4, 1) x.reshape((2, 2)) = Array with shape (2, 2) x + y.reshape((1, 3)) = Properly broadcasted operation</p> Signup and view all the answers

What does the expression A[0, :] return?

<p>The first row of matrix A (D)</p> Signup and view all the answers

A slicing operation of the form A[0:2] returns the first two elements of a matrix.

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

What is the output of the expression x[:, 1] when x is defined as np.array([[3,2,1], [4,5,6]])?

<p>[2, 5]</p> Signup and view all the answers

The expression x[x > 2] extracts elements from the array x that are greater than ___ .

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

Match the following expressions with their corresponding outputs:

<p>A[0:2, 1:3] = (2,2) A[:2, 1:] = (2,2) x[-1, :] = (3,) x[:2, 1:2] = (2,1)</p> Signup and view all the answers

What is the output of the expression x[::-1, [2,1,0]] for the array x defined as np.array([[6,5,4], [1,2,3]])?

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

Boolean indexing works with both NumPy arrays and regular Python lists.

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

What shape does the expression A[:, :2] return when A is defined as np.array([[0,1,2], [3,4,5]])?

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

What function is used to sort an array in ascending order?

<p>np.sort(array) (D)</p> Signup and view all the answers

The command np.sort(-a) sorts an array in ascending order.

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

What value of x produces the minimum of cos(x) between [0, 6]?

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

The function used to generate random numbers uniformly is np.random.______

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

Match the following NumPy functions with their description:

<p>np.random.seed(N) = Sets the seed for random number generation np.argmax(array) = Returns the index of the maximum value in the array np.argmin(array) = Returns the index of the minimum value in the array np.random.permutation(array) = Randomly permutes the elements of an array</p> Signup and view all the answers

Which of the following statements about direct sorting is correct?

<p>np.sort(array, axis=None) sorts the entire array. (D)</p> Signup and view all the answers

Np.random.rand(N) and np.random.randn(N) produce the same type of random numbers.

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

To find the maximum value in an array, you would use the function np.max(______).

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

What does A[A > 2] return when A is defined as np.array([[0,3,1], [4,2,5]])?

<p>[3, 4, 5] (D)</p> Signup and view all the answers

Creating a slice of a NumPy array results in a copy of the original array.

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

What operation does A * 2 perform on the NumPy array?

<p>It multiplies each element of A by 2.</p> Signup and view all the answers

In NumPy, A.copy() creates a __________ of the array A.

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

Match the following functions with their descriptions:

<p>np.sum() = Calculates the sum of array elements. np.dot() = Performs matrix product. A.copy() = Creates a separate copy of the array. A[A &gt; 2] = Returns elements that are greater than 2.</p> Signup and view all the answers

What will happen if you execute B[0, 0] = 10 after creating B via B = A.copy()?

<p>B[0, 0] will change to 10, but A[0, 0] remains unchanged. (A)</p> Signup and view all the answers

Element-wise operations like A + B perform matrix multiplication in NumPy.

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

In the given context, how are gains calculated when $X_{tj} > 0$?

<p>Gains are calculated as the sum of $X_{tj}$ for each department j for all time periods t where $X_{tj}$ is greater than 0.</p> Signup and view all the answers

Flashcards

NumPy

A Python library for numerical computation, particularly useful for working with vectors and matrices.

Vectors and matrices

Multi-dimensional arrays used for numerical data in Python, efficiently handled by NumPy.

Broadcasting

NumPy's ability to perform operations on arrays of different shapes, automatically expanding smaller arrays to match larger ones.

Aggregations

Summarizing numerical data in NumPy, like summing, averaging, or finding maximums.

Signup and view all the flashcards

Python loops vs. NumPy

NumPy operations are significantly faster than iterating through lists using Python loops for numerical calculations.

Signup and view all the flashcards

Numerical analysis

The branch of mathematics focused on numerical methods for solving problems, often using libraries like NumPy.

Signup and view all the flashcards

Import NumPy

The process of including NumPy in your Python code; commonly written as import numpy as np

Signup and view all the flashcards

Large multi-dimensional arrays

NumPy efficiently handles large data sets with multiple dimensions (vectors/matrices/etc.).

Signup and view all the flashcards

NumPy array slicing

Using slice notation to extract parts of a NumPy array. Allows selecting rows and columns.

Signup and view all the flashcards

Slice notation

A way to select portions of arrays or lists using start, end, and step indices.

Signup and view all the flashcards

Array indexing

Selection of specific elements within a NumPy array using their row and column indices.

Signup and view all the flashcards

Boolean Indexing

Selecting elements in a NumPy array based on a boolean mask.

Signup and view all the flashcards

x[0, 0]

Returns the element at row 0, column 0 of an array x.

Signup and view all the flashcards

x[:, 1]

Returns all rows, second column from an array x.

Signup and view all the flashcards

x[:2, 1:]

Returns first two rows and remaining columns from an array x.

Signup and view all the flashcards

x[-1, :]

Returns the last row in array.

Signup and view all the flashcards

NumPy Matrix

A multi-dimensional array in NumPy, representing a table of numbers.

Signup and view all the flashcards

Views vs Copies

When you operate on an array in NumPy, you can create a 'view' (changes affect original) or a 'copy' (changes are isolated).

Signup and view all the flashcards

Create NumPy View

Directly accessing sub-arrays or slices of a NumPy array creates a 'view' - changes to the slice update the original array.

Signup and view all the flashcards

Create NumPy Copy

Use the .copy() method to create an independent copy of a NumPy array or slice.

Signup and view all the flashcards

Element-wise Operations

NumPy operations usually apply to individual elements of an array.

Signup and view all the flashcards

Matrix Multiplication

In NumPy, you use np.dot(A, B) for matrix multiplication, NOT A * B.

Signup and view all the flashcards

Speed of NumPy Operations

NumPy operations are significantly faster than Python loops, especially for large arrays.

Signup and view all the flashcards

Error: operands could not be broadcast together

This error occurs when NumPy's broadcasting rules cannot be applied to perform a mathematical operation on arrays of incompatible shapes.

Signup and view all the flashcards

NumPy aggregations

Functions like sum, min, max, mean, all, and any that summarize or reduce numerical data in NumPy arrays.

Signup and view all the flashcards

Faster than loops

Mathematical operations using broadcasting in NumPy are generally faster than iterating through elements using loops.

Signup and view all the flashcards

np.all(A < 0, axis = 0)

Checks if all elements in each row of a 2D array (A) are negative. Returns a boolean array indicating rows with all negative elements.

Signup and view all the flashcards

np.sort(array, axis=)

Sorts the elements of a NumPy array along the specified axis. By default, it sorts in ascending order.

Signup and view all the flashcards

Shorter and easier to write

Using broadcasting for calculations in NumPy usually requires less code and is easier to read than using loops.

Signup and view all the flashcards

-np.sort(-a)

Sorts a NumPy array in descending order.

Signup and view all the flashcards

np.argsort(array, axis=)

Returns the indices that would sort an array along the specified axis. It doesn't actually sort the array, but tells you the positions to reorder it.

Signup and view all the flashcards

np.argmin(array, axis=)

Returns the index of the minimum value in the array along the specified axis.

Signup and view all the flashcards

np.argmax(array, axis=)

Returns the index of the maximum value in the array along the specified axis. It tells you where the largest value is.

Signup and view all the flashcards

np.random.permutation(array)

Randomly shuffles the elements of a NumPy array.

Signup and view all the flashcards

scipy.optimize

A SciPy sub-module that provides tools like linear programming, non-linear optimization and differential evolution for numerical optimization.

Signup and view all the flashcards

Broadcasting in NumPy

NumPy's ability to perform operations on arrays of different shapes by automatically expanding the smaller array to match the larger one.

Signup and view all the flashcards

Reshaping a NumPy array

Changing the dimensions (rows and columns) of a NumPy array without altering the data itself.

Signup and view all the flashcards

Why does x + y fail?

The arrays x and y have incompatible dimensions (4 and 3 respectively). Broadcasting requires the shapes to be compatible or one of them to be a scalar.

Signup and view all the flashcards

Reshape to enable addition

We can reshape x to be a 4x1 matrix (x.reshape((4,1))) so that it can be broadcast with y, which has a shape of (3,).

Signup and view all the flashcards

What is the result of x.reshape((4,1)) + y?

The result is a 4x3 array where each element is the sum of the corresponding element from x and y. Broadcasting implicitly copies the y array across each row of x.

Signup and view all the flashcards

Study Notes

Course Information

  • Course title: Programming Python for Business Analytics
  • Course code: BMAN737
  • Week: 3, Lecture 2
  • Topic: Numerical Analysis

Instructor Information

  • Professor: Manuel López-Ibáñez
  • Email: [email protected]
  • Office hours: Monday 4pm-5pm, Friday 9am-10am
  • Contact: Discussion board in Blackboard, Calendly link provided

Numerical Analysis (NumPy)

  • Overview: Week 3 lecture focuses on numerical analysis using NumPy.

New Material

  • Moving from basic to advanced analytics
  • Complete Python code available in Blackboard
  • Labs: shift towards case studies

Course Plan

  • Week 3: Numerical Analysis (NumPy)
  • Week 4, Lecture 1: Data Exploration and Visualization (Pandas + Matplotlib)
  • Week 4, Lecture 2: Data preprocessing and preparation (Pandas + Scikit-learn)
  • Week 5: Machine learning (Scikit-learn)

Part 1: Vectors and Matrices

  • Focus of initial portion of the lecture

Part 2: Broadcasting

  • Lecture topic

Part 3: Aggregations and Operations

  • Further lecture topic

NumPy Arrays

  • NumPy arrays differ from lists (e.g., appending an element to a NumPy array, will not add to the array but return a new array).
  • NumPy arrays support fast calculations
  • NumPy arrays are multi-dimensional (vectors and matrices)
  • Includes good integration with Pandas & Matplotlib

NumPy Arrays vs. Lists

  • NumPy arrays are different from Python lists
  • Using NumPy arrays for data structure and analysis will be much faster than using Python lists for identical operations.
  • Operations on NumPy arrays are element-wise by default.

NumPy Array Indexing

  • NumPy array indexing differs from list indexing, and uses square bracket notation.

NumPy Slices

  • NumPy slices are views, not copies. This means changes made to a slice will modify the original array

NumPy Slices Explained

  • Slices are not copies of arrays, but rather views to the same underlying data. This is an important concept to understand when working with NumPy arrays.
  • Modifying a NumPy slice affects the original array.
  • Use .copy() to create a true copy.

NumPy Arrays: Indexing

  • Different ways to index arrays
  • Examples shown for accessing specific elements or subsets.

Broadcasting

  • Operators (+,-,*,/) usually act element-wise on arrays
  • Can calculate across matrices, or arrays of different dimensions if dimensions align.
  • ValueError error if NumPy cannot broadcast correctly.
  • Reshaping an array can help with broadcasting when shapes are not matching

Recap

  • NumPy arrays are specialized data structures capable of numerous mathematical and statistical operations
  • NumPy provides specialized functions for sorting.
  • NumPy provides direct sorting and indirect sorting.
  • NumPy and SciPy are utilized for complex calculations and mathematical functions, respectively.

Broadcasting Examples

  • Matrix and vector calculations
  • Using reshape to enable broadcasting
  • Common error scenarios

Aggregations

  • Operations like sum, min, max, mean, all, any are aggregations (reductions),
  • Used along axis 0 or axis 1 of an array

SciPy Library

  • Library for numerical analysis and scientific computations
  • Includes sub-modules such as optimize, linalg, and stats
  • Provides useful mathematical and statistical functions

Next Week Material

  • Libraries: pandas and matplotlib

Studying That Suits You

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

Quiz Team

Related Documents

Description

This quiz covers the Week 3 lecture focusing on numerical analysis using NumPy in the Programming Python for Business Analytics course. It includes topics such as vectors, matrices, and broadcasting concepts essential for moving towards advanced analytics. Prepare to apply your knowledge to real-world case studies and strengthen your understanding of Python programming.

More Like This

Use Quizgecko on...
Browser
Browser