You can use the item() function:

import numpy as np

matrix = np.array([[[[7]]]])
print(matrix.item())

Output

7
Answer from gtlambert on Stack Overflow
🌐
Quora
quora.com β€Ί Can-you-only-convert-an-array-of-size-1-to-a-Python-scalar
Can you only convert an array of size 1 to a Python scalar? - Quora
To convert an array of size 1 to a scalar in python, you can use the following code: [code]scalar = array[0] [/code]For any array, you have index access to each of the elements. Depending on the language, ...
Discussions

python - Numpy convert scalars to arrays - Stack Overflow
I am evaluating arbitrary expressions in terms of an x array, such as 3*x**2 + 4. This normally results in an array with x's shape. However if the expression is just a constant, it returns a scalar... More on stackoverflow.com
🌐 stackoverflow.com
python - How to convert neatly 1 size numpy array to a scalar? Numpy "asscalar" gives error when input is not a 1 size array. - Stack Overflow
I have this silly interest in how to avoid the following error in a smart way (possibly using the right numpy functions). In many ocasions I need to use the numpy where function to find a single i... More on stackoverflow.com
🌐 stackoverflow.com
What are numpy scalars used for?
Its just a misuse of terminology carried over from MATLAB. In MATLAB, everything is an array, including values you would usually think of as scalars: 1 == [ 1 ] "string" == [ "string" ] '' == char([]) "" == [ "" ] ~= string([]) Notice especially the last line. The empty string scalar "" is not the same thing as an empty array of strings string([]). NumPy copies this idea of everything being an array for vectorized operations and broadcasting purposes. Scalars are just arrays with a single element. More on reddit.com
🌐 r/learnpython
4
7
July 5, 2023
Convert 1*1 matrix/array to scalar
Being deprecated means it probably still works fine but will be changed in the future. You could probably ignore this warning or just change it to A.item() More on reddit.com
🌐 r/learnpython
2
1
September 18, 2020
🌐
Finxter
blog.finxter.com β€Ί 5-best-ways-to-convert-a-python-numpy-array-to-a-scalar-value
5 Best Ways to Convert a Python NumPy Array to a Scalar Value – Be on the Right Side of Change
February 20, 2024 - We first create a one-element NumPy array and then apply the np.asscalar() function. It returns the scalar value of the element within the one-element array. Another method involves the tolist() function, which converts the array into a nested list of Python scalars.
🌐
Codedamn
codedamn.com β€Ί news β€Ί python
Solving β€œOnly Size-1 Arrays Can Be Converted to Python Scalars” Error
June 30, 2023 - A: Yes, you can convert a single element from an array to a scalar by simply accessing that element with its index and converting it to a scalar. For further reading, you can refer to the official Python and numpy documentation to understand ...
🌐
NumPy
numpy.org β€Ί doc β€Ί stable β€Ί reference β€Ί arrays.scalars.html
Scalars β€” NumPy v2.4 Manual
However, array scalars are immutable, so none of the array scalar attributes are settable. The built-in scalar types are shown below. The C-like names are associated with character codes, which are shown in their descriptions. Use of the character codes, however, is discouraged. Some of the scalar types are essentially equivalent to fundamental Python types and therefore inherit from them as well as from the generic array scalar type:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί numpy-asscalar-in-python
numpy.asscalar() in Python | GeeksforGeeks
November 28, 2018 - Input array : [8] output scalar from input array : 8 Code #2 : ... # Python program explaining # numpy.asscalar() function import numpy as geek in_list = [2 ] # changing the list to size 1 array arr = geek.array(in_list) print ("Input array from list : ", arr) # changing the array to scalar scalar = geek.asscalar(arr) print ("output scalar from input list : ", scalar) Output :
🌐
SciPy
docs.scipy.org β€Ί doc β€Ί numpy-1.13.0 β€Ί reference β€Ί arrays.scalars.html
Scalars β€” NumPy v1.13 Manual
June 10, 2017 - Along with their (mostly) C-derived names, the integer, float, and complex data-types are also available using a bit-width convention so that an array of the right size can always be ensured (e.g. int8, float64, complex128). Two aliases (intp and uintp) pointing to the integer type that is sufficiently large to hold a C pointer are also provided. The C-like names are associated with character codes, which are shown in the table. Use of the character codes, however, is discouraged. Some of the scalar types are essentially equivalent to fundamental Python types and therefore inherit from them as well as from the generic array scalar type:
🌐
IncludeHelp
includehelp.com β€Ί python β€Ί how-to-convert-singleton-array-to-a-scalar-value.aspx
Python - How to convert singleton array to a scalar value?
To convert a singleton array into a scalar value, we can use the item() function over the array and it will return the corresponding scalar value.
🌐
ListenData
listendata.com β€Ί home β€Ί python
Only size-1 arrays can be converted to Python scalars
5 ways to solve error - Only size-1 arrays can be converted to Python scalars. The article includes explanation with example to solve this error.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί what are numpy scalars used for?
r/learnpython on Reddit: What are numpy scalars used for?
July 5, 2023 -

