Assignment 1 Feedback

Generally well answered although some mixing up of reasons for large and small dx

There are marks for axis titles and labels. Use plt.title etc...

Some failed the autograder! You can check this with Kernel -> restart & run all.

Loops

Python 'range()' function:

  • Takes arguments (start, stop, step)
  • Start is inclusive, stop is exclusive
  for i in range(3):
      print(i)
  output: 0,1,2

  for i in range(1,4):
      print(i)
  output: 1,2,3

  for i in range(0,4,2):
      print(i)
  output: 0,2
                        

Loops

Python 'range()' function:

  • Can use modulo arithmetic to go even/odd
  • Can even use list comprehension to use range inside lists
for i in range(0,4):
    if i % 2 == 0:
        print(i)
output: 0,2

x = [i for i in range(5)]
print(x)
output: [0,1,2,3,4]
                      

NumPy Arrays

Ways to Create NumPy Arrays:

  • From Python Lists using numpy.array()
  • Zeros Array using numpy.zeros()
  • Ones Array using numpy.ones()
  • Identity Matrix using numpy.eye()
import numpy as np

# From Python Lists
a = np.array([1, 2, 3]) 

# Zeros Array
b = np.zeros((3,))

# Ones Array
c = np.ones((4,))

# Identity Matrix
d = np.eye(3)
                      

Which One Is Valid?

Which one of these methods is a valid way to create a NumPy array?

  • Arithmetic Sequences using numpy.arange()
  • Evenly Spaced Values over Range using numpy.linspace()
  • Logarithmic Scale using numpy.logspace()
  • Random Values (Uniform Distribution) using numpy.random.rand()

Answer

They all do.

Which One Is Valid? (Continued)

Which one of these methods is a valid way to create a NumPy array?

  • Random Values (Uniform Distribution) using numpy.random.rand()
  • Random Integers using numpy.random.randint()
  • Random Values (Normal Distribution) using numpy.random.randn()
  • Diagonal Matrix using numpy.diag()

Answer

They all do.

NumPy Array Slicing

Various ways to slice a NumPy array:

  • Remove first and last element: x = x[1:-1]
  • Select every other element: x = x[::2]
  • Reverse the array: x = x[::-1]
  • Select a subarray: x = x[1:4]
  • Select rows and columns: x = x[1:4, 0:2]
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5])

# Remove first and last element
y = x[1:-1]

# Select every other element
z = x[::2]

# Reverse the array
w = x[::-1]

# Select a subarray
v = x[1:4]