Expert answer:please do a modification on the code that I uploaded, so that when you input the file “montana-counties.txt” and produce the output file and the graph
lab_12_code.pdf

lab_12_instruction.pdf

montana_counties.txt

output.pdf

Unformatted Attachment Preview

11/17/2017
https://www.cs.montana.edu/paxton/classes/csci127/inlabs/lab12/lab12.py
import numpy as np
import matplotlib.pyplot as plt
#
#
#
#
#
—————————————————-CSCI 127, Lab 12
November 21, 2017
Your Name
—————————————————–
def read_file(name):
input_file = open(name, “r”)
number_buckets = int(input_file.readline())
total_counties = int(input_file.readline())
county_populations = np.zeros([total_counties], dtype=”int”)
for county_number in range(total_counties):
line = input_file.readline().split(“,”)
county_populations[county_number] = int(line[1])
county_populations.sort()
input_file.close()
return number_buckets, county_populations
# —————————————————-def print_summary(averages):
print(“Population Grouping Summary”)
print(“—————————“)
for grouping in range(len(averages)):
print(“Grouping”, grouping + 1, “has a population average of”,
averages[grouping])
# —————————————————-# Do not change anything above this line
# —————————————————-def calculate_averages(number_buckets, county_populations):
pass
# —————————————————-def graph_summary(averages):
pass
# —————————————————-number_buckets, county_populations = read_file(“montana-counties.txt”)
averages = calculate_averages(number_buckets, county_populations)
print_summary(averages)
graph_summary(averages)
https://www.cs.montana.edu/paxton/classes/csci127/inlabs/lab12/lab12.py
1/1
11/17/2017
CSCI 127, In-Lab 12
Lab 12: matplotlib
Logistics
Due: Tuesday, November 21st no later than midnight.
Partner Information: Complete this assignment individually.
Submission Instructions: Upload your solution, named YourFirstName-YourLastName.py to the
BrightSpace Lab 12 Dropbox.
Deadline Reminder: Once this deadline passes, BrightSpace will no longer accept your Python submission
and you will no longer be able to earn credit. Thus, if you are not able to fully complete the assignment,
submit whatever you have before the deadline so that partial credit can be earned.
Learning Outcomes
Gain experience creating graphs with matplotlib.
Gain additional experience using NumPy arrays.
Background
The input file for this assignment is montana-counties.txt.
The first line contains the number of desired groupings (e.g. 4).
The second line contains the number of counties (e.g. 56).
The remaining lines contain information about each county using comma-separated values. The first value
is the name of the county (e.g. Beaverhead County) and the second value is the population of that county
in 2010 (e.g. 9401). These lines are arranged in alphabetical order by county name and contain
information from this site.
Assignment
Download lab12.py, rename it according to the instructions above, and make sure you understand it.
Take the program above and modify it by adding the missing methods such that when the program is run,
it produces this text output and this graphical output.
calculate_averages Specification
The function takes as input how many buckets (number_of_buckets) should be used to summarize the
county census data (county_populations).
The function should return a NumPy array that contains number_of_buckets slots.
You may assume that the number of counties is evenly divisible by the number of buckets. For our specific
input file, there are 56 counties and 4 buckets so each slot in the array will summarize 14 counties. For
convenience, denote this number (14) as n.
The first slot in the array should contain the average of the n least populous counties.
The second slot in the array should contain the average of the next n least populous counties.
Etc.
graph_summary Specification
The function takes as input the NumPy array that is returned by the calculate_averages function.
https://www.cs.montana.edu/paxton/classes/csci127/inlabs/lab12/
1/2
11/17/2017
CSCI 127, In-Lab 12
The function uses matplotlib to produce this graph.
Grading – 10 points
3 points – The print_summary function produces the desired output when the complete program is run.
1 point – The graph_summary function creates a graph.
1 point – The words “CSCI 127, Lab 12” appear in the top gray bar.
1 point – The graph is entitled “Montana County Population Analysis”.
1 point – Blue hexagons are used to represent each point.
1 point – The blue hexagons are connected with a dashed, cyan line.
1 point – The x-axis and y-axis are labeled with the correct labels.
1 point – The x-axis contains marks for 1, 2, 3 and 4 (and nothing else).
If Time Remains
Work on Program 5, seeking feedback from your lab assistant if desired.
https://www.cs.montana.edu/paxton/classes/csci127/inlabs/lab12/
2/2
4
56
Beaverhead County,9401
Big Horn County,13343
Blaine County,6601
Broadwater County,5747
Carbon County,10460
Carter County,1203
Cascade County,81755
Chouteau County,5759
Custer County,11924
Daniels County,1755
Dawson County,9327
Deer Lodge County,9085
Fallon County,3120
Fergus County,11413
Flathead County,98082
Gallatin County,104502
Garfield County,1310
Glacier County,13694
Golden Valley County,831
Granite County,3368
Hill County,16542
Jefferson County,11853
Judith Basin County,1940
Lake County,29758
Lewis and Clark County,67282
Liberty County,2409
Lincoln County,19259
Madison County,7924
McCone County,1700
Meagher County,1827
Mineral County,4184
Missoula County,116130
Musselshell County,4589
Park County,16114
Petroleum County,489
Phillips County,4133
Pondera County,6084
Powder River County,1746
Powell County,6858
Prairie County,1182
Ravalli County,42088
Richland County,11482
Roosevelt County,11305
Rosebud County,9287
Sanders County,11534
Sheridan County,3648
Silver Bow County,34553
Stillwater County,9406
Sweet Grass County,3623
Teton County,6056
Toole County,4977
Treasure County,692
Valley County,7539
Wheatland County,2117
Wibaux County,1093
Yellowstone County,15843711/17/2017
Population Grouping Summary
————————–Grouping 1 has a population
Grouping 2 has a population
Grouping 3 has a population
Grouping 4 has a population
https://www.cs.montana.edu/paxton/classes/csci127/inlabs/lab12/output.txt
average
average
average
average
of
of
of
of
362
1227
2534
14491
https://www.cs.montana.edu/paxton/classes/csci127/inlabs/lab12/output.txt
1/1

Purchase answer to see full
attachment