There is `np.set_printoptions`

which allows to modify the "line-width" of the printed NumPy array:

```
>>>
import numpy as np
>>>
np.set_printoptions(linewidth = np.inf) >>>
a = np.array([1.02090721, 1.02763091, 1.03899317, 1.00630297, 1.00127454, 0.89916715, 1.04486896]) >>>
print(a)[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715 1.04486896]
```

Similar to here you could use a contextmanager if you just want to *temporarily* change that:

```
import numpy as np
from contextlib
import contextmanager
@contextmanager
def print_array_on_one_line():
oldoptions = np.get_printoptions()
np.set_printoptions(linewidth = np.inf)
yield
np.set_printoptions( ** oldoptions)
```

Then you use it like this (fresh interpreter session assumed):

>>> import numpy as np >>> np.random.random(10) # default [0.12854047 0.35702647 0.61189795 0.43945279 0.04606867 0.83215714 0.4274313 0.6213961 0.29540808 0.13134124 ] >>> with print_array_on_one_line(): # in this block it will be in one line ...print(np.random.random(10))[0.86671089 0.68990916 0.97760075 0.51284228 0.86199111 0.90252942 0.0689861 0.18049253 0.78477971 0.85592009] >>> np.random.random(10) # reset[0.65625313 0.58415921 0.17207238 0.12483019 0.59113892 0.19527236 0.20263972 0.30875768 0.50692189 0.02021453]

If you want a customized version of `str(a)`

, the answer is `array_str`

:

```
>>> print(a)[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715 1.04486896] >>>
str(a)
'[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715\n 1.04486896]' >>>
np.array_str(a, max_line_width = np.inf)
'[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715 1.04486896]' >>>
print(np.array_str(a, max_line_width = np.inf)[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715 1.04486896]
```

Type cast to a list when printing.

```
import numpy as np
a = np.array([1.02090721, 1.02763091, 1.03899317, 1.00630297, 1.00127454, 0.89916715, 1.04486896])
print(list(a))
```

Be aware that these visualizations are meant to simplify ideas and give you a basic understanding of NumPy concepts and mechanics. Arrays and array operations are much more complicated than are captured here!,NumPy arrays are faster and more compact than Python lists. An array consumes less memory and is convenient to use. NumPy uses much less memory to store data and it provides a mechanism of specifying the data types. This allows the code to be optimized even further.,The .npy and .npz files store data, shape, dtype, and other information required to reconstruct the ndarray in a way that allows the array to be correctly retrieved, even when the file is on another machine with different architecture.,It’s simple to read in a CSV that contains existing information. The best and easiest way to do this is to use Pandas.

conda install numpy

pip install numpy

`import numpy as np`

>>> a = np.arange(6) >>> a2 = a[np.newaxis,: ] >>> a2.shape(1, 6)

>>> a = np.array([1, 2, 3, 4, 5, 6])

>>> a = np.array([ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ])

One commonly needed routine is accessing of single rows or columns of an array. This can be done by combining indexing and slicing, using an empty slice marked by a single colon (:):,All of the preceding routines worked on single arrays. It's also possible to combine multiple arrays into one, and to conversely split a single array into multiple arrays. We'll take a look at those operations here.,First let's discuss some useful array attributes. We'll start by defining three random arrays, a one-dimensional, two-dimensional, and three-dimensional array. We'll use NumPy's random number generator, which we will seed with a set value in order to ensure that the same random arrays are generated each time this code is run:,Concatenation, or joining of two arrays in NumPy, is primarily accomplished using the routines np.concatenate, np.vstack, and np.hstack. np.concatenate takes a tuple or list of arrays as its first argument, as we can see here:

import numpy as np np.random.seed(0) # seed for reproducibility x1 = np.random.randint(10, size = 6) # One - dimensional array x2 = np.random.randint(10, size = (3, 4)) # Two - dimensional array x3 = np.random.randint(10, size = (3, 4, 5)) # Three - dimensional array

```
print("x3 ndim: ", x3.ndim)
print("x3 shape:", x3.shape)
print("x3 size: ", x3.size)
```

x3 ndim: 3 x3 shape: (3, 4, 5) x3 size: 60

`print("dtype:", x3.dtype)`

dtype: int64

```
print("itemsize:", x3.itemsize, "bytes")
print("nbytes:", x3.nbytes, "bytes")
```

The numpy ndarray class is used to represent both matrices and vectors. To construct a matrix in numpy we list the rows of the matrix in a list and pass that list to the numpy array constructor.,numpy overloads the array index and slicing notations to access parts of a matrix. For example, to print the bottom right entry in the matrix A we would do,For example, to construct a numpy array that corresponds to the matrix,In our next example program I will use numpy to construct the appropriate matrices and vectors and solve for the β vector. Once we have solved for β we will use it to make predictions for some test data points that we initially left out of our input data set.

we would do

A = np.array([ [1, -1, 2], [3, 2, 0] ])

The code for this is

v = np.transpose(np.array([ [2, 1, 3] ]))

numpy overloads the array index and slicing notations to access parts of a matrix. For example, to print the bottom right entry in the matrix *A* we would do

`print(A[1, 2])`

To do a matrix multiplication or a matrix-vector multiplication we use the `np.dot()`

method.

w = np.dot(A, v)

We start by constructing the arrays for *A* and **b**.

A = np.array([ [2, 1, -2], [3, 0, 1], [1, 1, -1] ]) b = np.transpose(np.array([ [-3, 5, -2] ])

Create a 2D Numpy adArray with3 rows & columns | Matrix ,Select the element at row index 1 and column index 2. ,Contents of the 2D Numpy Array nArr2D created at start of article are, ,To select sub 2d Numpy Array we can pass the row & column index range in [] operator i.e.

First of all, let’s import numpy module i.e.

`import numpy as np`

# Create a 2 D Numpy adArray with 3 rows & 3 columns | Matrix nArr2D = np.array(([21, 22, 23], [11, 22, 33], [43, 77, 89]))

We can use [][] operator to select an element from Numpy Array i.e.

`ndArray[row_index][column_index]`

Or we can pass the comma separated list of indices representing row index & column index too i.e.

# Another way to select element at row index 1 & column index 2 num = nArr2D[1, 2] print('element at row index 1 & column index 2 is : ', num)

We can call [] operator to select a single or multiple row. To select a single row use,

`ndArray[row_index]`

Last Updated : 24 Dec, 2021,GATE CS 2021 Syllabus

**Output:**

```
[1, 2]
[3, 4]
[5, 6]
[1, 3, 5]
[2, 4, 6]
```

**Output:**

(1, 2, 3) (4, 5, 6) (7, 8, 9) (10, 11, 12) (1, 4, 7, 10) (2, 5, 8, 11) (3, 6, 9, 12)

**Output:**

[ [1 2 3] [4 5 6] ] [ [1 4] [2 5] [3 6] ]