此目标是将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种对象类型需要转换为float包含:
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
我希望有人可以帮助我解决此问题。 谢谢:)
答案 0 :(得分:1)
首先,正如@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