簡體   English   中英

Python:ValueError:無法將字符串轉換為浮點數:讀取輸入文件以應用隨機森林時“隔離”

[英]Python: ValueError: could not convert string to float: 'Isolated' when reading input file for applying random forest

我正在嘗試將隨機森林應用於以下輸入文件:

gold,Program,Requirement,MethodType,Top,Side,CallersT,CallersN,CallersU,CallersCallersT,CallersCallersN,CallersCallersU,CalleesT,CalleesN,CalleesU,CalleesCalleesT,CalleesCalleesN,CalleesCalleesU
T,chess,1,Inner,T,T,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,-1,Low,
N,chess,2,Inner,N,N,-1,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,Low,
N,chess,3,Inner,N,N,-1,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,Low,
N,chess,4,Root,N,N,-1,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,Low,
N,chess,5,Inner,N,N,-1,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,Low,
N,chess,6,Root,N,N,-1,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,Low,
N,chess,7,Inner,N,N,-1,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,Low,
N,chess,8,Inner,N,N,-1,Low,-1,-1,Low,-1,-1,High,-1,-1,-1,Low,
N,chess,1,Leaf,NU,N,-1,Low,-1,-1,Low,-1,-1,Medium,Medium,-1,High,High,
N,chess,2,Leaf,NU,N,-1,Low,-1,-1,Low,-1,-1,Medium,Medium,-1,High,High,
N,chess,3,Leaf,NU,N,-1,Low,-1,-1,Low,-1,-1,Medium,Medium,-1,High,High,
N,chess,4,Root,NU,N,-1,Low,-1,-1,Low,-1,-1,Medium,Medium,-1,High,High,
N,chess,5,Isolated,NU,N,-1,Low,-1,-1,Low,-1,-1,Medium,Medium,-1,High,High,
T,chess,6,Inner,TU,T,Low,-1,-1,Low,-1,-1,Medium,-1,Medium,High,-1,High,
T,chess,7,Isolated,TU,T,Low,-1,-1,Low,-1,-1,Medium,-1,Medium,High,-1,High,
N,chess,8,Inner,NU,N,-1,Low,-1,-1,Low,-1,-1,Medium,Medium,-1,High,High,
N,chess,1,Inner,TNU,N,-1,Low,-1,-1,-1,-1,Low,Low,High,Medium,-1,Medium,
N,chess,2,Inner,NU,N,-1,Low,-1,-1,-1,-1,-1,Medium,High,Low,Low,Medium,
N,chess,3,Inner,NU,N,-1,Low,-1,-1,-1,-1,-1,Medium,High,-1,Medium,Medium,
T,chess,4,Inner,NU,N,-1,Low,-1,-1,-1,-1,-1,Medium,High,Low,Low,Medium,
N,chess,5,Leaf,NU,N,-1,Low,-1,-1,-1,-1,-1,Medium,High,-1,Medium,Medium,

這是我用來應用隨機森林的代碼:

import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split
# Feature Scaling
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

X_train={}
X_test={}
y_train={}
y_test={}
dataset = pd.read_csv( 'dataExtended2.txt', sep= ',') 
    #convert T into 1 and N into 0
dataset['gold'] = dataset['gold'].astype('category').cat.codes
dataset['Program'] = dataset['Program'].astype('category').cat.codes
dataset['MethodType'] = dataset['MethodType'].astype('category').cat.codes
dataset['Top'] = dataset['Top'].astype('category').cat.codes
dataset['Side'] = dataset['Side'].astype('category').cat.codes
dataset['CallersT'] = dataset['CallersT'].astype('category').cat.codes
dataset['CallersN'] = dataset['CallersN'].astype('category').cat.codes
dataset['CallersU'] = dataset['CallersU'].astype('category').cat.codes
dataset['CallersCallersT'] = dataset['CallersCallersT'].astype('category').cat.codes
dataset['CallersCallersN'] = dataset['CallersCallersN'].astype('category').cat.codes
dataset['CallersCallersU'] = dataset['CallersCallersU'].astype('category').cat.codes
dataset['CalleesT'] = dataset['CalleesT'].astype('category').cat.codes
dataset['CalleesN'] = dataset['CalleesN'].astype('category').cat.codes
dataset['CalleesU'] = dataset['CalleesU'].astype('category').cat.codes
dataset['CalleesCalleesT'] = dataset['CalleesCalleesT'].astype('category').cat.codes
dataset['CalleesCalleesN'] = dataset['CalleesCalleesN'].astype('category').cat.codes
dataset['CalleesCalleesU'] = dataset['CalleesCalleesU'].astype('category').cat.codes
pd.set_option('display.max_columns', None)

