Menu
CoddyTech

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:

The iris data drawn as a grid of numbers: rows 0 to 3 and row 149 shown, one row per flower, and four columns: sepal length, sepal width, petal length, petal width

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 icon

Challenge

Easy

A kayak rental counts the kayaks rented Monday to Wednesday. The program reads those three counts, then Thursday's forecast, a decimal.

  1. Make an array rented from the three counts and print it.
  2. Make an array week from the three counts and the forecast, in that order, and print it.
  3. 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler