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
- NEED A C++ PROGRAM CODE IN VISUAL STUDIO AND OUTPUT Write a C++ program to sum rows and columns in given 2D array? IMAGE GIVENarrow_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, 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_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 OR ANYTHING COMPLICATED PLEASE.. USE BASIC…arrow_forwardYou 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_forward
- hello, i need to create a python code for the following image. It has to start off by asking the user to enter how many rows, and then ask the user for which color they would like for each row. The colors must be stored in a list. It should look like the following image. If there are more rows the squares should be smaller and larger if there are less rows. Any Help?arrow_forwardite a java program that contains a two-dimensional array of 2 rows and 3 columns. Find the sum of each row in the array, trace the code using a tracing table. - Note: No two students can try the same inputs. - You should submit the code and the tracing table in a handwritten pdf file. - Also, you should submit the code and a screenshot of the output using a word file.arrow_forwardWrite a program that can read in a rank 2 array from an input disk file and calculate the sums of all the data in each row and each column in the array. The size of the array to read in will be specified by two numbers on the first line in the input file, and the elements in each row of the array will be found on a single line of the input file. Use allocatable arrays that are adjusted to match the number of rows and columns in the file each time the program is run. An example of an input data file containing a 2 row x 4 column array is shown below: 2 4 -24.0 -1121. 812.1 11.1 35.6 8.1E3 135.23 -17.3 Write out the results in the form: The array shape is: Sum of row 1 = Sum of row 2 = ... Sum of col 1 = Sum of col 2 = ...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