Question

In: Computer Science

Please I seek assistance Python Programing import os import numpy as np def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):...

Please I seek assistance
Python Programing

import os
import numpy as np

def generate_assignment_data(expected_grade_file_path,
                             std_dev, output_file_path):
    """
    Retrieve list of students and their expected grade from file,
    generate a sampled test grade for each student
    drawn from a Gaussian distribution defined by the
    student expected grade as mean, and the given
    standard deviation.

    If the sample is higher than 100, re-sample.
    If the sample is lower than 0 or 5 standard deviations below mean,
    re-sample

    Write the list of student grades to the given
    output file using ID and grade with TAB separation.

    :param expected_grade_file_path: This is our file of student IDs and expected grades
    :param std_dev: Standard deviation used when sampling grades
    :param output_file_path: Where to write the sample grades
    :return: number of student grades generated and
            tuple of mean, median and standard deviation of grades
    """

Solutions

Expert Solution

The required Code, Sample input file & sample output file are given below. Basically, we read the input file using python csv dictreader which reads each row as a dictionary. We then use Numpy random.normal function to draw a sample from the Gaussian distribution :

CODE:

import csv
import os
import numpy as np

def generate_assignment_data(expected_grade_file_path, std_dev, output_file_path):

    with open(expected_grade_file_path, newline='') as csv_in:    #Opening the file having expected grades
        with open(output_file_path, 'w', newline='') as csv_out:   #Opening the file to write in write mode
            fieldnames = ['id', 'expected_grade', 'sampled_grade']   #specifying fieldnames for output file
            writer = csv.DictWriter(csv_out, fieldnames=fieldnames, delimiter='\t')  #creating a writer object for writing output file
            writer.writeheader()
            data = csv.DictReader(csv_in, delimiter=',')  #read data from specified file
            generated_grades = []        #initialize empty list for generated sample grades
            for row in data:
                row['sampled_grade'] = -1
                #keep sampling again till any of the given condition is true
                while (row['sampled_grade'] < 0 or row['sampled_grade'] > 100 or row['sampled_grade'] < 5 * std_dev):
                    row['sampled_grade'] = np.random.normal(float(row['expected_grade']), std_dev)   #generate the sample
                generated_grades.append(row['sampled_grade'])
                print(row)
                writer.writerow(row)
            generated_grades = np.array(generated_grades)  #convert generated grades list to numpy array
            return generated_grades.size,(generated_grades.mean(),np.median(generated_grades),generated_grades.std()) #return required values

#testing
print(generate_assignment_data('students.csv', 5, 'students_out.csv'))

CODE Screenshot:

Sample Input file used for testing:

id,expected_grade
s1,70
s2,80
s3,65
s4,75

Output:

Output file data:

id   expected_grade   sampled_grade
s1   70   72.89787027604346
s2   80   86.3827664017767
s3   65   75.18435378753523
s4   75   77.46518085069853

(*Note: Please up-vote. If any doubt, please let me know in the comments)


Related Solutions

Please fix all the errors in this Python program. import math def solve(a, b, c): """...
Please fix all the errors in this Python program. import math def solve(a, b, c): """ Calculate solution to quadratic equation and return @param coefficients a,b,class @return either 2 roots, 1 root, or None """ #@TODO - Fix this code to handle special cases d = b ** 2 - 4 * a * c disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 if...
#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()...
#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()     return(d) print(a["Date"]) print("3/14/2012".get()) How to write the "get" function (without any imput), to convery the format ""3/14/2012" to the format "2012-03-14", by simply using "3/14/2012".get() ?
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def...
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def take_e(n, gen):     return [elem for (i, elem) in enumerate(gen) if i < n] def take_zc(n, gen):     return [elem for (i, elem) in zip(count(), gen) if i < n] FIBONACCI UNBOUNDED: def fibonacci_unbounded():     """     Unbounded Fibonacci numbers generator     """     (a, b) = (0, 1)     while True:         # return a         yield a         (a, b) = (b, a + b) SUPPOSED TO RUN THIS TO ASSIST WITH...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2)...
I need to write these three functions in Python using turtle module. def line(t, coord1, coord2) t is a turtle object passed in coord1 and coord2 are 2-element lists that represent x, y coordinates draws a line from coord1 to cord2 def poly(t, *coords) t is a turtle object passed in *coords is any number of x, y coordinates each as a 2-element list draws a polygon based on coords (will close off polygon by drawing a line from last...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and...
Please answer using python 3 and def functions! Lab 2 Drill 3: (function practice) create and use a function named highest() that takes three inputs and returns the highest number. After you have got it working, try calling the function with inputs ‘hat’, ‘cat’, ‘rat’.
Hello I have these questions, Can I please get some assistance, I need not so long...
Hello I have these questions, Can I please get some assistance, I need not so long or short answers please How to decide what job to take. Is that a marginal decision? What is an explicit cost? What is an implicit cost? How is accounting profit calculated? How is economic profit calculated?
Please give me an example of how we import stock data in jupyter notebook(Python) and analyze...
Please give me an example of how we import stock data in jupyter notebook(Python) and analyze each step.
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName,...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName, courses = None): The “id”, “firstName” and “lastName” parameters are to be directly assigned to member variables (ie: self.id = id) The “courses” parameter is handled differently. If it is None, assign dict() to self.courses, otherwise assign courses directly to the member variable. Note: The “courses” dictionary contains key/value pairs where the key is a string that is the course number (like “course1”) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT