Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
IC
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clac16
IC
Commits
def4e73c
Commit
def4e73c
authored
4 months ago
by
clac16
Browse files
Options
Downloads
Patches
Plain Diff
Ajusta scripts para resumir resultados
parent
297bb293
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
resultados/calcula-media-aritmetica.py
+73
-0
73 additions, 0 deletions
resultados/calcula-media-aritmetica.py
resultados/calcula-media-geometrica-resumida.py
+53
-0
53 additions, 0 deletions
resultados/calcula-media-geometrica-resumida.py
with
126 additions
and
0 deletions
resultados/calcula-media-aritmetica.py
0 → 100644
+
73
−
0
View file @
def4e73c
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
'
.
"
)
This diff is collapsed.
Click to expand it.
resultados/calcula-media-geometrica-resumida.py
0 → 100644
+
53
−
0
View file @
def4e73c
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment