# Nakafa Framework: LLM
URL: /en/subject/university/bachelor/ai-ds/ai-programming/array-numpy
Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/array-numpy/en.mdx
Output docs content for large language models.
---
export const metadata = {
  title: "Creating Arrays with NumPy",
  description: "Learn how to create NumPy arrays: manual, built-in functions, sequences, and basic operations. Complete guide with tested code examples for AI programming and data science.",
  authors: [{ name: "Nabil Akbarazzima Fatih" }],
  date: "09/18/2025",
  subject: "AI Programming",
};
## NumPy Introduction
NumPy transforms Python into a powerhouse for numerical computing. While Python lists can hold mixed data types, NumPy arrays store homogeneous data in highly optimized structures. This design choice enables lightning-fast mathematical operations that would be impossibly slow with regular Python lists.
The core `ndarray` object represents n-dimensional arrays with fixed sizes and identical data types throughout. This constraint isn't limiting, it's liberating because it unlocks vectorized operations that process entire arrays in single commands. The [official NumPy documentation](https://numpy.org/doc/stable/) offers comprehensive tutorials and examples for mastering these powerful capabilities.
NumPy provides several important advantages for scientific programming:
1. **High efficiency** because it's implemented in compiled C language
2. **Vectorization** allows operations on entire arrays without explicit loops
3. **Lower memory consumption** compared to Python lists
4. **Complete and optimized mathematical operations**
Vectorization is the ability to apply a single operation to an entire array at once. It's like giving commands to an entire army formation simultaneously, rather than one by one.
## Manual Array Creation
The most basic way to create NumPy arrays is to convert existing Python data structures into arrays.
One-dimensional arrays are like a sequence of numbers in a single row. You can create them from Python lists using the `np.array()` function.
print("Shape:", a.shape)  # Output: Shape: (4,)
print("Dimensions:", a.ndim)  # Output: Dimensions: 1`
    }
  ]}
/>
Two-dimensional arrays are like tables with rows and columns. You can create them from nested lists.
Three-dimensional arrays can be illustrated as stacks of tables. Imagine like several sheets of paper stacked, where each sheet contains table data.
### Multidimensional Structure
| Dimension | Shape | Structure | Example | Axis |
|-----------|-------|-----------|---------|------|
| **1D** | `(4,)` | Number sequence in single row | `[0, 1, 2, 3]` | Axis 0 for element index |
| **2D** | `(2, 2)` | Table with rows and columns | `[[0, 1], [2, 3]]` | Axis 0 for rows, Axis 1 for columns |
| **3D** | `(2, 2, 3)` | Stack of tables (planes) | 2 planes, each plane 2x3 | Axis 0 depth, Axis 1 height, Axis 2 width |
The higher the array dimension, the more complex the data structure, but the basic principle remains the same. Each axis represents one dimension of data organization.
NumPy can create arrays from various Python data structures, including lists, tuples, and mixtures of both.
## Array Creation Functions
NumPy provides various specialized functions for creating arrays with specific patterns or values. This is like having special molds for making cakes with consistent shapes.
### Constant Value Functions
The `np.ones()` function creates arrays filled with the number 1. Useful when you need initialization with base values.
Besides `np.ones()`, there are other functions for creating arrays with special patterns:
### Random Functions
Random functions are useful for creating simulation data or initialization with random values.
### Mathematical Functions
The `np.fromfunction` function enables array creation based on mathematical functions. This is like having a formula to generate the value of each element based on its position.
The `np.empty` function creates arrays without initializing element values. This is useful when you will fill the array with values later and want to save initialization time.
## Arrays from Sequences
NumPy provides specialized functions for creating arrays from value sequences with specific patterns.
### np.arange Function
The `np.arange` function is the NumPy version of Python's `range()`, but can generate arrays with floating-point data types and directly allocate memory for elements.
### np.linspace Function
Unlike `np.arange` which uses fixed steps, `np.linspace` divides a range into a number of evenly spaced points.
### Comparison arange vs linspace
| Aspect | `np.arange` | `np.linspace` |
|--------|-------------|---------------|
| Main parameters | start, stop, step | start, stop, num |
| Control | Distance between elements | Total number of elements |
| Endpoint | Excluded | Included (default) |
| Data type | Follows input | Always float (default) |
| Usage | Sequence with fixed distance | Even range division |
## Basic Operations
After creating arrays, you can perform various operations to manipulate and analyze data.
Arrays can be reshaped using the `reshape()` function to rearrange dimensions without changing the data: