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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads