## Introduction of Numpy ones

Numpy is a python library used for working with Arrays. NumPy.Ones is a method used with NumPy that returns a new Array with shapes given size where the element value is set to 1. It creates an Array and fills the value 1 to it. We can also define the Shape and the datatype being the optional parameter. We can create a multidimensional Array with the NumPy and insert the elements in it.

**Syntax:**

`ones(shape, dtype=None, order='C')`

- This is the syntax for the ones method in NumPy.
- The shape defines the size of the parameter to be used.
- The dtype is used to define the data type of the Array, by default the value is Float.
- The order defines to which order we need to insert the data. Columnar approach or row level approach.
- So by import the NumPy Library and calling this method will create the array of the desired size with values 1 over it.

### Working of NumPy Ones with Examples

The ones function basically inserts the value 1 over the array. So it starts with first creating an array with the size and data types given and then inserts the value 1 to it. It stores the array in a continuous place in memory so relatively is faster.

#### Example #1

```
import numpy as np
aa = np.ones(5)
print(aa)
b = np.ones((5,), dtype=int)
print(b)
c = np.ones((2, 1))
print(c)
```

The Output for this will create an Array with size 5. The first output has the default data type that is double.

The Second one we declared as Integer so the output comes as Integer.

The third one create a array with a definite size.

**Code Snippet:**

```
d = np.ones((3,3))
print(d)
```

**Output:**

We can change the data type for the Array which we are making and use it accordingly in our code.

#### Example #2

`import numpy as np`

The default data type is Double. So explicitly passing the value to Int.

```
a = np.ones(2,dtype = int)
print(a)
```

**Output:**

**To Float:**

```
b = np.ones(2,dtype = float)
print(b)
```

**Output:**

**The Complex Data Type:**

```
c = np.ones(2,dtype = complex)
print(c)
```

**Output:**

**The DictType:**

```
d = np.ones(2,dtype = dict)
print(d)
```

**Output:**

**The Type List:**

```
e = np.ones(2,dtype = list)
print(e)
```

**Output:**

**Code Snippet:**

Even we can change the size of the array also to 1D, 2D or multidimensional Array.

So the same code while changing the Array size will be like:

**1D Array:**

```
a = np.ones(3,dtype = int)
print(a)
```

**Output:**

**2D Array:**

```
b = np.ones((2,2),dtype = float)
print(b)
```

**Output:**

**MultiDimensionalArray:**

```
c = np.ones((3,2),dtype = complex)
print(c)
```

**Output:**

**3d Array:**

```
d = np.ones((3,3),dtype = dict)
print(d)
```

**Output:**

**Code Snippet:**

We can also have a mixture of Datatype in the array. This means there is no limitation that the data type we are using will be of the same data type.

#### Example #3

We will create an Array of size for that numpy.ones and change the data type for that.

```
b = np.ones((2,2),dtype = [('x','int'),('y','float')])
print("The data type ")
print(b)
```

**Output:**

Here we can see that one is type Int others type being Float.

```
c = np.ones((2,3),dtype = [('x','int'),('y','complex')])
print("The data type ")
print(c)
```

**Output:**

One more parameter that is used with numpy.ones() is the order in which the data is to inserted. The parameter it uses is Row Major or Column Major. And the value it takes is C style and F style.

#### Example #4

```
d = np.ones((2,2),dtype = [('x','int'),('y','float')], order= 'C')
print("The data type Order ")
print(d)
```

**Output:**

The C is used for Row Major Insertion, whereas F for Column.

```
e = np.ones((2,2),dtype = [('x','int'),('y','float')], order= 'F')
print("The data type ")
print(e)
```

**Code Snippet:**

```
d = np.ones((3,3),dtype = [('x','int'),('y','float')], order= 'C')
print("The data type Order ")
print(d)
e = np.ones((3,2),dtype = [('x','int'),('y','float')], order= 'F')
print("The data type ")
print(e)
```

**Output:**

### Conclusion

So From the above article, we saw the use of ones function in numpy we also saw the Syntax and various methods from which we can create use the ones function and create arrays with it. We also saw how it is used and is important for Python Programming. So numpy. Ones is an important method used in python for array operations.

### Recommend ed Articles

This is a guide to NumPy ones. Here we also discuss the introduction and working of numpy ones along with different examples and its code implementation. You may also have a look at the following articles to learn more –