Python Forum
Basic Boolean homework help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Boolean homework help
#1
Hello folks,

I'm a physics student with a single module in python which isn't supported with detailed notes on how to do things! I'm hoping for some help with the code below.

I've been given four pieces of data for four students - (i) name, (ii) average exam result, (iii) no. of exams "soft-failed", (iv) no. of exams "hard-failed".

Amy 54 1 0
Bob 56 0 1
Carol 72 0 0
Dave 34 3 0

Question part a. states: De fine a 2D NumPy array called results containing the numerical data given in the table (not the names or column headings), and a 1D NumPy array called names containing the four student names. I've done this (see code below).

I'm stuck on the next part of the homework which is as follows:

b. Calculate a Boolean array containing logical values which are True if the student progresses and False if they don't. For full marks, do this in one line of code.

c. By applying a logical array to select array indices, extract the rows from your array results relating to just the data for the students who have failed. Also extract the names of the students who have failed from names.

I've tried to do part b as you can see, but not with one line of code ...? And don't know how to do part c??

import numpy as np
import matplotlib.pyplot as plt

results = np.array([[54, 1, 0], [56, 0, 1], [72, 0, 0], [34, 3, 0]])
names = np.array(['Amy', 'Bob', 'Carol', 'Dave'])

results_colmn = results[:, 0]
progress = results_colmn>=40

For part (b) I think I need to change the code so that I get an array with True values when the values in column 0 are >= 40 and the value in column 2 are 0 ...?
Reply
#2
You need to clarify what means "the student progresses" in terms of column values. Honestly, I don't understand what does it mean, probably, student's progress implies that values in the second column of the array are greater corresponding values in its third column.
If so, you can write this condition easily: results[:, <appropriate column selector1>] > results[:, <appropriate column selector2>]. That would be a one-line solution which you are looking for. The third part of the assignment likely assumes that you solved the second one. Given a Boolean mask, e.g. mask = np.array([True, True, False, False]), you can do names[mask] and results[mask]. It is highly recommended to look into NumPy tutorial.
Reply
#3
(Nov-22-2019, 11:33 PM)scidam Wrote: You need to clarify what means "the student progresses" in terms of column values. Honestly, I don't understand what does it mean, probably, student's progress implies that values in the second column of the array are greater corresponding values in its third column.
If so, you can write this condition easily: results[:, <appropriate column selector1>] > results[:, <appropriate column selector2>]. That would be a one-line solution which you are looking for. The third part of the assignment likely assumes that you solved the second one. Given a Boolean mask, e.g. mask = np.array([True, True, False, False]), you can do names[mask] and results[mask]. It is highly recommended to look into NumPy tutorial.

A student progresses to the next year if they have an average of 40% or more (the first column of numbers gives the average score for each student) and they have no hard-fails (the last column gives this info - only Bob has a hard fail).
Reply
#4
Now it is clear what you want. Line 6 will do the trick.
import numpy as np
import matplotlib.pyplot as plt
 
results = np.array([[54, 1, 0], [56, 0, 1], [72, 0, 0], [34, 3, 0]])
names = np.array(['Amy', 'Bob', 'Carol', 'Dave'])
progresses = np.array([(avg >= 40) and (hard_fail == 0)  for (avg, soft_fail, hard_fail) in results])

print(progresses)
Reply
#5
(Nov-24-2019, 10:06 AM)ibreeden Wrote: progresses = np.array([(avg >= 40) and (hard_fail == 0) for (avg, soft_fail, hard_fail) in results])
Python loops are slow. It is expected something like this:

progresses = results[(results[:, 0] >= results[:, 0].mean()) * (results[:, 2] != 0)]
or
progresses = results[(results[:, 0] >= 40) * (results[:, 2] != 0)]
Reply
#6
(Nov-25-2019, 08:45 AM)scidam Wrote:
(Nov-24-2019, 10:06 AM)ibreeden Wrote: progresses = np.array([(avg >= 40) and (hard_fail == 0) for (avg, soft_fail, hard_fail) in results])
Python loops are slow. It is expected something like this:

progresses = results[(results[:, 0] >= results[:, 0].mean()) * (results[:, 2] != 0)]
or
progresses = results[(results[:, 0] >= 40) * (results[:, 2] != 0)]

Many thanks Scidam!! I'll try something along those lines.

