
Concept explainers
My Python
# Lucas Conklin
# 5772707
import csv
import statistics
def readCSVIntoDictionary(f_name):
data = []
with open(f_name) as f:
reader = csv.reader(f)
for row in reader:
if not data:
for index in range(len(row)):
data.append([])
for index in range(len(row)):
data[index].append(float(row[index]))
f.close()
return data
features = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\pima.csv")
row = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\pima.csv")
def find_median_and_SD(data, feature):
med = statistics.median(data[feature])
rounded_med = round(med, 4)
st_dev = statistics.stdev(data[feature])
rounded_st_dev = round(st_dev, 5)
return rounded_med, rounded_st_dev
for i in range(0, len(features)):
(median, standard_deviation) = find_median_and_SD(features, i)
print(f'Feature {i} Median: {median} Standard Deviation: {standard_deviation}')
for feature in range(len(features)-1):
valid_data = []
for i in range(len(features[-1])):
current_feat = features[-1][i]
target_feature = 0
if target_feature == current_feat:
valid_data.append(features[feature][i])
print(statistics.median(valid_data))
print(statistics.stdev(valid_data))

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

How could this code work if zero_indices were made into a for loop instead of zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
def find_nonzero_indices(col):
nonzero_indices = [];
for i in range(len(col)):
if col[i] != 0 :
nonzero_indices.append(i)
return nonzero_indices
features = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\pima.csv")
last_column = features[-1]
zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
for i in range(len(features[0])):
if i not in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
valid_data.append(features[j][i])
median, stdev = find_median_and_SD(features, i)
print(f"Feature {i} Median: {median} Standard Deviation: {stdev}")
for zero_index in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
if features[j][zero_index] != 0:
valid_data.append(features[j][zero_index])
if valid_data:
avg = sum(valid_data)/len(valid_data)
print(f"Average of non-zero values in row {zero_index}: {avg}")
else:
print(f"There are no non-zero values in row {zero_index}")
How could this code work without
zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
def find_nonzero_indices(col):
nonzero_indices = [];
for i in range(len(col)):
if col[i] != 0 :
nonzero_indices.append(i)
return nonzero_indices
features = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\pima.csv")
last_column = features[-1]
zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
for i in range(len(features[0])):
if i not in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
valid_data.append(features[j][i])
median, stdev = find_median_and_SD(features, i)
print(f"Feature {i} Median: {median} Standard Deviation: {stdev}")
for zero_index in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
if features[j][zero_index] != 0:
valid_data.append(features[j][zero_index])
if valid_data:
avg = sum(valid_data)/len(valid_data)
print(f"Average of non-zero values in row {zero_index}: {avg}")
else:
print(f"There are no non-zero values in row {zero_index}")
How could this be done without the zip() function or enumerate?
How could this code work if zero_indices were made into a for loop instead of zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
def find_nonzero_indices(col):
nonzero_indices = [];
for i in range(len(col)):
if col[i] != 0 :
nonzero_indices.append(i)
return nonzero_indices
features = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\pima.csv")
last_column = features[-1]
zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
for i in range(len(features[0])):
if i not in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
valid_data.append(features[j][i])
median, stdev = find_median_and_SD(features, i)
print(f"Feature {i} Median: {median} Standard Deviation: {stdev}")
for zero_index in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
if features[j][zero_index] != 0:
valid_data.append(features[j][zero_index])
if valid_data:
avg = sum(valid_data)/len(valid_data)
print(f"Average of non-zero values in row {zero_index}: {avg}")
else:
print(f"There are no non-zero values in row {zero_index}")
How could this code work without
zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
def find_nonzero_indices(col):
nonzero_indices = [];
for i in range(len(col)):
if col[i] != 0 :
nonzero_indices.append(i)
return nonzero_indices
features = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\pima.csv")
last_column = features[-1]
zero_indices = [i for i in range(len(last_column)) if last_column[i] == 0]
for i in range(len(features[0])):
if i not in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
valid_data.append(features[j][i])
median, stdev = find_median_and_SD(features, i)
print(f"Feature {i} Median: {median} Standard Deviation: {stdev}")
for zero_index in zero_indices:
valid_data = [];
for j in range(len(features[-1]):
if features[j][zero_index] != 0:
valid_data.append(features[j][zero_index])
if valid_data:
avg = sum(valid_data)/len(valid_data)
print(f"Average of non-zero values in row {zero_index}: {avg}")
else:
print(f"There are no non-zero values in row {zero_index}")
How could this be done without the zip() function or enumerate?
- Write a program that reads movie data from a csv (comma separated values) file and output the data in a formatted table. The program first reads the name of the CSV file from the user. The program then reads the csv file and outputs the contents according to the following requirements: Each row contains the title, rating, and all showtimes of a unique movie. A space is placed before and after each vertical separator (|) in each row. Column 1 displays the movie titles and is left justified with a minimum of 44 characters. If the movie title has more than 44 characters, output the first 44 characters only. Column 2 displays the movie ratings and is right justified with a minimum of 5 characters. Column 3 displays all the showtimes of the same movie, separated by a space. Each row of the csv file contains the showtime, title, and rating of a movie. Assume data of the same movie are grouped in consecutive rows. Ex: If the input of the program is: movies.csv and the contents of movies.csv…arrow_forwardPYTHON I have a file name KindOfNumbers.csv In that csv file there are these numbers: 2, 3, 6, 8, 9, 13, 16, 15, 28, 97, 64, 67, 59, 100, 128, 496, 386 893 4567, 843, 894, 935, 974, 863, 991. In PYTHON, After reading the data from the csv file, CREATE THE FOLLOWING LISTS FROM GIVEN NUMBERS: _PRIME NUMBERS _PERFECT NUMBERS _ODD NUMBERS _EVEN NUMBERS ***EACH LIST SHOULD BE SORTED. ***WRITE THE ARRAYS, INCLUDING THE SORTED ORIGINAL, TO A FILE WITH APPROPRIATE DESCRIPTION INCLUDING THE NUMBERS OF VALUES IN THE ARRAY.arrow_forwardTask. Your task is to develop a python program that reads input from text file and finds if the alphanumeric sequence in each line of the provided input file is valid for Omani car license plate. The rules for valid sequences for car plates in Oman are as follows: Each sequence is composed of 1 to 5 digits followed by one or two letters. Digits cannot start with 0, for instance, 00, 011, or 09 are not valid digit sequences. The following list are the only valid letter combinations: ['A','AA','AB','AD','AR','AM','AW','AY', 'B','BA','BB','BD','BR','BM','BW','BY', 'D',DA','DD','DR','DW','DY', "R',RA','RR','RM','RW','RY', 'S','SS', 'M', 'MA','MB','MM','MW','MY', W',WA','WB','ww, Y,YA','YB','YD','YR','YW','YY'] Program Input/Output. Your program should read input from a file named plates.txt and write lines with valid sequences to a file named valid.txt. Any line from the input file containing invalid sequence should be written to a file named invalid.txt. Each line from the input file must…arrow_forward
- Create a program in Python that reads data from Breakfast Menu (https://www.w3schools.com/xml/simple.xml) and builds parallel arrays for the menu items, with each array containing the menu item name, description, calories, and price, respectively. After reading the data and building the arrays, display the menu items similar to the following: name - description - calories - priceAt the bottom, display the total number of items on the menu, the average number of calories per item, and the average price per item similar to: 0 items - 0 average calories - $0.00 average price You may either read the page using Internet processing methods, or you may download and save the page and then read the data from the saved file. You must process the data using string functions (no XML libraries). must use separate subroutines/functions/methods to implement each type of processing, and include error handling for missing or invalid data. NO REGEX, TAGS OR ANYTHING COMPLICATED PLEASE.. USE…arrow_forwardWrite a program that reads movie data from a CSV (comma separated values) file and output the data in a formatted table. The program first reads the name of the CSV file from the user. The program then reads the CSV file and outputs the contents according to the following requirements: Each row contains the title, rating, and all showtimes of a unique movie. A space is placed before and after each vertical separator ('|') in each row. Column 1 displays the movie titles and is left justified with a minimum of 44 characters. If the movie title has more than 44 characters, output the first 44 characters only. Column 2 displays the movie ratings and is right justified with a minimum of 5 characters. Column 3 displays all the showtimes of the same movie, separated by a space. Each row of the CSV file contains the showtime, title, and rating of a movie. Assume data of the same movie are grouped in consecutive rows. Hints: Use the find() function to find the index of a comma in each row of…arrow_forwardCreate a program in Python that reads data from Breakfast Menu (https://www.w3schools.com/xml/simple.xml) and builds parallel arrays for the menu items, with each array containing the menu item name, description, calories, and price, respectively. After reading the data and building the arrays, display the menu items similar to the following: name - description - calories - priceAt the bottom, display the total number of items on the menu, the average number of calories per item, and the average price per item similar to: 0 items - 0 average calories - $0.00 average price You may either read the page using Internet processing methods, or you may download and save the page and then read the data from the saved file. You must process the data using string functions (no XML libraries). must use separate subroutines/functions/methods to implement each type of processing, and include error handling for missing or invalid data. NO REGEX OR ANYTHING COMPLICATED PLEASE.. USE BASIC…arrow_forward
- You saw that NumPy outputs a two-dimensional array "s" in a nice column-based format that right-aligns every element in a field width. The field width’s size is determined by the array element value that requires the most character positions to display. To understand how powerful it is to have this formatting simply built-in, write a function that reimplements NumPy’s array formatting for a two-dimensional array "s" using loops. Assume the array contains only positive integer values.arrow_forwardCreate a file with given numbers. Read them from file with a python program andcalculate mean, median, variance and standard deviation for this data.Mean, Variance, Standard Deviation,Here μ = = Mean900 932 298 918 645 505 922 324 979 360 775 53 12 986 764 400 81 923 233 450897 166 787 148 376 385 49 62 149 618 568 270 52 295 278 705 942 341 365 74 538604 958 816 634 566 216 919 475 988 732 835 200 741 587 910 183 204 684 349373 29 503 63 217 213 656 416 350 386 257 827 820 807 369 634 185 690 255 312380 449 564 615 966 342 153 425 830 365 347 487 550 788 959 79 129 666 115 27arrow_forwardIn Python: Given the name of a text file that is read from user input and contains student scores, complete a program to perform the following tasks: Load student scores from the text file into a NumPy array. Calculate the median and average of the student scores. Curve the student scores by adding the difference of 100 and the maximum score (100 - max) to each score. As a result, the new maximum score would become 100. Output the median and average scores with two digits after the decimal point. Output the curved scores as a NumPy array. End the last output with a newline. Follow the output format as shown in the example below. Note: Different input text files will be used by the auto-grader. The number of student scores in each file may vary. Ex: If the input of the program is: scores.txt the output is: Median = 73.00 Average = 73.32 Curved scores = [ 70 95 84 68 98 73 67 88 73 100 79 92 100 83 98 85 100 85 77 95 99 81 69 74 75]arrow_forward
- You can see in the above display, we first sort each row of the 2D array; we then take the transpose of a two D array, i.e., all the row elements becoming the column elements; we then sort each row of the 2D again. If you read the final array, each row is sorted; each column is also sorted. The smallest element obviously is the 1st element of the two D array and the last element is the largest element of a two D array. Let us now look at the following UML diagram: (Note that additional methods are allowed; proposed methods and instance variable cannot be changed) Main method firstly constructs a 2D array of certain sizes and then construct a TwoD object and drive the task according to the above runtime interactions and displays. TwoD class has only one instance variable which is a two D array of numbers ( int or double). The constructor must do some “deep” copying. A copy constructor. The other three methods are obvious in definition: to sort each row, to rotate the 2D array (i.e.,…arrow_forwardPlease help me with my code. Create a program in Python using RegEx that reads data from Breakfast Menu (https://www.w3schools.com/xml/simple.xml) and builds parallel arrays for the menu items, with each array containing the menu item name, description, calories, and price, respectively. After reading the data and building the arrays, display the menu items similar to the following: name - description - calories - priceAt the bottom, display the total number of items on the menu, the average number of calories per item, and the average price per item similar to: 0 items - 0 average calories - $0.00 average price You may either read the page using Internet processing methods, or you may download and save the page and then read the data from the saved file. You must process the data using string functions (no XML libraries). must use separate subroutines/functions/methods to implement each type of processing, and include error handling for missing or invalid data. Please use…arrow_forwardIn Python IDLE: How would I write a function for the problem in the attached image?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





