Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

My Python program analyzes data from a CSV file that is all numbers. This program is designed to find the median and standard deviation I'm trying to figure out a part where if the last column of the file contains zeros, it finds where the numbers are in the same row indexes of the other columns. It then calculates the average of these numbers. However, my program calculates them by columns first, not by rows. How could this be fixed? For example, if the last column had a zero and another column had a three in the same row index and another with 4, those numbers would be calculated.


# 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))

Expert Solution
Check Mark
Still need help?
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

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}")

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Question

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}")

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Question

How could this be done without the zip() function or enumerate?

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

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}")

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Question

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}")

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Question

How could this be done without the zip() function or enumerate?

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Knowledge Booster
Background pattern image
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education