|
| 1 | +--- |
| 2 | +Title: '.ones()' |
| 3 | +Description: 'Creates a new array of the given shape and type, filled with ones.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Functions' |
| 10 | + - 'Linear Algebra' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +In NumPy, the **`.ones()`** function creates a new array of the given shape and type, filled with ones. |
| 18 | + |
| 19 | +This function is particularly useful when there is a need to initialize an array with a placeholder value of `1`, which might be multiplied by another value or used in various numerical computations later. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```pseudo |
| 24 | +numpy.ones(shape, dtype=None, order='C', *, like=None) |
| 25 | +``` |
| 26 | + |
| 27 | +**Parameters:** |
| 28 | + |
| 29 | +- `shape` (`int` or tuple of `int`): Defines the dimensions of the new array. |
| 30 | + - If an int, a 1-D array of that length is created. |
| 31 | + - If a tuple of ints, an array with those dimensions is created (e.g., (2, 3) for a 2x3 array). |
| 32 | +- `dtype` (optional): The desired data type for the array elements. Defaults to `float64`. Examples: `int32`, `bool_`, `float`. |
| 33 | +- `order`: ({'C', 'F'}, optional): Controls whether the array is stored in row-major or column-major memory layout. |
| 34 | + - `'C'` (default): Row-major (C-style) |
| 35 | + - `'F'`: Column-major (Fortran-style) |
| 36 | +- `like`(array_like, optional): Reference object for creating arrays not strictly of NumPy type. If `None`, a NumPy array is returned. |
| 37 | + |
| 38 | +**Return value:** |
| 39 | + |
| 40 | +- `out` (ndarray): An array of ones of the given shape, `dtype`, and order. |
| 41 | + |
| 42 | +## Example |
| 43 | + |
| 44 | +This example demonstrates the usage of the `.ones()` function: |
| 45 | + |
| 46 | +```py |
| 47 | +import numpy as np |
| 48 | + |
| 49 | +# Create a 1-D array with 5 ones (default dtype is float64) |
| 50 | +arr1 = np.ones(5) |
| 51 | +print("Array 1:\n", arr1) |
| 52 | +print("Data type of Array 1:", arr1.dtype) |
| 53 | +print("Shape of Array 1:", arr1.shape) |
| 54 | + |
| 55 | +# Create a 2-D array (3 rows, 4 columns) of ones with integer type |
| 56 | +arr2 = np.ones((3, 4), dtype=int) |
| 57 | +print("\nArray 2:\n", arr2) |
| 58 | +print("Data type of Array 2:", arr2.dtype) |
| 59 | +print("Shape of Array 2:", arr2.shape) |
| 60 | + |
| 61 | +# Create a 3-D array of ones |
| 62 | +arr3 = np.ones((2, 3, 2)) |
| 63 | +print("\nArray 3:\n", arr3) |
| 64 | +print("Data type of Array 3:", arr3.dtype) |
| 65 | +print("Shape of Array 3:", arr3.shape) |
| 66 | +``` |
| 67 | + |
| 68 | +Here is the output: |
| 69 | + |
| 70 | +```shell |
| 71 | +Array 1: |
| 72 | + [1. 1. 1. 1. 1.] |
| 73 | +Data type of Array 1: float64 |
| 74 | +Shape of Array 1: (5,) |
| 75 | + |
| 76 | +Array 2: |
| 77 | + [[1 1 1 1] |
| 78 | + [1 1 1 1] |
| 79 | + [1 1 1 1]] |
| 80 | +Data type of Array 2: int64 |
| 81 | +Shape of Array 2: (3, 4) |
| 82 | + |
| 83 | +Array 3: |
| 84 | + [[[1. 1.] |
| 85 | + [1. 1.] |
| 86 | + [1. 1.]] |
| 87 | + |
| 88 | + [[1. 1.] |
| 89 | + [1. 1.] |
| 90 | + [1. 1.]]] |
| 91 | +Data type of Array 3: float64 |
| 92 | +Shape of Array 3: (2, 3, 2) |
| 93 | +``` |
| 94 | + |
| 95 | +## Codebyte Example: Add Bias Term to a Feature Matrix in Machine Learning |
| 96 | + |
| 97 | +In many machine learning algorithms (like linear regression), there is a need to add a bias column of 1s to the input feature matrix. Here's how `numpy.ones()` can be used: |
| 98 | + |
| 99 | +```codebyte/python |
| 100 | +import numpy as np |
| 101 | +
|
| 102 | +# Feature matrix (e.g., 4 samples with 3 features each) |
| 103 | +X = np.array([ |
| 104 | + [5.1, 3.5, 1.4], |
| 105 | + [4.9, 3.0, 1.4], |
| 106 | + [6.2, 3.4, 5.4], |
| 107 | + [5.9, 3.0, 5.1] |
| 108 | +]) |
| 109 | +
|
| 110 | +# Create a column of ones for the bias term |
| 111 | +bias = np.ones((X.shape[0], 1)) |
| 112 | +
|
| 113 | +# Concatenate bias and features |
| 114 | +X_with_bias = np.hstack((bias, X)) |
| 115 | +
|
| 116 | +print("Feature Matrix with Bias Term:\n", X_with_bias) |
| 117 | +``` |
0 commit comments