【问题标题】:Usage of onehotencoderonehotencoder 的使用
【发布时间】:2016-02-24 09:04:11
【问题描述】:

我是 python 新手。我以前只有 VBA 代码。最近开始使用python进行数据挖掘,但是一直在使用python遇到问题

我在使用 onehotencoder 正确转换我的类别特征时遇到问题,这是我的代码

from __future__ import print_function
import os import subprocess from sklearn.preprocessing import OneHotEncoder
from sklearn import preprocessing import csv    
import pandas as pd import numpy as np
from sklearn.tree import DecisionTreeClassifier, export_graphviz     

datapoint = []    
with open('raw2.csv',  'rb') as csvfile:    
    spamreader = csv.reader(csvfile, delimiter=',')    
    for row in spamreader: # Reading each row    
        data_point = []    
        for column in row: # Reading each column of the row    
            data_point.append((column))    
        datapoint.append(data_point)    
datapoint = np.array(datapoint)   

print(datapoint)
enc = preprocessing.OneHotEncoder()
enc.fit(datapoint)
enc.transform(datapoint).toarray()

features = list(df.columns[1:8])
print("* features:", features, sep="\n")
"#fit the decision tree"
y = df[,0]
X = df[features]
dt = DecisionTreeClassifier(min_samples_split=5, random_state=51)
dt.fit(X, y)

""produce graphic visualization""
def visualize_tree(tree, feature_names):
    """Create tree png using graphviz.

    Args
    ----
    tree -- scikit-learn DecsisionTree.
    feature_names -- list of feature names.
    """
    with open("dt.dot", 'w') as f:
        export_graphviz(tree, out_file=f,
                        feature_names=feature_names)

    command = ["dot", "-Tpng", "dt.dot", "-o", "dt.png"]
    try:
        subprocess.check_call(command)
    except:
        exit("Could not run dot, ie graphviz, to "
             "produce visualization")

visualize_tree(dt, features)        

这是我的第一个数据集的样本

['Tobermory' 'Car' '2-3hr' 'Fall' '<$100' '3 days' 'Male' '18 - 23'] 

这是我遇到的错误

ValueError                                Traceback (most recent call
last) <ipython-input-13-0bb2597d0276> in <module>()        
      25 enc = preprocessing.OneHotEncoder()    
 ---> 26 enc.fit(datapoint)    
      27 enc.transform(datapoint).toarray()    

ValueError: invalid literal for int() with base 10: 'Tobermory'    

【问题讨论】:

  • 在某些时候,您试图将非整数值视为整数。尝试将您的代码缩小到发生这种情况的特定行,并使用该信息更新您的问题

标签: python numpy tree encoder


【解决方案1】:

我相信您正在寻找sklearn.preprocessing.LabelBinarizerOneHotEncoder 接受一个整数并从中创建虚拟变量。

http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelBinarizer.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 2017-08-10
    • 2021-08-31
    • 2020-01-25
    • 2016-03-14
    • 2020-07-07
    • 2021-11-01
    相关资源
    最近更新 更多