Populate with Fixed Values
Lesson 6 of 18 in Coddy's Numpy Fundamentals course.
Numpy gives us the ability to create an array without passing a list using the zeros and the ones functions.
five_zeros = np.zeros(5)
three_ones = np.ones(3)Outputs:
>> array([0., 0., 0., 0., 0., 0.])
>> array([1., 1., 1.])The same thing can be accomplished with lists:
five_zeros = np.array([0]*5)
three_ones = np.array([1]*3)You can also pass any shape you want:
np.zeroes((1, 2, 3))
>> array([[[0., 0., 0.],
[0., 0., 0.]]])Challenge
EasyCreate a function named ones_and_zeros that receives a list representing a shape, and returns a numpy array one dimension higher than the input shape.
- The first element of this array is an array of zeroes with the given shape
- The second element of this array is an array of ones with the given shape.
Try it yourself
import numpy as np
def ones_and_zeros(shape):
pass
All lessons in Numpy Fundamentals
2N-Dimensional Array Creation
Higher DimensionUnderstanding ShapesPopulate with Fixed ValuesNumpy TypesRangeReshape