C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Np.array: Here we create an array with np.array. A Python List is passed to the method. We then multiply the array by 2.
ListPython program that uses numpy
import numpy as np
# Create an array of 3 elements.
array_one = np.array([5, 10, 15])
print(array_one)
# An array can be multiplied by an int.
# ... This is an array broadcasting example.
array_two = array_one * 2
print(array_two)
Output
[ 5 10 15]
[10 20 30]
Here: We generate a 1-dimensional array of 5 random float values. Some of the numbers are negative.
Python program that gets random array
import numpy as np
# Create a 1-dimensional array of 5 random values.
random_values = np.random.normal(size=[1, 5])
print(random_values)
Output
[[ 0.49786323 0.81794554 -0.63191935 0.25130401 0.80529426]]
First argument: This is the starting value for the range. So with 5, our range starts at the value 5.
Second argument: The exclusive end of the range—this value is not included in the range. With a value of 6, our range does not include 6.
Third argument: The step—this is how much each element in the resulting range is incremented. This is the same way Python slices work.
Python program that uses arange
import numpy as np
# Get values using arange method.
# ... This is an exclusive bound.
values = np.arange(5)
print(values)
# Two arguments can be used.
# ... The second argument is an exclusive bound.
values = np.arange(3, 6)
print(values)
# A step can be used.
values = np.arange(0, 5, 2)
print(values)
Output
[0 1 2 3 4]
[3 4 5]
[0 2 4]