[英]“ValueError: could not convert string to float: ” machine learning fit classifier
[英]ValueError: could not convert string to float - machine learning
我正在研究一個機器學習項目,以確定 PCAP 是否是一種攻擊,我必須處理 PCAP 文件並創建模型然后進行預測。 我的代碼的一部分是這樣的:
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 列表包含的內容:
[['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]]
我收到錯誤ValueError: could not convert string to float: '172.27.224.250'
in rf.fit(x_train, y_train)
我應該使用哪個分類器以及如何解決這個問題?
您需要將分類特征編碼為數值,很少有像Label Encoding和One Hot Encoding這樣的技術,它們是sklearn.preprocessing
模塊的一部分,可以讓您進行編碼。 因此,首先確定訓練集中的分類列,並按照上述鏈接進行虛擬編碼,然后應用.fit()
方法。
有關更多實現細節,請參閱標簽編碼器與一個熱編碼器。
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.