(Nov-24-2019, 10:06 AM)ibreeden Wrote: Now it is clear what you want. Line 6 will do the trick.
import numpy as np
import matplotlib.pyplot as plt
 
results = np.array([[54, 1, 0], [56, 0, 1], [72, 0, 0], [34, 3, 0]])
names = np.array(['Amy', 'Bob', 'Carol', 'Dave'])
progresses = np.array([(avg >= 40) and (hard_fail == 0)  for (avg, soft_fail, hard_fail) in results])

print(progresses)

Many thanks Ibreeden

Can any of you help with part © that I mentioned above:

By applying a logical array to select array indices, extract the rows from your array results relating to just the data for the students who have failed. Also extract the names of the students who have failed from names.
Reply
#7
A follow-up question on the same homework.

import numpy as np


a = input('what is the coefficient for a? ')
b = input('what is the coefficient for b? ')
c = input('what is the coefficient for c? ')

# calculate the discriminant
d = (b**2)-(4*a*c)

sol1 = (-b-np.sqrt(d))/(2*a)
sol2 = (-b+np.sqrt(d))/(2*a)

if d > 0.:
    print('There are two real roots which are ', sol1, 'and', sol2)
elif d == 0.:
    print('There is one real root which is ', sol1)
else:
    print ('There are no real roots ')
    
    
Why does this not work??
Reply
#8
Input returns string. You are trying to perform power of two (and other math operations) on string.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(Nov-27-2019, 10:17 AM)perfringo Wrote: Input returns string. You are trying to perform power of two (and other math operations) on string.

Ah okay. I need to add an int conversion. Fab!!

Almost there ?

import numpy as np

polynomial = np.polynomial.polynomial
p1 = polynomial([2, 3, 1])
p2 = polynomial([1, 2, 1])
p3 = polynomial([10, 1, 2])

roots_1 = p1.roots()    
roots_2 = p2.roots()
roots_3 = p3.roots()
Why does this return, "module object is not callable"?

(Nov-27-2019, 10:47 AM)StillAnotherDave Wrote:
(Nov-27-2019, 10:17 AM)perfringo Wrote: Input returns string. You are trying to perform power of two (and other math operations) on string.

Ah okay. I need to add an int conversion. Fab!!

Almost there ?

import numpy as np

polynomial = np.polynomial.polynomial
p1 = polynomial([2, 3, 1])
p2 = polynomial([1, 2, 1])
p3 = polynomial([10, 1, 2])

roots_1 = p1.roots()    
roots_2 = p2.roots()
roots_3 = p3.roots()
Why does this return, "module object is not callable"?

Resolved: it seems to work if I code as follows

p1 = np.poly1d([2, 3, 1])
p2 = np.poly1d([1, 2, 1])
p3 = np.poly1d([10, 1, 2])

roots_p1 = p1.r
roots_p2 = p2.r
roots_p3 = p3.r

Thank you all for the help so far.

The final part of the homework I can't figure out:

results = np.array([[54, 1, 0], [56, 0, 1], [72, 0, 0], [34, 3, 0]])
names = np.array(['Amy', 'Bob', 'Carol', 'Dave'])

progresses = np.array([(avg >= 40) and (hard_fail == 0)  for (avg, soft_fail, hard_fail) in results])
1. By applying a logical array to select array indices, extract the rows from your array results relating to just the data for the students who have failed. Also extract the names of the students who have failed from names.

2. Ask the user to choose a row number 1, 2, 3 or 4, storing the result as an integer. Use the result to extract, from names and results respectively, the name of the student in the chosen row and the data for that student.
Reply
#10
Not exactly sure what I'm doing here:

results = np.array([[54, 1, 0], [56, 0, 1], [72, 0, 0], [34, 3, 0]])
names = np.array(['Amy', 'Bob', 'Carol', 'Dave'])

progresses = np.array([(avg >= 40) and (hard_fail == 0)  for (avg, soft_fail, hard_fail) in results])
condition = np.array([(avg < 40) or (hard_fail == 1)  for (avg, soft_fail, hard_fail) in results])
Fail = np.extract(results[condition])
Trying to answer: By applying a logical array to select array indices, extract the rows from your array results relating to just the data for the students who have failed. Also extract the names of the students who have failed from names.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Homework advice - Boolean function, whole-word values musicjoeyoung 4 3,187 May-07-2020, 06:10 PM
Last Post: musicjoeyoung

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020