Shape and Size
Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 6 of 60.
Machine learning data is a table: one row per sample, one column per feature. A list of lists makes one, a 2D array:
scores = np.array([[8, 6, 9, 7],
[7, 10, 5, 8]])The array knows its own size. No parentheses: these are stored facts, not functions.
print(scores.shape) # rows, columns
print(scores.ndim) # dimensions
print(scores.size) # values
print(scores.dtype) # their typeThis prints:
(2, 4)
2
8
int64Check the shape first with any new data: a model expects one row per sample.
Challenge
EasyTwo cinemas can merge their ticket tables only if the layouts match. Park's tickets are in rows; Plaza's in plaza.
- Turn
rowsinto an arraypark. - Print each cinema's days, showings and values, Park first.
- Print
Same layout:andTrueorFalse.
For 2 days of 3 showings (48, 112, 65, then 37, 98, 120):
Park: 2 days, 3 showings a day, 6 values
Plaza: 7 days, 4 showings a day, 28 values
Same layout: FalseTry it yourself
import numpy as np
# Tickets sold at the Park cinema: one row per day, one value per showing
days = int(input())
showings = int(input())
rows = []
for day in range(days):
row = []
for showing in range(showings):
row.append(int(input()))
rows.append(row)
# Tickets sold at the Plaza cinema: one row per day, one value per showing
plaza = np.array([
[34, 58, 91, 40],
[29, 63, 85, 37],
[41, 70, 96, 52],
[38, 66, 102, 47],
[55, 88, 120, 74],
[72, 95, 131, 80],
[60, 81, 99, 45],
])
# 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