ValueError:无法将字符串转换为浮点数-机器学习 [英] ValueError: could not convert string to float - machine learning

查看:537
本文介绍了ValueError:无法将字符串转换为浮点数-机器学习的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个机器学习项目,以确定PCAP是否是攻击,我必须处理PCAP文件并创建模型然后进行预测. 我的代码的一部分是这样的:

I'm working on a machine learning project to identify if a PCAP is an attack or not and I have to process the PCAP files and create a model and then predict. a part of my code is like this:

train['is_train'] = np.random.uniform(0, 1, len(train)) <= .75
Train, Validate = train[train['is_train']==True], train[train['is_train']==False]
features = list(set(list(dataset.columns))-set(ID_col)-set(target_col)-set(other_col))

x_train = Train[list(features)].values
y_train = Train["class"].values
x_validate = Validate[list(features)].values
y_validate = Validate["class"].values
x_test = test[list(features)].values


random.seed(100)
rf = RandomForestClassifier(n_estimators=1000)
rf.fit(x_train, y_train)

这就是我的x_train列表包含的内容:

and it's how my x_train list contains:

[['172.27.224.250' 16 'TCP' ... 1532299481617 60 54200]
 ['172.27.224.251' 24 'TCP' ... 1532299483068 60 502]
 ['172.27.224.251' 24 'TCP' ... 1532299483069 60 502]
 ...
 ['172.27.224.251' 24 'TCP' ... 1532301279315 60 502]
 ['172.27.224.250' 16 'TCP' ... 1532301279324 60 49713]
 ['172.27.224.250' 24 'TCP' ... 1532301279335 66 49713]]

我在rf.fit(x_train, y_train)中出现错误ValueError: could not convert string to float: '172.27.224.250'

我应该使用哪个分类器以及如何解决此问题?

which classifier should I use and how can I solve this problem?

推荐答案

您需要将分类特征编码为数值,很少有像

You need to encode your categorical features into numeric values, there are few techniques like Label Encoding and One Hot Encoding which are a part of sklearn.preprocessing module which will allow you to do the encoding. So first identify the columns which are categorical in your train set and do the dummy encoding as mentioned in the above links and then apply .fit() method.

有关更多实施细节,请参见

For more implementation details see Label encoder vs one hot encoder.

希望这会有所帮助!

这篇关于ValueError:无法将字符串转换为浮点数-机器学习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