Agent Skills by ALSEL
Anthropic Claudeソフトウェア開発⭐ リポ 0品質スコア 50/100

NumPy高速化|ベクトル演算とメモリ最適化の実践ガイド

NumPy配列を高速化する実践テクニック集。ベクトル演算でループを排除し、メモリ管理・ブロードキャスト・データ型最適化で数値計算のボトルネックを解消。Pythonの処理速度を改善したいエンジニア向け。

description の原文を見る

Best practices for NumPy array programming, numerical computing, and performance optimization in Python

SKILL.md 本文

NumPy Best Practices

Expert guidelines for NumPy development, focusing on array programming, numerical computing, and performance optimization.

Code Style and Structure

  • Write concise, technical Python code with accurate NumPy examples
  • Prefer vectorized operations over explicit loops for performance
  • Use descriptive variable names reflecting data content (e.g., weights, gradients, input_array)
  • Follow PEP 8 style guidelines for Python code
  • Use functional programming patterns when appropriate

Array Creation and Manipulation

  • Use appropriate array creation functions: np.array(), np.zeros(), np.ones(), np.empty(), np.arange(), np.linspace()
  • Prefer np.zeros() or np.empty() for pre-allocation when array size is known
  • Use np.concatenate(), np.vstack(), np.hstack() for combining arrays
  • Leverage broadcasting for operations on arrays with different shapes

Indexing and Slicing

  • Use advanced indexing with boolean arrays for conditional selection
  • Prefer views over copies when possible to save memory
  • Use np.where() for conditional element selection
  • Understand the difference between fancy indexing (creates copy) and basic slicing (creates view)

Data Types

  • Specify appropriate data types explicitly using dtype parameter
  • Use np.float32 for memory-efficient computations when full precision is not needed
  • Be aware of integer overflow with fixed-size integer types
  • Use np.asarray() for type conversion without unnecessary copies

Performance Optimization

Vectorization

  • Always prefer vectorized operations over Python loops
  • Use NumPy universal functions (ufuncs) for element-wise operations
  • Leverage np.einsum() for complex tensor operations
  • Use np.dot() or @ operator for matrix multiplication

Memory Management

  • Use np.ndarray.flags to check memory layout (C-contiguous vs Fortran-contiguous)
  • Prefer in-place operations with out parameter when possible
  • Use memory-mapped arrays (np.memmap) for large datasets
  • Be mindful of array copies vs views

Computation Efficiency

  • Use np.sum(), np.mean(), np.std() with axis parameter for aggregations
  • Leverage np.cumsum(), np.cumprod() for cumulative operations
  • Use np.searchsorted() for efficient sorted array operations

Error Handling and Validation

  • Validate input shapes and data types before computations
  • Use assertions for dimension checking with informative messages
  • Handle NaN and Inf values appropriately with np.isnan(), np.isinf()
  • Use np.errstate() context manager for controlling floating-point error handling

Random Number Generation

  • Use np.random.default_rng() for modern random number generation
  • Set seeds for reproducibility: rng = np.random.default_rng(seed=42)
  • Prefer the new Generator API over legacy np.random functions
  • Use appropriate distributions: rng.normal(), rng.uniform(), rng.choice()

Linear Algebra

  • Use np.linalg for linear algebra operations
  • Leverage np.linalg.solve() instead of computing inverse for linear systems
  • Use np.linalg.eig(), np.linalg.svd() for decompositions
  • Check matrix condition with np.linalg.cond() before inversion

Testing and Documentation

  • Write unit tests using pytest with np.testing assertions
  • Use np.testing.assert_array_equal() for exact comparisons
  • Use np.testing.assert_array_almost_equal() for floating-point comparisons
  • Include comprehensive docstrings following NumPy docstring format

Key Conventions

  • Import as import numpy as np
  • Use snake_case for variables and functions
  • Document array shapes in docstrings
  • Profile code with %timeit to identify bottlenecks

ライセンス: Apache-2.0(寛容ライセンスのため全文を引用しています) · 原本リポジトリ

詳細情報

作者
mindrally
リポジトリ
mindrally/skills
ライセンス
Apache-2.0
最終更新
不明

Source: https://github.com/mindrally/skills / ライセンス: Apache-2.0

関連スキル

汎用ソフトウェア開発⭐ リポ 39,967

doubt-driven-development

重要な判断はすべて、本番環境への展開前に新しい視点から対抗的レビューを実施します。速度より正確性が重要な場合、不慣れなコードを扱う場合、本番環境・セキュリティに関わるロジック・取り消し不可の操作など影響度が高い場合、または後でバグを修正するよりも今検証する方が効率的な場合に活用してください。

by addyosmani
汎用ソフトウェア開発⭐ リポ 1,175

apprun-skills

TypeScriptを使用したAppRunアプリケーションのMVU設計に関する総合的なガイダンスが得られます。コンポーネントパターン、イベントハンドリング、状態管理(非同期ジェネレータを含む)、パラメータと保護機能を備えたルーティング・ナビゲーション、vistestを使用したテストに対応しています。AppRunコンポーネントの設計・レビュー、ルートの配線、状態フローの管理、AppRunテストの作成時に活用してください。

by yysun
OpenAIソフトウェア開発⭐ リポ 797

desloppify

コードベースのヘルスチェックと技術負債の追跡ツールです。コード品質、技術負債、デッドコード、大規模ファイル、ゴッドクラス、重複関数、コードスメル、命名規則の問題、インポートサイクル、結合度の問題についてユーザーが質問した場合に使用してください。また、ヘルススコアの確認、次の改善項目の提案、クリーンアップ計画の作成をリクエストされた際にも対応します。29言語に対応しています。

by Git-on-my-level
汎用ソフトウェア開発⭐ リポ 39,967

debugging-and-error-recovery

テストが失敗したり、ビルドが壊れたり、動作が期待と異なったり、予期しないエラーが発生したりした場合に、体系的な根本原因デバッグをガイドします。推測ではなく、根本原因を見つけて修正するための体系的なアプローチが必要な場合に使用してください。

by addyosmani
汎用ソフトウェア開発⭐ リポ 39,967

test-driven-development

テスト駆動開発により実装を進めます。ロジックの実装、バグの修正、動作の変更など、あらゆる場面で活用できます。コードが正常に動作することを証明する必要がある場合、バグ報告を受けた場合、既存機能を修正する予定がある場合に使用してください。

by addyosmani
汎用ソフトウェア開発⭐ リポ 39,967

incremental-implementation

変更を段階的に実施します。複数のファイルに影響する機能や変更を実装する場合に使用してください。大量のコードを一度に書こうとしている場合や、タスクが一度では完結できないほど大きい場合に活用します。

by addyosmani
本サイトは GitHub 上で公開されているオープンソースの SKILL.md ファイルをクロール・インデックス化したものです。 各スキルの著作権は原作者に帰属します。掲載に問題がある場合は info@alsel.co.jp または /takedown フォームよりご連絡ください。
原作者: mindrally · mindrally/skills · ライセンス: Apache-2.0