Different Ways to Create Numpy Arrays in Python Last Updated : 03 Apr, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Ways to Create Numpy ArraysBelow are some of the ways by which we can create NumPy Arrays in Python: Create Numpy Arrays Using Lists or TuplesThe 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) Output[1 2 3 4 5] Initialize a Python NumPy Array Using Special FunctionsNumPy provides several built-in functions to generate arrays with specific properties. np.zeros(): Creates an array filled with zeros.np.ones(): Creates an array filled with ones.np.full(): Creates an array filled with a specified value.np.arange(): Creates an array with values that are evenly spaced within a given range.np.linspace(): Creates an array with values that are evenly spaced over a specified interval. 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) OutputZero 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 GenerationNumPy provides functions to create arrays filled with random numbers. np.random.rand(): Creates an array of specified shape and fills it with random values sampled from a uniform distribution over [0, 1).np.random.randn(): Creates an array of specified shape and fills it with random values sampled from a standard normal distribution.np.random.randint(): Creates an array of specified shape and fills it with random integers within a given range. 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 RoutinesNumPy provides functions to create specific types of matrices. np.eye(): Creates an identity matrix of specified size.np.diag(): Constructs a diagonal array.np.zeros_like(): Creates an array of zeros with the same shape and type as a given array.np.ones_like(): Creates an array of ones with the same shape and type as a given array. 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]] Comment More infoAdvertise with us Next Article Different Ways to Create Numpy Arrays in Python R rahulsanketpal0431 Follow Improve Article Tags : Numpy Python-numpy Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Like