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.
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.
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 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]]
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]
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
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.
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:
- Fork the repository.
- Create a new branch for your feature or fix.
- Make your changes.
- Commit your changes with a clear message.
- Push to your forked repository.
- Submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.
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!