Skip to content
Snippets Groups Projects
Commit def4e73c authored by clac16's avatar clac16
Browse files

Ajusta scripts para resumir resultados

parent 297bb293
Branches
No related tags found
No related merge requests found
import csv
import numpy as np
# Function to process the files and calculate the mean for each field
def compute_means(file_pattern, num_files):
# Create a list to store the data from each file
data = []
# Read each file and parse its content
for i in range(1, num_files + 1):
file_name = file_pattern.format(i)
with open(file_name, newline='', encoding='utf-8') as f:
reader = csv.reader(f, delimiter=',')
file_data = []
for row in reader:
# Strip leading/trailing spaces from each field and convert to float
cleaned_row = [field.strip() for field in row]
file_data.append(cleaned_row)
data.append(file_data)
# Assuming the structure is consistent across all files, i.e., same rows and columns
num_rows = len(data[0]) # Number of rows
num_columns = len(data[0][0]) # Number of columns (excluding the identifier)
# We need to store the means for each field (row, column)
means = []
# Iterate over each row and each column (excluding the first column, which is the identifier)
for row_index in range(num_rows):
row_means = []
for col_index in range(1, num_columns): # Starting from 1 to exclude the first column
values = []
for file_data in data:
# Extract the value from the current row and column (skip the identifier)
value = file_data[row_index][col_index]
try:
values.append(float(value))
except ValueError:
values.append(np.nan) # Handle any non-numeric values (e.g., NaN or missing data)
# Calculate the mean for the current field (ignoring NaN values)
mean_value = np.nanmean(values)
row_means.append(mean_value)
means.append(row_means)
return means
# Function to save the computed means to a new CSV file
def save_means_to_file(means, file_name="media-aritmetica-resultados.csv"):
with open(file_name, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
# Assuming we have the same rows and columns as the original files,
# write the means to a new file.
for row in means:
writer.writerow(row)
# Main function to execute the program
if __name__ == "__main__":
file_pattern = "resultados-{}.txt"
num_files = 8
# Step 1: Compute the means
means = compute_means(file_pattern, num_files)
# Step 2: Save the results to a CSV file
save_means_to_file(means, "media-aritmetica-resultados.csv")
print("Mean values have been computed and saved to 'media-aritmetica-resultados.csv'.")
import csv
import math
import numpy as np
def clean_field(value):
return value.strip()
def arithmetic_mean(values):
return sum(values) / len(values)
def geometric_mean(values):
product = math.prod(values)
return product**(1/len(values))
def geometric_std_dev(values):
geo_mean = geometric_mean(values)
log_ratios = [math.log(v / geo_mean) for v in values]
variance = sum(r ** 2 for r in log_ratios) / len(log_ratios)
return math.exp(math.sqrt(variance))
data = {}
file_count = 8
for n in range(1, file_count + 1):
filename = f"resultados-{n}.txt"
with open(filename, "r") as f:
reader = csv.reader(f)
for row in reader:
key = clean_field(row[0])
values = list(map(lambda x: float(clean_field(x)), row[1:]))
if key not in data:
data[key] = []
data[key].append(values)
output = []
for key, all_values in data.items():
num_cols = len(all_values[0])
col_means = [arithmetic_mean([values[c] for values in all_values]) for c in range(num_cols)]
line_geo_mean = geometric_mean(col_means)
line_geo_means = [geometric_mean(values) for values in all_values]
line_geo_std_dev = geometric_std_dev(line_geo_means)
col_means_excl_12 = [arithmetic_mean([values[c] for values in all_values]) for c in range(num_cols) if c != 10]
line_geo_mean_excl_12 = geometric_mean(col_means_excl_12)
line_geo_means_excl_12 = [geometric_mean([v for i, v in enumerate(values) if i != 10]) for values in all_values]
line_geo_std_dev_excl_12 = geometric_std_dev(line_geo_means_excl_12)
output.append([key, round(line_geo_mean, 2), round(line_geo_std_dev, 3), round(line_geo_mean_excl_12, 2), round(line_geo_std_dev_excl_12, 3)])
with open("tabela-medias-geometricas.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["FLAGS", "GEOMEAN", "GEOSD", "GEOMEANEXLUD", "GEOSDEXLUD"])
writer.writerows(output)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment