Menu
CoddyTech

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 scores array drawn as a grid of 2 rows and 4 columns of numbers, titled scores.shape is (2, 4)

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 type

This prints:

(2, 4)
2
8
int64

Check the shape first with any new data: a model expects one row per sample.

challenge icon

Challenge

Easy

Two cinemas can merge their ticket tables only if the layouts match. Park's tickets are in rows; Plaza's in plaza.

  1. Turn rows into an array park.
  2. Print each cinema's days, showings and values, Park first.
  3. Print Same layout: and True or False.

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: False

Try 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
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