2014-07-14 36 views
0

爲char我有以下的代碼實現的scikit學習的決策樹分類:轉換整數Python中

import numpy as np 
import pandas as pd 
from sklearn import tree 

# #--------------------------------------------------------------------------------------------------- 
with open('data/training.csv', 'r') as f: 

    df = pd.read_csv(f, index_col=None) 

Subset = df.iloc[:, 32:33] # Just the labels 
df['Num_Labels'] = df.Label.map(lambda x: '-1' if x == 's' else '1') # Convert labels to '0' or '1'. 

Z = df.iloc[:, 32:34] # the letter labels & numerical labels 
Train_values = df.iloc[:, 1:31].values 
Train_labels = df.iloc[:, 33:34].values 

with open('data/test.csv', 'r') as f2: 

    df2 = pd.read_csv(f2, index_col=None) 

Test_values = df2.iloc[:, 1:31].values 

# #---------------------------------------------------------------------------------------------- 

X = Train_values 
Y = Train_labels.astype(np.float) 

print X.dtype 
print Y.dtype 

clf = tree.DecisionTreeClassifier() 
clf = clf.fit(X, Y) 

Pred = clf.predict(Test_values) 

print Pred.dtype 

Out = Pred.astype(np.float) 
np.savetxt('Output_Numerical.csv', Out, delimiter=' ') 

直到此時的代碼按預期工作。然後,我想將標籤轉換回原來的字符值's'和'h'。我寫了以下內容:

Out2 = Pred.astype(str) # Initialize 

print "Out2's type is:" 
print Out2.dtype 

for i in range(0, len(Out)): 
    if Out[i] == -1: 
     Out2[i] == 's' 
    else: 
     Out2[i] == 'h' 

print Out2 

但它不會更改Out2的值。

+0

我對Sci-kit學習不是很熟悉,但是您是否嘗試過Python的chr()函數? –

+0

@MikeDriscoll:最有可能的'Out2'將是一個合適類型的'ndarray'(長度爲12或者什麼的字符串),所以即使它們看起來很奇怪,操作也很好。 – DrV

回答

3

這是相當簡單的,即使錯誤是不是你想:

for i in range(0, len(Out)): 
    if Out[i] == -1: 
     Out2[i] == 's' 
    else: 
     Out2[i] == 'h' 

使用單=,而不是在最後兩次==!現在發生的是Out2[1] == 's'等於False,這是沒有人有興趣使用的。所以它不是一個非法的構造,口譯員沒有理由抱怨它。

+0

謝謝,我認爲這件事很簡單 – user3708902