Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to create an empty and a full NumPy array?
Next article icon

One-dimensional arraycontains elements only in one dimension. In other words, the shape of theNumPy array should contain only one value in the tuple. We can create a 1-D array in NumPy using the array() function, which converts a Python list or iterable object.

Python
importnumpyasnp# Create a one-dimensional array from a listarray_1d=np.array([1,2,3,4,5])print(array_1d)

Output
[1 2 3 4 5]

Let's explore various methods to Create one- dimensional Numpy Array.

UsingArrange()

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

Python
# importing the moduleimportnumpyasnp# creating 1-d arrayx=np.arange(3,10,2)print(x)

Output
[3 5 7 9]

Using Linspace()

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

Python
importnumpyasnp# creating 1-d arrayx=np.linspace(3,10,3)print(x)

Output:

[ 3.   6.5 10. ]

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.

Python
importnumpyasnp# creating the stringstr="geeksforgeeks"# creating 1-d arrayx=np.fromiter(str,dtype='U2')print(x)

Output:

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

Using Zeros()

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

Python
importnumpyasnparr5=np.zeros(5)print(arr5)

Output:

[0.0.0.0.0]

Using Ones() Function

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

Python
importnumpyasnparr6=np.ones(5)print(arr6)

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

Using Random() Function

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

Python
importnumpyasnpa=np.random.rand(2,3)print(a)

Output
[[0.07752187 0.74982957 0.53760007] [0.73647835 0.62539542 0.27565598]]

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp