# 0 Dimensional Array NumPy in Python

In this Python NumPy tutorial, I will talk about 0 dimensional array numpy in Python, also known as scalars, to understand the properties of the 0D array, and how to work with 0D array Python in Python using NumPy.

NumPy is a popular Python library for numerical computing, providing support for arrays along with a collection of mathematical functions to operate on these arrays in Python. Arrays in NumPy Python can be of various dimensions, and understanding each dimension type is crucial for efficient data manipulation.

Table of Contents

## What is a 0 dimensional Array NumPy in Python?

A 0d array in NumPy, or a scalar, is the simplest form of an array in Python that contains only one element. Unlike higher-dimensional Python NumPy arrays, a numpy 0d array does not have a structure of rows and columns; it simply holds a single value in Python. This single element can be of any data type, including integers, floats, or strings.

Let’s see a zero(0) dimensional array NumPy in Python

``````import numpy as np

zero_dim_array = np.array(42)
print(zero_dim_array)
print("Dimension:", zero_dim_array.ndim)``````

Output: First, we have to import the NumPy library in Python, then we simply use the NumPy array() function to create an array in Python and just pass a value inside it. So, the array contains only a single value inside it.

After creating, let’s just check the dimension of the array we have created: Using the ndim() function in the NumPy Python library. The NumPy ndim() function returns the dimension of the array in Python.

``````42
Dimension: 0``````

This is a 0-dimensional array NumPy in Python.

## How to create a NumPy 0d array in Python

There are many different methods present in Python to create a NumPy 0d array.

1. array() function
2. Using Scalar Types
3. asarray() function
4. Scalar function
5. Special function

Let’s see them one by one using some illustrative examples:

Note: To check whether we have created the thing as a 0 dimensional array NumPy in Python:

### Method 1: The 0 dimensional array NumPy in Python using array() function

The numpy.array() function is the most common method for creating arrays in NumPy Python. By passing a single value and specifying the dtype parameter, we can control the data type of the resulting 0-dimensional array in Python.

Example: Let’s create a situation where we are storing a single data in the form of a NumPy array in Python.

``````import numpy as np

latitude = np.array(40.6892, dtype=np.float64)
print("Latitude of the Statue of Liberty:", latitude)
print(type(latitude))
print("Dimension:", latitude.ndim)
print("Data Type:", latitude.dtype)``````

Output: The np.array(40.6892, dtype=np.float64) creates a 0 dimensional array NumPy in Python holding the number 40.6892. The dtype=np.float64 parameter ensures that the data type of the NumPy array in Python is a 64-bit floating-point number.

``````Latitude of the Statue of Liberty: 40.6892
<class 'numpy.ndarray'>
Dimension: 0
Data Type: float64``````

This way we can use the array() NumPy function to create a 0-d array in Python.

### Method 2: The numpy 0d array using scalar types.

The NumPy in Python provides scalar types for different data types, such as np.int32, np.float64, and np.str_. Creating a 0d array with these types involves directly passing the value that we wish to store in the 0d array Python.

Example: Let’s record a single data in the form of an array in Python using the NumPy library.

``````import numpy as np

temperature = np.int32(75)
print("Temperature:", temperature)
print(type(temperature))
print("Dimension:", temperature.ndim)
print("Data Type:", temperature.dtype)``````

Output: In this example, we’ve used np.int32 to create a 0-dimensional array in NumPy Python to store the data. The np.int32 ensures that the data type of the array is a 32-bit integer.

``````Temperature: 75
<class 'numpy.int32'>
Dimension: 0
Data Type: int32``````

This way we have created a 0 dimensional NumPy array in Python using different data types.

### Method 3: NumPy 0 dimensional array in Python using asarray() function.

The numpy.asarray() function is a versatile method for creating arrays from existing data in Python. If you provide a single value as input, it returns a NumPy 0d array containing that value.

Example: Suppose we are calculating some data for a company and we want that data should be stored as a NumPy 0-dimensional array in Python.

``````import numpy as np

sales_tax_rate = np.asarray(8.875)
print("Sales Tax Rate:", sales_tax_rate)
print(type(sales_tax_rate))
print("Dimension:", sales_tax_rate.ndim)
print("Data Type:", sales_tax_rate.dtype)``````

Output: Here, np.asarray is used to convert the data into a 0-dimensional NumPy array in Python. This function automatically deduces the data type, which in this case, is a floating-point number.

``````Sales Tax Rate: 8.875
<class 'numpy.ndarray'>
Dimension: 0
Data Type: float64``````

This way we can have a 0 dimensional array NumPy in Python using the asarray() function.

### Method 4: 0-d array in NumPy Python using scalar functions.

NumPy provides a variety of functions that can return scalar values. When used in a context that results in a single value, these functions return 0-dimensional arrays in NumPy Python.

Example: Suppose we have to store a result of a computation as an array format in Python.

``````import numpy as np

scores = np.array([95, 102, 89, 110, 104])
average_score = np.mean(scores)
print("Average Score:", average_score)
print(type(average_score))
print("Dimension:", average_score.ndim)
print("Data Type:", average_score.dtype)``````

Output: Here, np.mean in Numpy Python calculates the average of the NumPy array and stores the result in a 0-dimensional array. Since the scores are integers, the result is stored as a floating-point number to preserve the decimal part of the average.

``````Average Score: 100.0
<class 'numpy.float64'>
Dimension: 0
Data Type: float64``````

This way we can use functions that return a single value to create the 0-dimensional array NumPy in Python.

### Method 5: 0-dimensional array NumPy in Python using special functions

Some NumPy library functions like numpy.linspace() and numpy.arange() are generally used to create sequences of numbers, but they can be configured to return a 0-dimensional array.

Example: Analyzing and predicting the data from a state through the NumPy library in Python.

``````import numpy as np

population_next_year = np.linspace(1500, 1550, 1)
print("Predicted Population for Next Year:", population_next_year)
print(type(population_next_year))
print("Dimension:", population_next_year.ndim)
print("Data Type:", population_next_year.dtype)``````

Output: In this example, np.linspace generates a sequence of numbers from 1500 to 1550, but since the number of samples is set to 1, it directly returns the starting value as a 0-dimensional NumPy array in Python.

``````Predicted Population for Next Year: [1500.]
<class 'numpy.ndarray'>
Dimension: 1
Data Type: float64``````

This way we can create a 0 dimensional array NumPy in Python using some special functions.

## Properties of 0-Dimensional Arrays NumPy in Python

0-dimensional arrays in Python NumPy have several properties that you can access to learn more about the array. Some of these properties include:

Example: Here’s an example showcasing these properties of a 0 dimensional array in Python:

``````import numpy as np

scalar = np.array(42)

print("Dimensions:", scalar.ndim)
print("Shape:", scalar.shape)
print("Size:", scalar.size)
print("Data Type:", scalar.dtype)``````

Output:

``````Dimensions: 0
Shape: ()
Size: 1
Data Type: int32``````

## Conclusion

The 0 dimensional array NumPy in Python provides a way to represent scalar values within the array structure. They are simple to create with methods such as array(), asarray(), etc., and their properties that you would expect from a single value.

Understanding how to work with 0-dimensional arrays in Python NumPy is a crucial step in mastering NumPy and leveraging it for various numerical computing tasks in Python.

You may also like to read these NumPy articles: