Преобразовать тип объекта в тип с плавающей точкой - PullRequest
0 голосов
/ 29 января 2019

Эта цель - преобразовать тип из 'object' в 'float' из набора данных KDD 99.

Это информация о наборе данных:

class 'pandas.core.frame.DataFrame'
RangeIndex: 494020 entries, 0 to 494019
Data columns (total 42 columns):
duration                       494020 non-null int64
protocol_type                  494020 non-null object
service                        494020 non-null object
src_bytes                      494020 non-null object
dst_bytes                      494020 non-null int64
flag                           494020 non-null int64
land                           494020 non-null int64
wrong_fragment                 494020 non-null int64
urgent                         494020 non-null int64
hot                            494020 non-null int64
num_failed_logins              494020 non-null int64
logged_in                      494020 non-null int64
num_compromised                494020 non-null int64
root_shell                     494020 non-null int64
su_attempted                   494020 non-null int64
num_root                       494020 non-null int64
num_file_creations             494020 non-null int64
num_shells                     494020 non-null int64
num_access_files               494020 non-null int64
num_outbound_cmds              494020 non-null int64
is_hot_login                   494020 non-null int64
is_guest_login                 494020 non-null int64
count                          494020 non-null int64
serror_rate                    494020 non-null int64
rerror_rate                    494020 non-null float64
same_srv_rate                  494020 non-null float64
diff_srv_rate                  494020 non-null float64
srv_count                      494020 non-null float64
srv_serror_rate                494020 non-null float64
srv_rerror_rate                494020 non-null float64
srv_diff_host_rate             494020 non-null float64
dst_host_count                 494020 non-null int64
dst_host_srv_count             494020 non-null int64
dst_host_same_srv_rate         494020 non-null float64
dst_host_diff_srv_rate         494020 non-null float64
dst_host_same_src_port_rate    494020 non-null float64
dst_host_srv_diff_host_rate    494020 non-null float64
dst_host_serror_rate           494020 non-null float64
dst_host_srv_serror_rate       494020 non-null float64
dst_host_rerror_rate           494020 non-null float64
dst_host_srv_rerror_rate       494020 non-null float64
class                          494020 non-null object
dtypes: float64(15), int64(23), object(4)
memory usage: 158.3+ MB

Есть 4 объектаТипы, которые необходимо преобразовать в число с плавающей запятой, содержат:

1. protocol type : 'tcp' , 'udp' , 'icmp'
2. service : 'http' , 'auth' , 'http_443' , etc
3. src_bytes : 'OTH' 'REJ' , 'SF' , etc
4. class : 'normal' , 'neptune' , 'smurf' , etc

model('protocol_type').astype(float)

Но я получил эту ошибку:

TypeError: 'DataFrame' object is not callable

Я надеюсь, что кто-то может помочь мне решить эту проблему.Спасибо:)

1 Ответ

0 голосов
/ 29 января 2019

Прежде всего, как указывало @thecruisy, вы должны использовать скобки вместо (), что приводит к:

model['protocol_type'].astype(float)

Однако, поскольку столбец находится в объекте (или str),это повысит ValueError.

ValueError: could not convert string to float: 'tcp'

. Вместо этого вы должны кодировать их.Вы можете использовать либо pandas.DataFrame:

model['protocol_type'].astype('category').cat.codes.astype(float)
#                                                  ^^^^^^^^^^^^^^
#                                   This may be redundant, though

, либо использовать sklearn.preprocessing.LabelEncoder

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...