Create Arrays Practice Problem
This data science coding problem helps you practice Array Creation & Attributes, create arrays, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Array Creation & Attributes.
- Problem ID: 46
- Problem key: 46-create-arrays
- URL: https://datacrack.app/solve/46-create-arrays
- Difficulty: easy
- Topic: Array Creation & Attributes
- Module: NumPy Foundations
Problem Statement
# 🧩 Create Arrays
---
### 🎯 Goal
NumPy's array creation functions are the first thing you use in every data science task. This problem teaches the five most important ones: `zeros`, `ones`, `eye`, `arange`, and `linspace` — the building blocks for initializing model weights, creating index ranges, and sampling feature spaces.
---
### 🔍 The Five Functions
| Function | Returns | Common Use |
|:---------|:--------|:-----------|
| `np.zeros(shape)` | Array of 0.0s | Initialize weights, masks |
| `np.ones(shape)` | Array of 1.0s | Bias vectors, ones-hot |
| `np.eye(n)` | Identity matrix | Linear algebra, no-op transforms |
| `np.arange(n)` | `[0, 1, 2, ..., n-1]` | Index ranges, loop replacement |
| `np.linspace(0, 1, n)` | n evenly-spaced values 0→1 | Feature grids, plot axes |
---
### 💻 Task
Implement `create_array(kind, shape)` that dispatches to the correct NumPy creation function.
---
### 📥 Input
- `kind`: string — `"zeros"`, `"ones"`, `"eye"`, `"arange"`, or `"linspace"`
- `shape`: list — for 2D arrays `[rows, cols]`, for 1D `[n]`
### 📤 Output
- The corresponding NumPy array as a nested list
---
### 🧩 Starter Code
```python
import numpy as np
def create_array(kind, shape):
"""
Create a NumPy array using the specified creation function.
Args:
kind (str): "zeros" | "ones" | "eye" | "arange" | "linspace"
shape (list): [rows, cols] for 2D, [n] for 1D
Returns:
list: The array as a nested Python list
"""
# 🧠 TODO
pass
```
---
### 🔑 Key Concepts
- NumPy shapes are **tuples**: `np.zeros((3, 4))` not `np.zeros(3, 4)`
- Use `arr.tolist()` to convert NumPy array to Python list for returning
- `np.arange` is like Python's `range` but returns a NumPy array
- `np.linspace` generates evenly-spaced floats — critical for plotting and feature gridsStarter Code
import numpy as np
def create_array(kind, shape):
"""
Create a NumPy array using the specified creation function.
Args:
kind (str): "zeros" | "ones" | "eye" | "arange" | "linspace"
shape (list): [rows, cols] for 2D, [n] for 1D
Returns:
list: The array as a nested Python list
"""
# 🧠 TODO
pass