Cannot read matrix from file and calculate mean.: TypeError: cannot perform redundancy with flexible type

Asked 2 years ago, Updated 2 years ago, 381 views

I would like to calculate the average value of each line in the following files using Python.

A=8×8 matrix

So I wrote the following code, but it turned out to be an error.
What should I do?

code:

import numpy as np

f = open ('A')
lines=f.readlines()

print(np.mean(lines,axis=0))

error messages:

Traceback (most recent call last):
  File "ave.py", line 6, in <module>
    print(np.mean(lines,axis=0))
  File"/usr/lib64/python 2.7/site-packages/numpy/core/fromnumeric.py", line2488, in mean
    out=out, keepdims=keepdims)
  File"/usr/lib64/python 2.7/site-packages/numpy/core/_methods.py", line51, in_mean
    out=out, keepdims=keepdims)
TypeError: cannot perform redundancy with flexible type

python numpy

2022-09-30 21:50

1 Answers

3x3 matrix for example

print(A)
array([1,2,3],
       [4, 5, 6],
       [7, 8, 9]])

np.mean(A,axis=1)
array([2.,5.,8.])

np.mean(A,axis=0)
array([4.,5.,6.])


2022-09-30 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.