In this post, we are going to learn Way to create 1-D NumPy array using list.The 1-D array is the basic array size that NumPy provides us to use in a lot of mathematical problems. Today we are going to learn how to create a 1-D array using the NumPy library. We will import NumPy to use in our program, and then we will use the NumPy methods to create a 1-D NumPy array. So let us start our program:
1. Create 1D NumPy array using Python list
As you know, in Python we have a lot of data structures like lists, tuples, dictionaries etc. In our first example we will learn, how to create a NumPy array by using a regular Python list. NumPy provides us the array() function that is capable of creating a NumPy array. As an argument to this methods, We can pass the Python list to the array function and this will create an array using this list.
Here are some examples of this:
Python Program to create NumPy array Using List
import numpy as np array_one = np.array([1,2,3,4]) print("array one is linear",array_one)
array one is linear [1 2 3 4]
Python Program to create NumPy Using List
numbers = [1,2,3,4] array_two = np.array(numbers) print("array two is linear",array_two)
array two is linear [1 2 3 4]
These are some of the simple examples which we have taken. In practical data science cases we read huge data files and get the data in the form of Python lists.
Those lists can be converted to numpy array to perform the calculations.