How to create 1D array?

I’m learning NumPy, a Python library for my project. For this purpose, I want to create a NumPy one-dimensional array. I have used a code below and it is giving me an error:

import numpy as np

AttributeError: module ‘numpy’ has no attribute ‘aray’

Can someone provide me with the function and code that shows me the creation of the NumPy array?

1 Like

Hello @safa, you can easily create a 1D array using the arange() function. I have provided a code below that uses this function and creates a 1D array (also called a vector) with random values from 0 to 99.

In the 4th line of the code, the vector is indexed from the 10th index (inclusive) up to the 50th index (exclusive) and then returns the resulting sub-array.

Hey @safa, you can use the linspace() function provided by NumPy. This function takes three arguments: the start value, the end value, and the number of values to generate. It returns a one-dimensional array with equally spaced values within the specified range. Here is the code below for your better understanding:

This code creates a NumPy array called vector using the linspace() function with three arguments: 10, 49, and 40. This function creates an array of 40 evenly spaced values between 10 and 49, inclusive.