Before you can crunch data in NumPy, you first need to build an array. There are several ways to create one, and the most common are np.array(), np.zeros(), np.ones(), np.arange(), np.linspace(). Which function to use depends on how you want your data to start out.
import numpy as np
# Create an array from an existing list
prices = np.array([1200, 1500, 1800, 2100])
print("Prices:", prices)
# Create a 2 rows x 3 columns array filled with zeros
empty_table = np.zeros((2, 3))
print("
Empty table:
", empty_table)
# Create values from 0 to 8, step by 2
steps = np.arange(0, 10, 2)
print("
Steps:", steps)
# Create 5 evenly spaced values between 0 and 1
percent = np.linspace(0, 1, 5)
print("
Percent points:", percent)np.array() turns an existing list into an array. np.zeros((2, 3)) builds a table of zeros with 2 rows and 3 columns. np.arange(0, 10, 2) — notice that the stop value, 10, is not included. np.linspace(0, 1, 5) splits the range between 0 and 1 into 5 evenly spaced points.
Prices: [1200 1500 1800 2100] Empty table: [[0. 0. 0.] [0. 0. 0.]] Steps: [0 2 4 6 8] Percent points: [0. 0.25 0.5 0.75 1. ]Info
shape needs to be written as a tuple in the form (rows, columns). np.zeros(2, 3) won't give you what you expect.