как исправить: аргумент float () может быть строкой или числом, а не «картой» - PullRequest
0 голосов
/ 21 апреля 2019

это мой простой код:

Я попытался изменить какой-либо тип данных

@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' 

, пожалуйста, помогите мне разрешитьэтот выпуск

1 Ответ

1 голос
/ 21 апреля 2019

Вы составляете список 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]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...