I am working on a data science project on chemistry. My main expertise is in chemistry, and this is my first time working with statistical learning/machine learning. I am trying to test various different methods to see which one works. In my case, the input is a 2D matrix like the one shown below. And the output has to be a real number (it will be in the range -30 to +30).

$$\text{input =}\begin{bmatrix} 2.56&1&2&0 \\ 0&1.98&1&1 \\ 1&0&3.47&2 \\ 0&1&0&1.67 \end{bmatrix} \,\,\text{ output = }\mathrm{-3.7}$$

I have a dataset, with a 2D matrix input for each point, and the corresponding target output value for that point. I am supposed to train a learning model (regression or something similar) on the dataset. I am mainly considering regression models from scikit-learn, but I am open to using other packages (and other learning models such as CNN's).

The problem is that I don't know how to feed the 2D matrix into the learning model. The regression models assume a 1D array input for each datapoint. I could flatten the 2D array into 1D as suggested in some answers on this site, but the adjacency of elements is one of the most important pieces of information that I think will be lost when I flatten it. (What I mean is that there is important information in the closeness of two different elements in the matrix)

What are some of the good ways to handle a 2D matrix input for a machine learning model?

The technical details of what the numbers in matrix mean, are not important for my question. I am trying to understand the general methods of dealing with a 2D input for a statistical model.


1 Answer 1


If you go for a CNN (with only one "color" channel), which sounds like a good idea, you don't need to do much except glueing multiple observations together across an additional dimension. So for training a CNN with Keras, the input would be a n × 4 x 4 x 1 array and the output a one-dimensional vector of length n.

For less flexible modeling techniques like a random forest, an old trick with complex input is to derive a couple of meaningful features and use these as input:

  • largest value on diagonal
  • mean value on diagonal
  • standard deviation on diagonal
  • largest value off diagonal
  • ...

This is an alternative to just flatten the numbers. However, if your sample is very large, then flattening is not a bad approach either.

  • $\begingroup$ Thanks! Could you please explain what the 'one colour channel' CNN means? I have never heard that before. Also, do you think it is reasonable to diagonalize the 2D matrix and then take the diagonal values as input (I have seen this in a couple of research papers)? $\endgroup$
    – S R Maiti
    Jan 1 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.