- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Interpret the input as a matrix and display a different type for the output in Numpy
To Interpret the input as a matrix, use the numpy.asmatrix() method in Python Numpy. The dtype parameter is used to set the type of the output array. Unlike matrix, asmatrix does not make a copy if the input is already a matrix or an ndarray. Equivalent to matrix(data, copy=False).
NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.
Steps
At first, import the required library −
import numpy as np
Create a 2d array −
arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])
Displaying our array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Array...
",arr.size)
To Interpret the input as a matrix, use the numpy.asmatrix() method. The dtype parameter is used to set the type of the output array −
res = np.asmatrix(arr, dtype = float) print("
Result...
",res)
Example
import numpy as np # Create a 2d array arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69], [69, 80, 80, 99]]) # Displaying our array print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size) # To Interpret the input as a matrix, use the numpy.asmatrix() method in Python Numpy # The dtype parameter is used to set the type of the output array res = np.asmatrix(arr, dtype = float) print("
Result...
",res)
Output
Array... [[36 36 78 88] [92 81 98 45] [22 67 54 69] [69 80 80 99]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 4) Elements in the Array... 16 Result... [[36. 36. 78. 88.] [92. 81. 98. 45.] [22. 67. 54. 69.] [69. 80. 80. 99.]]
- Related Articles
- Interpret the input as a matrix in Numpy
- Add arguments element-wise and display the result in a different type in Numpy
- Subtract arguments element-wise and display the result in a different type in Numpy
- Divide arguments element-wise and display the result in a different type in Numpy
- True Divide arguments element-wise and display the result in a different type in Numpy
- Generate a Vandermonde matrix and set the number of columns in the output in Numpy
- Return a new array of given shape filled with a fill value and a different output type in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere with a different output type in Numpy
- Power array elements of an array with a given value and display the result in a different type in Numpy
- How can Keras be used to plot the model as a graph and display input and output shapes using Python?
- Get a matrix as Output if a condition for a single value is met in R.
- Convert the input to a masked array of the given data-type in Numpy
- What are the input and output for strings in C language?
- Return an array of zeroes with the same shape as a given array but with a different type in Numpy
- Create a two-dimensional array with the flattened input as a diagonal in Numpy
