Assignment 4 Feedback

Closed at 2pm Monday 6th; I can't accept late submissions unfortunately.

I need your CIS username in emails if you each send me only 1 email in the whole course forgetting your CIS username it translates to 30 times per week I have to ask for it.

In general very good again although a repeat of some of the plotting issues from last week.

There were a few instances of people using the analytic method for the final question; it is acourse on numerical methods.

Per the Monte Carlo method last week, if I ask you to calculate pi I don't want you to return np.pi I want you to use the numerical method.

Plagarism

I've had advice that it is apt to start formal Plagarism procedures against people copy/pasting.

Don't want people to be in a position where they think they need to do this. If you have an issue please send me an email (or two) or come to the workshop(s) for help.

Seems like I'm giving this warning every week + there is the statement on the website!

This is the current situation which may not always be the case in the future.

I'll put a form at the top of this weeks assignment about evaluating your submissions anonymously for updating policies surrounding plagarism.

I don't want to be in the horse and carriage business but we need evidence to change university policy.

Good news, some impressive coding

Break statements to leave loops

for x in y:     
    if x == 0:
        break
# Out of the loop now
    

Splats to unpack lists

  x = [1, 2, 3]
  y = [4, 5, 6]
  z = [*x, *y] = [1,2,3,4,5,6]
    

List comprehensions

  x = [i**2 for i in range(5)]
  # x = [0, 1, 4, 9, 16]
    

If it works. it works!

This is a 100% scoring example.

What is wrong here?

What is wrong here?

What is wrong here?

What is wrong here?

I'm not trying to trick people, the info should be clear and on the website. Please make this mistake every week.

What is wrong here?

This is a 100% scoring example.

What is wrong here?

What is wrong here?

NumPy Arrays

NumPy Array creation:

 
  a = np.array([1, 2, 3]) 
  # np.array with elements [1,2,3]
  
  b = np.array(100)
  # np.array with element [100] not a 100 space array!
  # The argument needs to exist!

  # Zeros Array
  c = np.zeros((100))
  # [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0.] 
                        

Array Indexing

Say our numpy array looks like this:

A B C

A B C

A B C

  • How can we select the first 'A,B,C'

Array Indexing

Selecting Subarrays:

  • Selecting rows using array[row, :]
  • Selecting rows using array[row]
    # Create array of zeros
    x = np.zeros((3,4))
    # [[0. 0. 0. 0.]
       [0. 0. 0. 0.]
       [0. 0. 0. 0.]]

    # Selecting the first row of b
    x[0] = np.ones((4))
    # [[1. 1. 1. 1.]
       [0. 0. 0. 0.]
       [0. 0. 0. 0.]]
                          

Array Indexing

Say our numpy array looks like this:

A B C

A B C

A B C

  • How can we select all the 'A's

Array Indexing

Selecting Subarrays:

  • Selecting columns using array[:, column]
      # Assume x is a 10x4 array of zeros
      y = np.zeros((10,4))
      
      # Selecting the first column of c
      y[:, 0] = np.ones((10))

      # [[1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]
        [1. 0. 0. 0.]]
                            

Array Functions

Applying Functions to Arrays:

  • Element-wise function application with numpy functions
        y => [[1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]
              [1. 0. 0. 0.]]

        np.sum(y, axis=0)
        [10.  0.  0.  0.]

        np.sum(y, axis=1)
        [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
                      

Membership

Membership:

          a = [1,2,3]
          1 in a # True
          
          for i in a:
              print(i)
          # 1, 2, 3

          a[0] == 1
          # True