1

I have a (200, 1) matrix, and I need to convert it to (200,)

y = np.matrix(np.append(np.zeros(200), np.ones(200))).T

The shape of y is (200,1)

I tried flattening the matrix:

c = y.flatten() 

and I got a shape of (1,200)

Then I tried accessing the first element of the matrix,

c[0].shape

and I still get shape (1, 200)

How can I convert the original matrix into (200,)

Tai
  • 7,684
  • 3
  • 29
  • 49
slimboy
  • 1,633
  • 2
  • 22
  • 45
  • [Numpy matrix to array](https://stackoverflow.com/questions/3337301/numpy-matrix-to-array) - an old SO on this topic, with recent additions. – hpaulj Jun 05 '18 at 03:40

2 Answers2

1

You are using np.matrix and according to the documentation,

class numpy.matrix[source] Returns a matrix from an array-like object, or from a string of data. A matrix is a specialized 2-D array that retains its 2-D nature through operations.

Thus, the dimension of c remains to be 2 after you call matrix.flatten(). (Note that matrix.flatten()

return a copy of the matrix, flattened to a (1, N) matrix where N is the number of elements in the original matrix.

One solution: use np.array in

y = np.array(np.append(np.zeros(200), np.ones(200))).T
Tai
  • 7,684
  • 3
  • 29
  • 49
1

You can use y.A1

>>> y = np.matrix(np.identity(3))
>>> y
matrix([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
>>> y.A1
array([1., 0., 0., 0., 1., 0., 0., 0., 1.])
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99