Question: Write a program that creates the following 4 histograms based on the data in weight-height.csv: male heights male weights female heights female weights Redirect the
Write a program that creates the following 4 histograms based on the data in weight-height.csv:
- male heights
- male weights
- female heights
- female weights
Redirect the input from the file into your program. Example with my Windows Computer:
Get-Content weight-height.csv | python weightheight.py
Here are a some lines from the weight-height.csv file (including the header at the top)::
"Gender","Height","Weight" "Male",73.847017017515,241.893563180437 "Male",68.7819040458903,162.310472521300 "Male",74.1101053917849,212.7408555565 "Male",71.7309784033377,220.042470303077 "Male",69.8817958611153,206.349800623871
"Female",58.9107320370127,102.088326367840 "Female",65.2300125077128,141.305822601420 "Female",63.3690037584139,131.041402692995 "Female",64.4799974256081,128.171511221632 "Female",61.7930961472815,129.781407047572 "Female",65.9680189539591,156.802082613991
Here are the programs for the assignment so far:
Histogram
import sys import stdarray import stddraw import stdrandom import stdstats
class Histogram:
# Construct self such that it can store n frequency counts. def __init__(self, n): # Frequency counts. self._freqCounts = stdarray.create1D(n, 0)
# Add one occurrence of the value i to self. def addDataPoint(self, i): self._freqCounts[i] += 1
# Draw self. def draw(self): stddraw.setYscale(0, max(self._freqCounts)) stdstats.plotBars(self._freqCounts)
def main(): n = int(sys.argv[1]) # number of biased coin flips per trial p = float(sys.argv[2]) # heads with probability p trialCount = int(sys.argv[3]) # number of trials histogram = Histogram(n + 1) for trial in range(trialCount): heads = stdrandom.binomial(n, p) histogram.addDataPoint(heads) stddraw.setCanvasSize(500, 200) stddraw.clear(stddraw.LIGHT_GRAY) histogram.draw() stddraw.show()
if __name__ == '__main__': main()
# Command line input: # python histogram.py 50 0.5 10000
Frequency Count:
import sys import stdarray import stdio import stdrandom
class Counter: def __init__(self, id, maxCount): self._name = id self._maxCount = maxCount self._count = 0
def increment(self): if self._count < self._maxCount: self._count += 1
def value(self): return self._count
def __str__(self): return self._name + ': ' + str(self._count) words = stdio.readAllStrings() words.sort() # or merge.sort(words) zipf = [] for i in range(len(words)): if (i == 0) or (words[i] != words[i-1]): entry = Counter(words[i], len(words)) zipf += [entry] zipf[len(zipf) - 1].increment() # Code directly below this line causes runtime error: # merge.sort(zipf) # or zipf.sort() # zipf.reverse() for entry in zipf: stdio.writeln(entry)
# Command line input: # get-content weight-height.csv | python frequencycount.py
Split CSV File
data_file = open("weight-height.csv").read() lines = data_file.split(' ') lines = lines[1:-1] # for removing first and last row of text # last row contains null '' empty string data_lines = dict() # for loop to convert the data into dictionary for line in lines: data = line.split(',') data = data[:2] + data[4:] if data[0] in data_lines.keys(): data_lines[data[0]].append(tuple(data[1:])) # if same key found then appending the else: # tuple in the dictionary data_lines[data[0]] = [(tuple(data[1:]))] # saving as tuple in list print(data_lines)
# This program runs in Python: # Run > Run Module or F5
I have 3 programs that can aid in making 4 histograms from the data in the weight-height.csv file. That's as far as I can solve for this assignment. I appreciate your coding assistance to produce the assignment's result!.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
