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

import math import numpy as np import numpy.linalg from scipy.linalg import solve A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0,...
import math import numpy as np import numpy.linalg from scipy.linalg import solve A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0, 0, 0], [-math.sin(math.pi/6), 0, -math.sin(math.pi/3), 0, 0, 0], [math.cos(math.pi/6), 1, 0, 1, 0, 0], [math.sin(math.pi/6), 0, 0, 0, 1, 0], [0, -1, -math.cos(math.pi/3), 0, 0, 0], [0, 0, math.sin(math.pi/3), 0, 0, 1]]) b = np.array([0, 2000, 0, 0, 0, 0]) x = [0, 0, 0, 0, 0, 0] def seidel(a, x, b): # Finding length of a(3) n = len(a) # for loop for...
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 I am creating a class in python. Here is my code below: import csv import...
Python I am creating a class in python. Here is my code below: import csv import json population = list() with open('PopChange.csv', 'r') as p: reader = csv.reader(p) next(reader) for line in reader: population.append(obj.POP(line)) population.append(obj.POP(line)) class POP: """ Extract the data """ def __init__(self, line): self.data = line # get elements self.id = self.data[0].strip() self.geography = self.data[1].strip() self.targetGeoId = self.data[2].strip() self.targetGeoId2 = self.data[3].strip() self.popApr1 = self.data[4].strip() self.popJul1 = self.data[5].strip() self.changePop = self.data[6].strip() The problem is, I get an error saying:  ...
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 +...
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs....
1. Please use Python 3 programing. 2. Please share your code. 3. Please show all outputs. Create a GUI Calculator with the following: Title : Calculator Label and Entry box for 1st Number Label and Entry box for 2nd Number Buttons for *, /, +, - Label for result and Displaying the result
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 have an excel file imported into canopy (python) using import pandas as pd. The excel...
I have an excel file imported into canopy (python) using import pandas as pd. The excel file has headers titled: datetime created_at PM25 temperatureF dewpointF    humidityPCNT windMPH    wind_speedMPH wind_gustsMPH pressureIN precipIN these column headers all have thousands of data numbers under them. How could i find the average of all of the numbers in each column and plot them on 1 graph (line graph or scatter plot) Thank you.(please comment out your code)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT