A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/different-ways-to-create-numpy-arrays-in-python/ below:

Different Ways to Create Numpy Arrays in Python

Different Ways to Create Numpy Arrays in Python

Last Updated : 03 Apr, 2024

Creating NumPy arrays is a fundamental aspect of working with numerical data in Python. NumPy provides various methods to create arrays efficiently, catering to different needs and scenarios. In this article, we will see how we can create NumPy arrays using different ways and methods.

Below are some of the ways by which we can create NumPy Arrays in Python:

Create Numpy Arrays Using Lists or Tuples

The simplest way to create a NumPy array is by passing a Python list or tuple to the numpy.array() function. This method creates a one-dimensional array.

Python3
import numpy as np

my_list = [1, 2, 3, 4, 5]
numpy_array = np.array(my_list)
print("Simple NumPy Array:",numpy_array)
Initialize a Python NumPy Array Using Special Functions

NumPy provides several built-in functions to generate arrays with specific properties.

Python3
import numpy as np

zeros_array = np.zeros((2, 3))
ones_array = np.ones((3, 3))
constant_array = np.full((2, 2), 7)
range_array = np.arange(0, 10, 2)  # start, stop, step
linspace_array = np.linspace(0, 1, 5)  # start, stop, num

print("Zero Array:","\n",zeros_array)
print("Ones Array:","\n",ones_array)
print("Constant Array:","\n",constant_array)
print("Range Array:","\n",range_array)
print("Linspace Array:","\n",linspace_array)

Output
Zero Array 
 [[0. 0. 0.]
 [0. 0. 0.]]
Zero Array 
 [[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
Constant Array 
 [[7 7]
 [7 7]]
Range Array 
 [0 2 4 6 8]
Linspace Array 
 [0.   0.25 0.5  0.75 1.  ]
Create Python Numpy Arrays Using Random Number Generation

NumPy provides functions to create arrays filled with random numbers.

Python3
import numpy as np

random_array = np.random.rand(2, 3)
normal_array = np.random.randn(2, 2)
randint_array = np.random.randint(1, 10, size=(2, 3))  

print(random_array)
print(normal_array)
print(randint_array)

Output
[[0.87948864 0.55022063 0.29237533]
 [0.99475413 0.76666244 0.55240304]]
[[ 1.77971899  0.67837749]
 [ 0.33101208 -1.04029635]]
[[6 6 3]
 [8 5 8]]
Create Python Numpy Arrays Using Matrix Creation Routines

NumPy provides functions to create specific types of matrices.

Python3
import numpy as np

identity_matrix = np.eye(3)
diagonal_array = np.diag([1, 2, 3])
zeros_like_array = np.zeros_like(diagonal_array)
ones_like_array = np.ones_like(diagonal_array)

print(identity_matrix)
print(diagonal_array)
print(zeros_like_array)
print(ones_like_array)

Output
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[[1 0 0]
 [0 2 0]
 [0 0 3]]
[[0 0 0]
 [0 0 0]
 [0 0 0]]
[[1 1 1]
 [1 1 1]
 [1 1 1]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4