print(dataset.head())
row_count, column_count = dataset.shape
   
X = dataset.iloc[:, 1:column_count].values
y = dataset.iloc[:, 0].values
Xcol = dataset.iloc[:, 1:column_count]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
   
sc = StandardScaler()
X_train = sc.fit_transform(X_train)

我收到錯誤: ValueError: could not convert string to float: 'Isolated' when execution of my code ( X_train = sc.fit_transform(X_train) ) 雖然我正在使用以下代碼行: dataset['MethodType'] = dataset['MethodType'].astype('category').cat.codesMethodType從字符串轉換為浮點數。 我怎樣才能解決這個問題?

這是錯誤的回溯:

Traceback (most recent call last):

  File "<ipython-input-38-d7fe5c294c10>", line 1, in <module>
    runfile('C:/Users/mouna/ownCloud/Mouna Hammoudi/dumps/Python/RandomForestSimplified.py', wdir='C:/Users/mouna/ownCloud/Mouna Hammoudi/dumps/Python')

  File "C:\Users\mouna\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\mouna\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/mouna/ownCloud/Mouna Hammoudi/dumps/Python/RandomForestSimplified.py", line 43, in <module>
    X_train = sc.fit_transform(X_train)

  File "C:\Users\mouna\Anaconda3\lib\site-packages\sklearn\base.py", line 517, in fit_transform
    return self.fit(X, **fit_params).transform(X)

  File "C:\Users\mouna\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 590, in fit
    return self.partial_fit(X, y)

  File "C:\Users\mouna\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py", line 612, in partial_fit
    warn_on_dtype=True, estimator=self, dtype=FLOAT_DTYPES)

  File "C:\Users\mouna\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)

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

好的,當您查看代碼的 output (print(dataset.head())) 時,您會看到第一列“gold”,但這仍然是一個字符串。 發生這種情況是因為 pandas 使用第一列作為索引。

     gold  Program Requirement  MethodType  Top  Side  CallersT  CallersN  \
T     0        0       Inner           2    1     1         0         0
N     0        1       Inner           0    0     0         1         0
N     0        2       Inner           0    0     0         1         0
N     0        3        Root           0    0     0         1         0
N     0        4       Inner           0    0     0         1         0

   CallersU  CallersCallersT  CallersCallersN  CallersCallersU  CalleesT  \
T         1                0                0                1         0
N         0                1                0                0         1
N         0                1                0                0         1
N         0                1                0                0         1
N         0                1                0                0         1

   CalleesN  CalleesU  CalleesCalleesT  CalleesCalleesN  CalleesCalleesU
T         0         0                0                1               -1
N         0         0                0                1               -1
N         0         0                0                1               -1
N         0         0                0                1               -1
N         0         0                0                1               -1

解決方案:

dataset = pd.read_csv( 'dataExtended2.txt', sep= ',', index_col=False) 

那么 output 將是:

  gold  Program  Requirement  MethodType  Top  Side  CallersT  CallersN  \
0     1        0            1           0    2     1         1         0
1     0        0            2           0    0     0         0         1
2     0        0            3           0    0     0         0         1
3     0        0            4           3    0     0         0         1
4     0        0            5           0    0     0         0         1

   CallersU  CallersCallersT  CallersCallersN  CallersCallersU  CalleesT  \
0         0                1                0                0         1
1         0                0                1                0         0
2         0                0                1                0         0
3         0                0                1                0         0
4         0                0                1                0         0

   CalleesN  CalleesU  CalleesCalleesT  CalleesCalleesN  CalleesCalleesU
0         0         0                0                0                1
1         1         0                0                0                1
2         1         0                0                0                1
3         1         0                0                0                1
4         1         0                0                0                1

More details in the csv import docu of pandas https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM