Python 如何修复:float()参数可以是字符串或数字,而不是';地图';
这是我的简单代码: 我已尝试更改某些数据类型Python 如何修复:float()参数可以是字符串或数字,而不是';地图';,python,numpy,typeerror,Python,Numpy,Typeerror,这是我的简单代码: 我已尝试更改某些数据类型 @staticmethod def load_from_file(filename, size_fit = 50): ''' Loads the signal data from a file. filename: indicates the path of the file. size_fit: is the final number of sample axes will have. It uses lin
@staticmethod
def load_from_file(filename, size_fit = 50):
'''
Loads the signal data from a file.
filename: indicates the path of the file.
size_fit: is the final number of sample axes will have.
It uses linear interpolation to increase or decrease
the number of samples.
'''
#Load the signal data from the file as a list
#It skips the first and the last line and converts each number into an int
data_raw = list(map(lambda x: int(x), i.split(" ")[1:-1]) for i in open(filename))
#Convert the data into floats
data = np.array(data_raw).astype(float)
#Standardize the data by scaling it
data_norm = scale(data)
它抛出一个错误,如下所示:
data=np.array(data_raw).astype(float)
float() argument must be 'string' or 'number', not 'map'
请帮助我解决此问题您正在制作
地图
对象列表。请尝试以下列表:
data_raw = [[int(x) for x in i.split()[1:-1]] for i in open(filename)]
split
默认为在空白处拆分,因此不需要该参数。此外,请考虑将与
一起使用以正确关闭文件:
with open(filename) as infile:
data_raw = [[int(x) for x in i.split()[1:-1]] for i in infile]
另一方面,
numpy
在执行astype
操作时会将字符串转换为数字,因此您只需执行以下操作即可
with open(filename) as infile:
data_raw = [i.split()[1:-1] for i in infile]
您能告诉我们您试图打开的文件是什么样子的吗?同样在我看来,您需要“具体化”生成的地图列表。为了清晰起见,您最好将该行(原始数据)分解为多个步骤。