Scikit Learn - Identifying target from loading a CSV

2.8k Views Asked by At

I'm loading a csv, using Numpy, as a dataset to create a decision tree model in Python. using the below extract places columns 0-7 in X and the last column as the target in Y.

#load and set data
data = np.loadtxt("data/tmp.csv", delimiter=",")
X = data[:,0:7] #identify columns as data sets
Y = data[:,8] #identfy last column as target

#create model
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)

What i'd like to know is if its possible to have the classifier in any column. for example if its in the fourth column would the following code still fit the model correctly or would it produce errors when it comes to predicting?

#load and set data
data = np.loadtxt("data/tmp.csv", delimiter=",")
X = data[:,0:8] #identify columns as data sets
Y = data[:,3] #identfy fourth column as target

#create model
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)
2

There are 2 best solutions below

3
On

If you have >4 columns, and the 4th one is the target and the others are features, here's one way (out of many) to load them:

# load data

X = np.hstack([data[:, :3], data[:, 5:]]) # features
Y = data[:,4] # target

# process X & Y

(with belated thanks to @omerbp for reminding me hstack takes a tuple/list, not naked arguments!)

0
On

First of all, As suggested by @mescalinum in a comment to the question, think of this situation:

.... 4th_feature ...    label
....      1      ...      1
....      0      ...      0
....      1      ...      1
............................

In this example, the classifier (any classifier, not DecisionTreeClassifier particularly) will learn that the 4th feature can best predict the label, since the 4th feature is the label. Unfortunately, this issue happen a lot (by accident I mean).

Secondly, if you want the 4th feature to be input label, you can just swap the columns:

arr[:,[frm, to]] = arr[:,[to, frm]]

@Ahemed Fasih's answer can also do the trick, however its around 10 time slower:

import timeit


setup_code = """
import numpy as np
i, j = 400000, 200
my_array = np.arange(i*j).reshape(i, j)
"""

swap_cols = """
def swap_cols(arr, frm, to):
    arr[:,[frm, to]] = arr[:,[to, frm]]
"""

stack ="np.hstack([my_array[:, :3], my_array[:, 5:]])"
swap ="swap_cols(my_array, 4, 8)"

print "hstack - total time:", min(timeit.repeat(stmt=stack,setup=setup_code,number=20,repeat=3))
#hstack - total time: 3.29988478635
print "swap - total time:", min(timeit.repeat(stmt=swap,setup=setup_code+swap_cols,number=20,repeat=3))
#swap - total time: 0.372791106328