Search for:
header addddddd code
Numpy Articles

Numpy Create Array

Pinterest LinkedIn Tumblr

Numpy Create Array

NumPy, short for Numerical Python, is a fundamental package for scientific computing in Python. It provides support for arrays, matrices, and a host of mathematical functions to operate on these arrays. One of the first steps in using NumPy is creating arrays. This comprehensive guide will delve into various methods to create arrays in NumPy, providing detailed explanations and examples.

1. Introduction to NumPy Arrays

NumPy arrays are a grid of values, all of the same type, indexed by a tuple of non-negative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

2. Creating Arrays from Lists

A common way to create arrays is from Python lists using the numpy.array() function. This function can take a list and convert it into a NumPy array.

Example Code 1:

import numpy as np

# Creating a NumPy array from a list
list_data = [1, 2, 3, 4, 5]
array_data = np.array(list_data)

print(array_data, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

In this example, a Python list list_data containing integers from 1 to 5 is converted into a NumPy array using np.array(). The print statement will display the newly created array.

3. Using numpy.array()

The numpy.array() function is versatile and can also create arrays from nested lists to generate multi-dimensional arrays.

Example Code 2:

import numpy as np

# Creating a 2D NumPy array from a list of lists
nested_list_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
array_data_2d = np.array(nested_list_data)

print(array_data_2d, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

Here, nested_list_data is a list of lists, effectively a 2D array. Using np.array(), we convert it into a 2D NumPy array. The print statement outputs the 2D array.

4. Creating Arrays with numpy.zeros()

The numpy.zeros() function creates an array filled with zeros. The shape of the array is defined by the given arguments.

Example Code 3:

import numpy as np

# Creating a 1D array of zeros
zeros_array_1d = np.zeros(5)

print(zeros_array_1d, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

This example creates a 1D array of five zeros using np.zeros(). The argument 5 specifies the number of elements in the array. The print statement displays the array of zeros.

5. Creating Arrays with numpy.ones()

The numpy.ones() function is used to create an array filled with ones. Similar to numpy.zeros(), the shape is defined by the provided arguments.

Example Code 4:

import numpy as np

# Creating a 2D array of ones
ones_array_2d = np.ones((3, 4))

print(ones_array_2d, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

Here, np.ones() is used to create a 3×4 2D array filled with ones. The argument (3, 4) specifies the shape of the array. The print statement shows the 2D array of ones.

6. Creating Arrays with numpy.full()

The numpy.full() function creates an array filled with a specified value.

Example Code 5:

import numpy as np

# Creating a 3D array filled with a specified value
full_array_3d = np.full((2, 3, 4), 7)

print(full_array_3d, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

In this example, np.full() is used to create a 2x3x4 3D array filled with the value 7. The argument (2, 3, 4) defines the shape, and 7 is the fill value. The print statement outputs the 3D array.

7. Creating Arrays with numpy.arange()

The numpy.arange() function is similar to Python’s built-in range() but returns a NumPy array.

Example Code 6:

import numpy as np

# Creating an array using arange
arange_array = np.arange(10, 21, 2)

print(arange_array, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

This example uses np.arange() to create an array of integers from 10 to 20 (exclusive) with a step of 2. The print statement shows the array [10, 12, 14, 16, 18, 20].

8. Creating Arrays with numpy.linspace()

The numpy.linspace() function generates an array of evenly spaced numbers over a specified interval.

Example Code 7:

import numpy as np

# Creating an array with linspace
linspace_array = np.linspace(0, 1, 5)

print(linspace_array, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

Here, np.linspace() generates an array of 5 numbers evenly spaced between 0 and 1. The print statement outputs the array [0. , 0.25, 0.5 , 0.75, 1. ].

9. Creating Arrays with numpy.eye()

The numpy.eye() function creates a 2D array with ones on the diagonal and zeros elsewhere, which is known as an identity matrix.

Example Code 8:

import numpy as np

# Creating an identity matrix
identity_matrix = np.eye(4)

print(identity_matrix, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

This example uses np.eye() to create a 4×4 identity matrix. The print statement shows the matrix with ones on the diagonal.

10. Creating Arrays with numpy.random.rand()

The numpy.random.rand() function generates an array of given shape with random samples from a uniform distribution over [0, 1).

Example Code 9:

import numpy as np

# Creating an array with random values
random_array = np.random.rand(3, 3)

print(random_array, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

Here, np.random.rand() creates a 3×3 array with random values between 0 and 1. The print statement outputs the array with random values.

11. Creating Arrays with numpy.random.randn()

The numpy.random.randn() function returns a sample (or samples) from the “standard normal” distribution.

Example Code 10:

import numpy as np

# Creating an array with random values from normal distribution
randn_array = np.random.randn(3, 3)

print(randn_array, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

This example uses np.random.randn() to create a 3×3 array with values from a standard normal distribution. The print statement displays the array with normally distributed random values.

12. Creating Arrays with numpy.random.randint()

The numpy.random.randint() function returns random integers from the “discrete uniform” distribution of the specified dtype.

Example Code 11:

import numpy as np

# Creating an array with random integers
randint_array = np.random.randint(1, 100, (4, 4))

print(randint_array, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

Here, np.random.randint() generates a 4×4 array with random integers between 1 and 99. The print statement shows the array with random integers.

13. Creating Arrays from Existing Data

NumPy allows creating arrays from existing data such as other arrays, lists, or even data files.

Example Code 12:

import numpy as np

# Creating an array from an existing array
existing_array = np.array([1, 2, 3, 4])
new_array_from_existing = np.array(existing_array)

print(new_array_from_existing, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

This example demonstrates creating a new array from an existing array existing_array using np.array(). The print statement outputs the new array.

14. Advanced Array Creation Techniques

NumPy also offers advanced techniques for array creation, such as from buffers, from functions, and from iterables.

Example Code 13:

import numpy as np

# Creating an array from a function
def func(i, j):
    return i + j

function_array = np.fromfunction(func, (3, 3), dtype=int)

print(function_array, " - Created using numpyarray.com")

Output:

Numpy Create Array

Explanation:

This example uses np.fromfunction() to create a 3×3 array where each element is the sum of its indices. The print statement displays the array created from the function.

15. Numpy Create Array Conclusion

Creating arrays is the first step in harnessing the power of NumPy. Whether starting from lists, generating arrays of zeros or ones, or using advanced techniques, NumPy provides a flexible and powerful toolkit for array creation. The examples and explanations provided should offer a thorough understanding of various array creation methods in NumPy.

Write A Comment

Pin It