Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a 2D numpy array to a 2D numpy matrix

I have a python code in which I have to convert a 2D array to a 2D matrix so that I can use it to calculate inverse.For that I am using numpy.matrix(array) but it is not working. Can anyone tell how to convert a 2D array to a numpy matrix? The array consists of all float numbers

like image 892
abcdxx Avatar asked Jul 03 '13 08:07

abcdxx


People also ask

How to convert 2D NumPy array to 1D array in Python?

1 Convert 2D Numpy array / Matrix to a 1D Numpy array using flatten () 2 Convert 2D Numpy array to 1D Numpy array using numpy.ravel () 3 Convert a 2D Numpy array to a 1D array using numpy.reshape () 4 numpy.reshape () and -1 size. 5 numpy.reshape () returns a new view object if possible. 6 Convert 2D Numpy array to 1D array but Column Wise.

How to flatten a 2D array into a 1D array in Python?

Flatten a 2d numpy array into 1d array in Python 1 With flatten. The flatten function in numpy is a direct way to convert the 2d array in to a 1D array. 2 Example 3 Output 4 With ravel. There is another function called ravel which will do a similar thing of flattening the 2D array into 1D. 5 Example 6 Output

How to create a 2D array without changing the original array?

Here, the 2D array is created of the 1D array. If you want to change the 2D array without changing the original array, just use the copy () function and the reshape () function.

What is NumPy in Python?

Python NumPy is the ultimate package in a python programming language that includes multidimensional array objects and a set of operations or routines to execute various operations on the array and process of the array.


2 Answers

If a is your array, np.asmatrix(a) is a matrix.

like image 194
Tobias Kienzler Avatar answered Oct 13 '22 11:10

Tobias Kienzler


If you have a list of lists (as you mentioned), you need to convert it first to a numpy array; see how to convert 2d list to 2d numpy array?

A short example is given here:

import numpy as np
a = [[  0. +0.j,   1.j,   2. -2.j],
     [  4. -4.j,   5. -5.j,   6. -1.j],
     [  8. -8.j,   9. -9.j,  10.]]
b = np.matrix(np.array(a))
b_inv = np.linalg.inv(b)
like image 2
strpeter Avatar answered Oct 13 '22 12:10

strpeter