search
Search
Map of Data Science
Guest 0reps
exit_to_appLog out
Map of data science
Thanks for the thanks!
close
account_circle
Profile
exit_to_app
Sign out
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview Doc Search Code Search Beta SORRY NOTHING FOUND!
mic
Start speaking... Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
A
A
brightness_medium
share
arrow_backShare Twitter Facebook

# NumPy | asmatrix method

NumPy
chevron_right
Documentation
schedule Jul 1, 2022
Last updated
local_offer PythonNumPy
Tags
expand_more
map
Check out the interactive map of data science

NumPy's `asmatrix(~)` method constructs a matrix from a sequence of data (e.g. arrays and tuples).

# Parameters

1. `a` | `array-like`

The sequence of data used to construct a matrix.

NOTE

If `a` is a NumPy array or matrix, no copying would be done - modifying the result of `asmatrix(~)` would also automatically modify `a`.

2. `dtype` | `string` or `type` | `optional`

Type of data stored in the NumPy array. By default, the type will be inferred.

A NumPy matrix.

# Examples

## Basic usage

To create a 2 by 2 matrix:

``` np.asmatrix([[4,5],[6,7]]) matrix([[4, 5], [6, 7]]) ```

## Case when array is not copied

Suppose we create a NumPy matrix out of a NumPy array, like so:

``` a = np.array([[4,5],[6,7]])b = np.asmatrix(a) ```

When we modify the contents of `b`:

``` b[0,0] = 9b matrix([[9, 5], [6, 7]]) ```

This would automatically modify `a` as well:

``` a array([[9, 5], [6, 7]]) ```

What's happening here is that the NumPy objects `a` and `b` actually share the same memory space, so modifying one would involve modifying the other. Note that this happens only if we try to call `asmatrix(~)` on a NumPy array or matrix.