Bird
Raised Fist0
NumPydata~5 mins

Creating structured arrays in NumPy - Quick Revision & Summary

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a structured array in NumPy?
A structured array is a special type of NumPy array that allows you to store different data types in each element, similar to a table with columns of different types.
Click to reveal answer
beginner
How do you define the data type for a structured array in NumPy?
You define a structured array's data type using a list of tuples, where each tuple contains a field name and its data type, for example: [('name', 'U10'), ('age', 'i4')] means a string field 'name' and an integer field 'age'.
Click to reveal answer
beginner
Show how to create a structured array with fields 'name' (string) and 'age' (integer) with two entries.
Use numpy.array with dtype: np.array([('Alice', 25), ('Bob', 30)], dtype=[('name', 'U10'), ('age', 'i4')])
Click to reveal answer
beginner
How do you access the 'age' field from a structured array named 'data'?
You access it by using data['age'], which returns an array of all ages in the structured array.
Click to reveal answer
beginner
Why are structured arrays useful in data science?
They let you store and manipulate tabular data with different types efficiently, similar to a spreadsheet or database table, but with NumPy's speed and functionality.
Click to reveal answer
What does the dtype [('name', 'U10'), ('age', 'i4')] specify in a structured array?
AA string field 'name' with max length 10 and an integer field 'age'
BTwo integer fields named 'name' and 'age'
CA float field 'name' and a string field 'age'
DAn array of 10 strings and 4 integers
How do you create a structured array with fields 'x' and 'y' both as floats?
Anp.array([(1, 2)], dtype=[('x', 'i4'), ('y', 'i4')])
Bnp.array([1.0, 2.0], dtype='float')
Cnp.array([(1.0, 2.0)], dtype=[('x', 'f8'), ('y', 'f8')])
Dnp.array([{'x':1.0, 'y':2.0}])
How can you access the 'name' field of a structured array called 'arr'?
Aarr['name']
Barr.name
Carr[0]['name']
Darr.get('name')
What type of data can a structured array hold?
AOnly integers
BMultiple data types in different fields
COnly floats
DOnly strings
Which of these is a benefit of using structured arrays?
AOnly works with numeric data
BFaster than plain Python lists for strings only
CAutomatically sorts data
DEfficient storage of tabular data with mixed types
Explain how to create a structured array with fields for 'city' (string) and 'population' (integer).
Think about how to specify the dtype and how to pass data as tuples.
You got /3 concepts.
    Describe how you would access and print all values of a specific field in a structured array.
    Remember how to get a column from a table.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the main purpose of creating a structured array in numpy?
      easy
      A. To create arrays with only one data type
      B. To store data with different types in named fields within one array
      C. To speed up numerical calculations on large arrays
      D. To visualize data using plots

      Solution

      1. Step 1: Understand structured arrays

        Structured arrays allow storing mixed data types in one array with named fields.
      2. Step 2: Compare options

        Only To store data with different types in named fields within one array correctly describes this purpose; others describe unrelated features.
      3. Final Answer:

        To store data with different types in named fields within one array -> Option B
      4. Quick Check:

        Structured arrays = mixed types + named fields [OK]
      Hint: Structured arrays hold mixed data types with names [OK]
      Common Mistakes:
      • Thinking structured arrays only hold one data type
      • Confusing structured arrays with plotting functions
      • Assuming structured arrays speed up all calculations
      2. Which of the following is the correct way to define a structured array dtype with fields 'name' (string) and 'age' (integer)?
      easy
      A. dtype = [('name', 'U10'), ('age', 'i4')]
      B. dtype = ['name': str, 'age': int]
      C. dtype = {'name': 'string', 'age': 'int'}
      D. dtype = [('name', str), ('age', float)]

      Solution

      1. Step 1: Recall dtype syntax for structured arrays

        Structured array dtypes are defined as a list of tuples: (field_name, data_type).
      2. Step 2: Check each option

        dtype = [('name', 'U10'), ('age', 'i4')] uses correct tuple syntax with numpy string and integer types. Others use invalid syntax or wrong types.
      3. Final Answer:

        dtype = [('name', 'U10'), ('age', 'i4')] -> Option A
      4. Quick Check:

        Structured dtype = list of (name, type) tuples [OK]
      Hint: Use list of (field, type) tuples for dtype [OK]
      Common Mistakes:
      • Using dictionary syntax instead of list of tuples
      • Using Python types instead of numpy dtype strings
      • Mixing float type for integer fields
      3. What will be the output of the following code?
      import numpy as np
      dtype = [('id', 'i4'), ('score', 'f4')]
      data = np.array([(1, 9.5), (2, 8.0)], dtype=dtype)
      print(data['score'])
      medium
      A. Error: KeyError
      B. [1 2]
      C. [(1, 9.5) (2, 8.0)]
      D. [9.5 8. ]

      Solution

      1. Step 1: Understand structured array creation

        The array has fields 'id' (int) and 'score' (float), with two records.
      2. Step 2: Access the 'score' field

        Accessing data['score'] returns an array of the 'score' values: [9.5, 8.0].
      3. Final Answer:

        [9.5 8. ] -> Option D
      4. Quick Check:

        Accessing field returns array of that column [OK]
      Hint: Access fields by name to get column arrays [OK]
      Common Mistakes:
      • Expecting full records instead of single field
      • Confusing field names causing KeyError
      • Thinking output is list of tuples
      4. Identify the error in the following code that tries to create a structured array:
      import numpy as np
      dtype = [('name', 'U5'), ('age', 'i4')]
      data = np.array([('Alice', 25), ('Bob', 30)], dtype=dtype)
      print(data['age'])
      medium
      A. The dtype definition is missing field names
      B. The tuple elements should be lists, not tuples
      C. No error; code runs correctly
      D. The string length 'U5' is too short for 'Alice'

      Solution

      1. Step 1: Check dtype and data compatibility

        'U5' means Unicode string of length 5, 'Alice' has 5 characters, so it fits.
      2. Step 2: Verify data structure and code correctness

        Data is a list of tuples matching dtype fields; code runs without error and prints ages.
      3. Final Answer:

        No error; code runs correctly -> Option C
      4. Quick Check:

        String length matches field size; tuples allowed [OK]
      Hint: Check string length matches longest string [OK]
      Common Mistakes:
      • Assuming string length too short causes error
      • Thinking tuples are invalid for data input
      • Believing dtype must be dictionary
      5. You have a list of employee data: [('John', 28, 50000), ('Jane', 32, 60000), ('Doe', 24, 45000)]. How do you create a structured array with fields 'name' (string, max 10 chars), 'age' (int), and 'salary' (float) to store this data?
      hard
      A. dtype = [('name', 'U10'), ('age', 'i4'), ('salary', 'f8')]; np.array(data, dtype=dtype)
      B. dtype = [('name', str), ('age', int), ('salary', float)]; np.array(data)
      C. dtype = {'name': 'U10', 'age': 'i4', 'salary': 'f8'}; np.array(data)
      D. dtype = [('name', 'S10'), ('age', 'i4'), ('salary', 'f4')]; np.array(data)

      Solution

      1. Step 1: Define correct dtype for fields

        Use list of tuples with field names and numpy types: 'U10' for Unicode string max 10 chars, 'i4' for int, 'f8' for float64.
      2. Step 2: Create structured array with data and dtype

        Pass data and dtype to np.array to create structured array storing all fields correctly.
      3. Final Answer:

        dtype = [('name', 'U10'), ('age', 'i4'), ('salary', 'f8')]; np.array(data, dtype=dtype) -> Option A
      4. Quick Check:

        Use list of (field, type) tuples and pass dtype [OK]
      Hint: Use Unicode string 'U10' for names, correct numeric types [OK]
      Common Mistakes:
      • Using Python types instead of numpy dtype strings
      • Using dictionary instead of list of tuples for dtype
      • Choosing byte string 'S10' instead of Unicode 'U10'