The Wayback Machine - https://web.archive.org/web/20240616175321/https://www.geeksforgeeks.org/creating-a-one-dimensional-numpy-array/
Open In App

Creating a one-dimensional NumPy array

Last Updated : 01 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The One-dimensional array contains elements only in one dimension. In other words, the shape of the NumPy array should contain only one value in the tuple. Let us see how to create 1-dimensional NumPy arrays.

Create 1-D NumPy Array using Array() Function

In NumPy, you can create a 1-D array using the “array” function, which converts a Python list or iterable object. First, make a list then pass it in Numpy.array()

Python3




import numpy as np
 
# creating the list
list = [100, 200, 300, 400]
 
# creating 1-d array
n = np.array(list)
print(n)


Output:

[100 200 300 400]

Create 1D NumPy Array Arrange()

arrange() returns evenly spaced values within a given interval.

Python3




# importing the module
import numpy as np
 
# creating 1-d array
x = np.arange(3, 10, 2)
print(x)


Output:

[3 5 7 9]

Create 1D NumPy Array using Linspace()

Linspace() creates evenly space numerical elements between two given limits.

Python3




# importing the module
import numpy as np
 
# creating 1-d array
x = np.linspace(3, 10, 3)
print(x)


Output:

[ 3.   6.5 10. ]

Create 1D NumPy Array using Fromiter()

Fromiter() is useful for creating non-numeric sequence type array however it can create any type of array. Here we will convert a string into a NumPy array of characters.

Python3




# importing the module
import numpy as np
 
# creating the string
str = "geeksforgeeks"
 
# creating 1-d array
x = np.fromiter(str, dtype='U2')
print(x)


Output:

['g' 'e' 'e' 'k' 's' 'f' 'o' 'r' 'g' 'e' 'e' 'k' 's']

Create 1D NumPy Array using Zeros()

Zeros() returns the numbers of 0s as passed in the parameter of the method 

Python3




import numpy as np
 
arr5 = np.zeros(5)
print(arr5)


Output:

[0.0.0.0.0]

Create 1D NumPy Array using Ones() Function

ones() returns the numbers of 1s as passed in the parameter of the method 

Python3




import numpy as np
 
arr6 = np.ones(5)
print(arr6)


Output:

[1. 1. 1. 1. 1.]

Create 1D NumPy Array using Random() Function

Random() return the random module provides various methods to create arrays filled with random values.

Python3




import numpy as np
 
a=np.random.rand(2,3)
 
print(a)


Output:

[[0.47600047 0.79761493 0.88802904]
 [0.4164723  0.29056245 0.66839112]]  #output changing every time

Example: Random.randn()

The function numpy.random.randn() is used to generate random numbers from a standard normal distribution. This distribution is also known as a Gaussian distribution or a bell curve, and has a mean of 0 and a standard deviation of 1. The random numbers generated by this function are commonly referred to as standard normal or z-scores.

Python3




import numpy as np
 
a=random.randn(2,3)
 
print(a)


 Output:

[[ 0.12851253  0.78121784  0.75631304]
 [ 0.72923781 -0.80707132 -0.04503177]]   #output changing every time

1-D NumPy Array Functions

NumPy is a powerful Python library for numerical computations. It provides efficient functions for working with one-dimensional arrays.

Max() Function

numpy.max() This function return the maximum value in an array or along a specified axis.

Python3




import numpy as np
 
a=np.arange(0,1000)
 
b=a.max()
 
print(b)


 Output:

999

Min() Function

numpy.min() function is used to find the minimum value  in an array.

Python3




import numpy as np
 
a=np.arange(1,1000)
 
b=a.min()
 
print(b)


 Output:

1

Argmax() Function

numpy.argmax() index of the maximum value (argmax):To find the index of the maximum value in an array.

Python3




import numpy as np
 
a=np.array([1,6,8,2,5,7])
 
b=np.argmax(a)
 
print(b)


 Output:

2

Argmin() Function

numpy.argmin() index of the minimum value function is used to find the index of the minimum value in ana array.

Python3




import numpy as np
 
a=np.array([1,6,8,2,5,7])
 
b=np.argmin(a)
 
print(b)


 Output:

0


Previous Article
Next Article

Similar Reads

Combining a one and a two-dimensional NumPy Array
Sometimes we need to combine 1-D and 2-D arrays and display their elements. Numpy has a function named as numpy.nditer(), which provides this facility. Syntax: numpy.nditer(op, flags=None, op_flags=None, op_dtypes=None, order='K', casting='safe', op_axes=None, itershape=None, buffersize=0) Example 1: C/C++ Code # importing Numpy package import nump
2 min read
How to filter two-dimensional NumPy array based on condition ?
In this article, we are going to see how to apply the filter by the given condition in NumPy two-dimensional array. We have to obtain the output of required elements i.e., whatever we want to filter the elements from the existing array or new array. Here we are going to create a two-dimensional array in numpy. C/C++ Code import numpy as np # 2-D Ar
4 min read
Create and display a one-dimensional array-like object using Pandas in Python
Series() is a function present in the Pandas library that creates a one-dimensional array and can hold any type of objects or data in it. In this article, let us learn the syntax, create and display one-dimensional array-like object containing an array of data using Pandas library. pandas.Series() Syntax : pandas.Series(parameters) Parameters : dat
2 min read
Indexing Multi-dimensional arrays in Python using NumPy
In this article, we will cover the Indexing of Multi-dimensional arrays in Python using NumPy. NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. It contains various features.Not
3 min read
How to Concatenate two 2-dimensional NumPy Arrays?
Sometimes it might be useful or required to concatenate or merge two or more of these NumPy arrays. In this article, we will discuss various methods of concatenating two 2D arrays. But first, we have to import the NumPy package to use it: # import numpy package import numpy as np Then two 2D arrays have to be created to perform the operations, by u
5 min read
Evaluate Hermite series at points x when coefficients are multi-dimensional using NumPy in Python
In this article, we will cover how to evaluate a Hermite series at points x with a multidimensional coefficient array in Python using NumPy. Example Input: [[11 12][13 14]] Output: [[37. 63.][40. 68.]] Explanation: Hermite series at points x.NumPy.polynomial.hermite.hermval method To evaluate a Hermite series at points x with a multidimensional coe
3 min read
One-Dimensional Tensor in Pytorch
In this article, we are going to discuss a one-dimensional tensor in Python. We will look into the following concepts: Creation of One-Dimensional TensorsAccessing Elements of TensorSize of TensorData Types of Elements of TensorsView of TensorFloating Point TensorIntroduction The Pytorch is used to process the tensors. Tensors are multidimensional
5 min read
Discrete Linear Convolution of Two One-Dimensional Sequences and Get Where they Overlap in Python
In this article, we will be looking at the approach to returning the discrete linear convolution of two one-dimensional sequences and getting where they overlap in Python. Numpy np.convolve() To return the discrete linear convolution of two one-dimensional sequences, the user needs to call the numpy.convolve() method of the Numpy library in Python.
3 min read
Creating Series from list, dictionary, and numpy array in Pandas
Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index. Pandas Series is nothing but a column in an excel sheet. In this article, we will see various ways of creating a series using different data types. Creating Series from l
2 min read
NumPy Array Sorting | How to sort NumPy Array
Sorting an array is a very important step in data analysis as it helps in ordering data, and makes it easier to search and clean. In this tutorial, we will learn how to sort an array in NumPy. You can sort an array in NumPy: Using np.sort() functionin-line sortsorting along different axesUsing np.argsort() functionUsing np.lexsort() functionUsing s
4 min read