python - 如何解决:float() 参数可以是字符串或数字,而不是“地图”
问题描述
这是我的简单代码:
我试过改变一些数据类型
@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'
请帮我解决这个问题
解决方案
您正在制作一个map
对象列表。试试这个列表理解:
data_raw = [[int(x) for x in i.split()[1:-1]] for i in open(filename)]
split
默认为空格分割,因此该参数是不必要的。另外,考虑使用with
正确关闭您的文件:
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]
推荐阅读
- laravel - 如何在验证的 after 函数中访问请求参数?
- selenium - 当 Protractor 测试远程运行时,浏览器 URL 不加载
- go - Type a type后,不能再混用
- postgresql - 如何重置表的存储参数
- reactjs - 你如何在反应中设置嵌套 css 的样式?
- javascript - 当我使用 JQuery.find 时,哪个是“选择”选项列表的 jQuery 选择器
- javafx - 如何使 JavaFX Stage 始终处于底部?
- java - 如何在JAVA中找到随机生成的数字的平均值
- c - 如何使用 C 或 glsl 实现 2D FFT 以将图像转换为频率空间并返回
- c - 调用 fork() 然后变成调用 sys_fork() 的过程是什么?