[Python] Introduction to NumPy: From Installation to Creating Multidimensional Arrays (ndarray)

In fields such as scientific computing, machine learning, and web data analysis, high-speed processing of large amounts of numerical data is essential. Since Python’s standard list type has limitations in terms of processing speed and syntax, the NumPy library is used as the de facto standard.

Here, I will explain how to install NumPy and the basics of creating its core component, the “multidimensional array (ndarray).”

目次

1. Installation and Import

Since NumPy is an external library, it requires installation. By convention, it is imported with the alias np.

Installation

pip install numpy

Import

import numpy as np

2. Creating NumPy Arrays (ndarray)

In NumPy, you use the np.array() function to create an array from a Python list. If you pass a “list of lists” as shown in the example, it is automatically recognized as a “2-dimensional array (matrix).”

Executable Sample Code

import numpy as np

def numpy_basics_demo():
    print("=== NumPy Array Creation ===")

    # 1. Create a 2D array (User provided example)
    # Pass nested lists
    source_list = [
        [11, 12, 13],
        [21, 22, 23],
        [31, 32, 33]
    ]
    
    # Convert to ndarray object using np.array()
    x = np.array(source_list)

    print(f"x:\n{x}")
    
    # 2. Check important array attributes (properties)
    # Checking this information is fundamental in data analysis
    print("\n[Array Attributes]")
    print(f"Type:  {type(x)}")      # <class 'numpy.ndarray'>
    print(f"Rank (ndim): {x.ndim}") # Number of dimensions (Rank) - 2 in this case
    print(f"Shape: {x.shape}")      # Number of elements in each dimension (3 rows, 3 columns -> (3, 3))
    print(f"Size:  {x.size}")       # Total number of elements (9)
    print(f"Dtype: {x.dtype}")      # Data type (e.g., int64)

    # 3. Unique NumPy features: Bulk access to elements
    # Operations that require loops in Python lists can be written in one line with NumPy
    print("\n[Operations]")
    
    # Slicing: [row_range, column_range]
    # Example: Get all rows, index 1 (2nd column)
    col_2 = x[:, 1]
    print(f"2nd column: {col_2}") # [12 22 32]
    
    # Broadcast calculation
    # Add 100 to all elements (No loop required)
    x_plus = x + 100
    print(f"x + 100:\n{x_plus}")

if __name__ == "__main__":
    numpy_basics_demo()

Explanation: Differences from Python Lists

np.array is not just a list. The biggest differences are “Memory Efficiency” and “Vector Arithmetic.”

  • Fixed Type: While lists can mix numbers and strings, NumPy arrays require “all elements to be of the same type (e.g., int, float).” This allows for contiguous memory allocation and high-speed calculation.
  • Element-wise Calculation:
    • List: [1, 2] * 2[1, 2, 1, 2] (Concatenation)
    • NumPy: np.array([1, 2]) * 2[2, 4] (Calculation on each element)

In data analysis, matrix calculations are performed utilizing these properties.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次