Skip to content

Explore NumPy ufuncs with examples of element-wise operations, broadcasting, and custom ufuncs in Python. Perfect for learning efficient array handling! πŸš€πŸ’»

Notifications You must be signed in to change notification settings

folkKEEHEE/numpy-ufuncs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 

Repository files navigation

NumPy Universal Functions (ufunc) Examples Collection 🌟

NumPy Ufuncs Python Jupyter Notebook

Welcome to the NumPy Universal Functions (ufunc) Examples Collection! This repository provides a simple yet comprehensive set of examples to help you understand element-wise operations, broadcasting, and custom ufuncs in Python using the NumPy library.

Table of Contents

  1. Getting Started
  2. Examples
  3. Installation
  4. Usage
  5. Contributing
  6. License
  7. Releases

Getting Started

To get started with this repository, you can download the examples from the Releases section. Once you have downloaded the necessary files, you can run them in your local environment.

Examples

Basic Operations

NumPy universal functions, or ufuncs, allow you to perform element-wise operations on arrays. Here are some basic examples:

import numpy as np

# Create two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

# Add the arrays
result = np.add(a, b)
print(result)  # Output: [5 7 9]

Broadcasting

Broadcasting is a powerful feature in NumPy that allows you to perform operations on arrays of different shapes. Here’s how it works:

import numpy as np

# Create a 1D array
a = np.array([1, 2, 3])

# Create a 2D array
b = np.array([[10], [20], [30]])

# Add the arrays using broadcasting
result = a + b
print(result)
# Output:
# [[11 12 13]
#  [21 22 23]
#  [31 32 33]]

Custom ufuncs

You can create your own universal functions using the np.frompyfunc method. Here’s an example of a custom ufunc that calculates the square of an input:

import numpy as np

# Define a simple function
def square(x):
    return x * x

# Create a ufunc from the function
square_ufunc = np.frompyfunc(square, 1, 1)

# Use the custom ufunc
result = square_ufunc(np.array([1, 2, 3, 4]))
print(result)  # Output: [1 4 9 16]

Installation

To use the examples in this repository, you need to have Python and NumPy installed. You can install NumPy using pip:

pip install numpy

If you want to run the examples in Jupyter Notebook, make sure to install Jupyter as well:

pip install notebook

Usage

After installing the required packages, you can run the Jupyter Notebooks provided in this repository. Simply navigate to the directory where you downloaded the files and run:

jupyter notebook

This command will open a new tab in your web browser where you can access the notebooks.

Contributing

Contributions are welcome! If you have any examples or improvements to add, please feel free to fork the repository and submit a pull request.

To contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or fix.
  3. Make your changes.
  4. Commit your changes with a clear message.
  5. Push to your forked repository.
  6. Submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Releases

You can find the latest releases of this repository in the Releases section. Download the files and execute them to explore the examples.

Feel free to explore the various topics covered in this repository, including:

  • Distributed computing
  • Jupyter Notebooks
  • NumPy arrays
  • NumPy functions
  • NumPy library
  • Python 3

Happy coding!

About

Explore NumPy ufuncs with examples of element-wise operations, broadcasting, and custom ufuncs in Python. Perfect for learning efficient array handling! πŸš€πŸ’»

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •