create 1-D numpy.ndarray instead of 2 dimensional

Viewed 60

I am trying to do a classification using Python. I have some input columns (let k variables) and one output column.

Let inputfeatures 
  = array([[ 0, 0,  0, ...,  0,  0,  0],
           [ 1, 0,  0, ...,  0,  0,  0],
           [ 1, 0,  0, ...,  0,  0,  0],
           ...,
           [ 0, 0,  0, ...,  0,  0,  0],
           [ 1, 0,  0, ...,  0,  0,  0],
           [ 0, 0,  0, ...,  0,  1,  0]], dtype=int64)
target_array
= array([0, 0, 0, ..., 0, 0, 1], dtype=int64)

I am fitting the model as,

trainedModel = model.fit(inputfeatures,target_array)

The classifier I am using needs Data Phenotypes for all data instances are represented by a 1 dimensional numpy.ndarray of length n.

I am getting no errors while fitting, since the target_array is 1 dimensional numpy.ndarray of length n.

Suppose, I need to have two output variables, how can I create a 1 dimensional numpy.ndarray of length n to fit the model?

I just tried as given below:

target_array=data[data.columns[data.columns.isin(['var1', 'var2'])]].values
target_array
array([[1, 1],
       [0, 1],
       [1, 1],
       ...,
       [1, 0],
       [0, 0],
       [0, 0]], dtype=int64)

But it is 2 dimensional. How to make it 1 dimensional numpy.ndarray of length n?

1 Answers

You can't simply transform an n-dimensional label (or "target output") into a 1-dimensional one. In some cases where the output distribution is an m-dimensional manifold embedded into an n-dimensional space, you may try to do a projection first (including, if necessary, a non-linear projection), but you have to think very carefully about what you want your classifier or your regressor to learn.

One simple strategy is to have a dedicated learner per output label. This will "work" in the mechanical sense that you'll be able to forecast an output for any input that is consistent with the input distribution. But it will ignore the possible interactions between the output variables. Imagine an 2D output distribution like this:

Two learners, each seeing only one of the output variables, won't have a chance to learn about that output structure and will likely (and wrongly) forecast some output points in the positive quadrant that should be empty.

One strategy is to learn a first variable y_0 from your input X. Then learn a second variable y_1 after augmenting your input X with the forecast y_0_hat from the first classifier. And so on.

Generally speaking, check out:

Related