In this blog post, I will be discussing some important operations that you can perform in NumPy arrays, which will be useful for you.

1. How to print the complete array without truncation?

In case, you have printed the array, you might have seen that the array gets truncated while displaying. That is only some of the values at the start and some of the values at the end are getting displayed.

import numpy as np
x = np.random.randn(10,10)
x
array([[-0.75606806, -0.73820922,  1.38801781, ...,  0.80185084,
         1.16933117,  0.70118423],
       [-0.67732298,  1.61885614,  2.36716239, ..., -0.23489604,
         1.18992022, -0.07948916],
       [-1.036676  , -1.18397652, -2.76977182, ...,  1.37786463,
         0.03972298,  0.12961595],
       ...,
       [ 0.56010981, -0.1966829 ,  0.08928052, ...,  0.41294451,
         0.38252878, -1.3864166 ],
       [-0.04290867, -0.09334332,  0.85880378, ..., -0.67935781,
        -1.11159562, -1.24442601],
       [-0.05233677, -0.72721717, -1.28980624, ..., -0.65419307,
        -0.51026796,  0.18763356]])

For displaying as much as you want, use the np.set_printoptions() function. You have to pass them threshold as an argument to the function. For displaying fully, pass np.inf as an argument.

np.set_printoptions(threshold = np.inf)
x
array([[-7.56068061e-01, -7.38209216e-01,  1.38801781e+00,
         6.15553376e-01,  8.77751419e-01, -7.80462186e-01,
         3.66171737e-01,  8.01850843e-01,  1.16933117e+00,
         7.01184234e-01],
       [-6.77322980e-01,  1.61885614e+00,  2.36716239e+00,
        -4.88509053e+00,  2.92502212e-01, -5.20298080e-01,
         1.25324338e+00, -2.34896036e-01,  1.18992022e+00,
        -7.94891583e-02],
       [-1.03667600e+00, -1.18397652e+00, -2.76977182e+00,
        -1.78215006e+00,  1.32161665e-01, -1.23062788e+00,
         1.47220441e+00,  1.37786463e+00,  3.97229836e-02,
         1.29615950e-01],
       [ 4.87157481e-01,  2.77468950e-01, -3.64655709e+00,
        -1.23000597e-03, -1.07205295e-01, -4.67954513e-02,
         1.04951088e+00, -5.57784011e-01,  1.12775231e+00,
         1.55268233e-01],
       [ 2.18628077e-01, -3.70475859e-01, -9.67424009e-01,
         1.75877869e-01,  3.07758737e+00, -7.52808731e-01,
        -8.00567649e-01,  1.78459130e-02,  1.92058158e-01,
         1.63263356e+00],
       [ 7.56919941e-01,  9.24336202e-01, -5.37411637e-01,
         1.00350594e+00,  3.36603233e-01,  1.93272847e+00,
        -2.38860023e-01, -1.92888130e+00, -1.71199873e+00,
        -3.27873072e+00],
       [ 9.52397345e-01,  7.57140302e-01,  2.89045771e-01,
         4.59547008e-01,  3.71816859e-01,  9.11892833e-01,
        -1.34795546e+00,  8.16883512e-01,  1.23698727e+00,
        -8.41204097e-02],
       [ 5.60109808e-01, -1.96682895e-01,  8.92805212e-02,
         2.72289819e-01, -4.58822531e-03, -1.66014541e+00,
        -3.24241883e-01,  4.12944510e-01,  3.82528781e-01,
        -1.38641660e+00],
       [-4.29086676e-02, -9.33433191e-02,  8.58803777e-01,
        -1.56395653e-01, -1.21108505e+00, -1.20596385e+00,
         7.53067977e-01, -6.79357814e-01, -1.11159562e+00,
        -1.24442601e+00],
       [-5.23367723e-02, -7.27217175e-01, -1.28980624e+00,
         6.87253267e-01,  7.04078055e-01, -2.43839117e+00,
         7.04935317e-01, -6.54193072e-01, -5.10267957e-01,
         1.87633564e-01]])

2. How to save the array as CSV file?

np.savetxt() function can be used to save a array as a CSV file. You need to pass the NumPy array, file name, and the delimiter as arguments to the function.

x = np.random.randn(5,5)
np.savetxt("numpyarray.csv",x,delimiter=',')

The file will be stored in the directory in which you are running the program.

tofile() the function can also be used. Pass the file name and separator as an argument to the function. The file will be saved in the directory in which you are running the program.

x = np.random.randn(5,5)
x.tofile("newfile.csv",sep=',')

3. How to get elements that only follow certain conditions in a NumPy array?

You need to pass the condition as an argument to the function. Like, let’s check the condition in which the elements should be greater than 0.5.

x = np.random.randn(5,5)
x
array([[-0.16122349, -0.24345879, -0.80692821,  0.81786262,  1.12061052],
       [ 0.81856821, -0.54249618,  0.00609754, -1.55320683, -0.97098009],
       [-0.49166651, -0.80256038, -0.26968152,  1.31287286, -0.2153748 ],
       [-0.94777361,  1.2412358 , -1.13210909, -0.36402683, -1.77858805],
       [ 0.52261471, -1.31941046,  0.05964615,  0.19351657,  0.17142225]])
       
x = x[x>0.5]
print(x)
[0.81786262 1.12061052 0.81856821 1.31287286 1.2412358  0.52261471]

See that we are able to get all the elements from the NumPy array that is greater than 0.5.


4. How to reshape an array?

Use the reshape() the command to reshape an array in NumPy. You need to pass the dimensions of the array you need an argument to the function.

x = np.random.randn(25)
y = x.reshape(5,5)
print(y)
[[ 0.56571012 -0.66466931 -0.63749147 -0.29833203 -0.63460809]
 [-0.71058416 -0.08304384 -1.85882866 -0.0568684  -0.35039407]
 [-1.21208541  0.56328466 -1.23398399 -0.13531119  0.60270198]
 [ 1.1712268   0.26907352 -0.46611781 -0.11712202  1.20491459]
 [ 0.35421332  1.52756191  0.71392461  1.10414555  1.21616108]]

5. How to find the index of a particular element?

np.where() the function will give you all the indexes where the given element value is present.

x = np.arange(1,100,1)
np.where(x==10)
(array([9]),)

If there are elements at multiple places, then all the indexes will be displayed when you use np.where() the function.

x = np.array([1,2,3,4,1,2,3,4,1,2,3,4])
np.where(x==1)
(array([0, 4, 8]),)

#numpy #python #machine-learning #programming #developer

9 Things You Don't Know You Can Do On NumPy Arrays
1.90 GEEK