Using numpy.ones() function (5 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

Numerical Python, or NumPy for short, is a foundational package for numerical computing in Python. It provides a high-performance multidimensional array object, along with tools for working with these arrays. A critical aspect of NumPy is its capability to create arrays with default values quickly, and among its array creation functions, numpy.ones() is notably versatile. This tutorial will delve into numpy.ones(), illustrating its utility with five progressive examples.

Syntax & Parameters

Syntax:

numpy.ones(shape, dtype=None, order='C')

Parameters:

  • shape: int or tuple of ints. Shape of the new array, e.g., (2, 3) or 2.
  • dtype: data-type, optional. The desired data-type for the array, e.g., numpy.int8. Default is numpy.float64.
  • order: {‘C’, ‘F’}, optional. Whether to store multi-dimensional data in C-style (row-major) order or Fortran-style (column-major) order. The default is ‘C’.

Returns:

  • out: ndarray. Array of ones with the given shape, dtype, and order.

Basic Example: Creating a Single-dimensional Array

import numpy as np

# Create a 1D array of ones
test_array = np.ones(5)
print(test_array)

Output:

[1. 1. 1. 1. 1.]

This example demonstrates how to create a one-dimensional array filled with ones. It’s simple, yet it helps us understand the function’s basic capability and its default datatype, which is float.

Specifying Data Type

import numpy as np

# Create a 1D array of ones with integer type
test_array = np.ones(5, dtype=int)
print(test_array)

Output:

[1 1 1 1 1]

In this example, we delve into modifying the numpy.ones() function’s behavior by specifying the data type of the array elements. Here, we create a one-dimensional array of ones but as integers instead of the default float.

Creating Multidimensional Arrays

import numpy as np

# Create a 2D array (matrix) of ones
matrix = np.ones((3,4))
print(matrix)

Output:

[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]

This example showcases the versatility of numpy.ones() in creating multidimensional arrays. By passing a tuple as the shape parameter, we can generate a two-dimensional array or matrix filled with ones, showcasing the function’s flexibility.

Advanced Usage: Structured Arrays

import numpy as np

# Define a structured data type
dtype = [('name', 'U10'), ('age', 'i4'), ('height', 'f4')]
# Create a structured array of ones
structured_array = np.ones(3, dtype=dtype)
print(structured_array)

Output:

[(u'1', 1, 1.) (u'1', 1, 1.) (u'1', 1, 1.)]

This more complex example demonstrates the advanced use of numpy.ones() to create structured arrays. Here, we define a custom data type for our array, consisting of a string, an integer, and a float. Then, we generate an array where each element is a record filled with the ‘ones’ value in its respective data type format. This application illustrates the power and flexibility of NumPy, especially for creating complex data structures.

Applying numpy.ones() in Real-World Scenarios

import numpy as np

# Example: Initialize weights for a neural network layer
weights = np.ones((10,10), dtype=float) / 10
print(weights)

Output:

[[0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]]

In this realistic example, we observe how numpy.ones() can be applied to neural network development, particularly for weight initialization in neural layers. The array of ones is created and then divided by 10 to generate small weight values, illustrating one practical application of this function in machine learning.

Conclusion

numpy.ones() proves to be highly versatile for various purposes, ranging from simple array creation to complex data structure development and practical applications in fields like machine learning. Each example builds upon the last, highlighting the depth and flexibility of this powerful tool within the NumPy library.