Am I correct in treating scalars as souped-up versions of 'standard' Python data types (+ some new data types not in base Python), which happen to have the same attributes and methods that ndarrays have, even though many of such attributes/methods are meaningless to scalars?

The one part that confuses me a lot on the document is the term "array scalars". What does the word array have to do with the scalar?

https://numpy.org/doc/stable/reference/arrays.scalars.html

Top answer
1 of 3
5
Its just a misuse of terminology carried over from MATLAB. In MATLAB, everything is an array, including values you would usually think of as scalars: 1 == [ 1 ] "string" == [ "string" ] '' == char([]) "" == [ "" ] ~= string([]) Notice especially the last line. The empty string scalar "" is not the same thing as an empty array of strings string([]). NumPy copies this idea of everything being an array for vectorized operations and broadcasting purposes. Scalars are just arrays with a single element.
2 of 3
2
The link is talking about datatypes. A scalar is a single value, in this context it is talking about a single datatype that is broadcast across an array hence the term array scalar. numpy has its own versions of builtins datatypes. Many of these inherit from the builtins datatype however some of the datatypes that are not inheritable such as bool or are not a fixed width int are essentially reimplemented in numpy. The following is essentially equivalent to bool: np.bool The following is essentially equivalent to int: np.int32 == np.int_ The following is essentially equivalent to float: np.float64 == np.float_ The following is essentially equivalent to complex: np.complex128 == np.complex_ The following is essentially equivalent to str: np.str_ The following is essentially equivalent to bytes: np.bytes_ The following is essentially equivalent to datetime.datetime: np.datetime64_ The following is essentially equivalent to datetime.timedelta: np.timedelta64_ The 32 bit (4 bytes), 64 bit (8 bytes) and 128 bit (16 bytes) are the number of bits occupied by the datatype. For the int there are numerous signed (_ and +) and unsigned (+ only) datatypes that usually use less memory but have a smaller dynamic range. They are used in some applications to conserve memory/be slightly more efficient. For example an integer array may be used to represent the brightness value of each pixel on a screen. Since the hardware of the screen only supports 256 values ranging from 0:256 (inclusive of 0 and exclusive of 256), using np.int32 by default will consume more memory than required and therefore using np.uint8 will be more appropriate. If numpy is imported: import numpy as np And an array is produced using: arr1 = np.array([1, 2, 3]) arr2 = np.array([1., 2, 3]) arr3 = np.array([1, 2, 3], dtype=np.float64) arr4 = np.array([1, 2, 3], dtype=float) The datatype will be automatically be determined from the data in the list unless explicitly implied: arr1.dtype arr2.dtype arr3.dtype arr4.dtype will return 'int32','float64','float64' and 'float64' respectively. Notice when the builtins float is specified that the numpy equivalent np.float64 is selected.
🌐
Statology
statology.org β€Ί home β€Ί how to fix: only size-1 arrays can be converted to python scalars
How to Fix: Only size-1 arrays can be converted to Python scalars
November 10, 2021 - However, this function only accepts a single value instead of an array of values. Instead, you should use x.astype(int) to convert a NumPy array of float values to an array of integer values because this function is able to accept an array.
🌐
Studyopedia
studyopedia.com β€Ί home β€Ί scalar operations on numpy arrays
Scalar operations on Numpy arrays - Studyopedia
October 18, 2023 - Scalar operations on Numpy arrays, include performing addition or subtraction, or multiplication on each element of a Numpy array.
🌐
Finxter
blog.finxter.com β€Ί 5-best-ways-to-convert-a-numpy-array-to-a-scalar-in-python
5 Best Ways to Convert a NumPy Array to a Scalar in Python – Be on the Right Side of Change
February 20, 2024 - By indexing with array[0], we obtain ... with array slices or specific elements from multi-dimensional arrays. The asscalar() NumPy method can be used to convert a one-element NumPy array into a scalar....
🌐
SciPy
docs.scipy.org β€Ί doc β€Ί numpy-1.14.1 β€Ί reference β€Ί arrays.scalars.html
Scalars β€” NumPy v1.14 Manual
April 16, 2018 - Along with their (mostly) C-derived names, the integer, float, and complex data-types are also available using a bit-width convention so that an array of the right size can always be ensured (e.g. int8, float64, complex128). Two aliases (intp and uintp) pointing to the integer type that is sufficiently large to hold a C pointer are also provided. The C-like names are associated with character codes, which are shown in the table. Use of the character codes, however, is discouraged. Some of the scalar types are essentially equivalent to fundamental Python types and therefore inherit from them as well as from the generic array scalar type:
🌐
NumPy
numpy.org β€Ί devdocs β€Ί reference β€Ί generated β€Ί numpy.ndarray.tolist.html
numpy.ndarray.tolist β€” NumPy v2.5.dev0 Manual
If a.ndim is 0, then since the depth of the nested list is 0, it will not be a list at all, but a simple Python scalar. ... The possibly nested list of array elements. ... The array may be recreated via a = np.array(a.tolist()), although this may sometimes lose precision.