Creating Arrays
Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 5 of 60.
Remember the 150 iris flowers? To a model they are 600 numbers in one NumPy array, a row per flower:
This chapter builds arrays like it, starting from a list. np.array turns a list into an array:
temps = np.array([21, 23, 19])
print(temps)
print(type(temps))This prints:
[21 23 19]
<class 'numpy.ndarray'>No commas: that is how you spot an array. And an array holds one type, so one decimal makes every value a decimal:
print(np.array([1, 2.5, 3]))This prints:
[1. 2.5 3. ]One shared type is what lets NumPy do math on a million values fast.
Challenge
EasyA kayak rental counts the kayaks rented Monday to Wednesday. The program reads those three counts, then Thursday's forecast, a decimal.
- Make an array
rentedfrom the three counts and print it. - Make an array
weekfrom the three counts and the forecast, in that order, and print it. - Print the type of
week.
For 8, 15, 4 and 12.5 the output is:
[ 8 15 4]
[ 8. 15. 4. 12.5]
<class 'numpy.ndarray'>See how one decimal forecast turned every count into a decimal?
Try it yourself
import numpy as np
# Kayaks rented Monday to Wednesday, then Thursday's forecast
monday = int(input())
tuesday = int(input())
wednesday = int(input())
forecast = float(input())
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler