random variable

--

Before you read this article, your should know that I am using Python in Jupyter Notebook.

Numpy is one of the most important and library in Python and the reason is that you can performing operations on n-arrays in Python. Also, It is very useful for fundamental scientific computations like: Machine Learning, Linear Algebra, Fourier Transform, etc.

If you don’t have install Numpy in Python yet, please refer to: “Installing Numpy”.

For more details about Numpy you can check Numpy Documentation

import numpy as np

Arrays in Numpy

Array in 1D

Suppose that we have the following list in python ( if you are not familiar with lists in python we can read my previous articles called: Introduction to Python” or you can find more information at w3schools):

my_list = [111,222,333]
my_list
>>>[111, 222, 333]

Now, we transform this list in an array, using numpy library:

np.array(my_list)>>>array([111, 222, 333])# Is it really an array?
type (np.array(my_list))
>>> numpy.ndarray

Arrays in 2D

Analogous to the previous step, suppose now instead of getting a 1-dimensional array, we want to get 2-dimensional array.

For that, we can cast a list of lists like this:

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

Some Built-in Methods for array

Python has a set of built-in functions. Here, I will show you some functions to create arrays, if you want to learn more about this check out here.

zeros and ones function

Generate arrays of zeros or ones:

# 1-D for zero
np.zeros(10)
>>> array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])# 3-D for zero
np.zeros((3,3))
>>> array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
---------------------------------------------------
# 1-D for ones
np.ones(10)>>> array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])# 3-D for ones
np.ones((3,3))
>>> array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])

Note that when we have 1-D we, there is only one bracket, instead 2-D we have double brackets.

Linspace Function

Return evenly spaced numbers over a specified interval.

Suppose that we want an array that starts at 0 and ends at 10 with 3 evenly spaced points between (0,10), for that we can apply Linspace Function as follows:

np.linspace(0,10,3)>>> array([ 0.,  5., 10.])
-------------------------
# another example
np.linspace(0,100,5)>>> array([ 0., 25., 50., 75., 100.])

eye

Creates an identity matrix.

## Identity matrix 4X4
np.eye(4)
>>> array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])

Random

Also, something very important about Numpy is that it has a lots of ways to create random number arrays. Here, you can see some examples, but again if you want to delve into this topic you can go to the Python documentation to the section called Generate pseudo-random numbers.

RANDINT

Returns random integers. For example, if you have a list of numbers from 0 to 5 and if you want a number from this list to come out randomly, you should use RANDINT:

np.random.randint(1,5)>>> 2#Another example: from 0 to 5, return 10 integer random number 
np.random.randint(0,50,10)
>>> array([19, 44, 0, 39, 49, 0, 34, 14, 18, 30])

RAND

Create a matrix of the size you want and fill it with random samples of uniform distribution ([0,1])

np.random.rand(2,2)>>> array([[0.34853727, 0.45665998],
[0.50126298, 0.53788552]])

RANDN

Like RAND, create a matrix of the size you want but unlike RAND, it returns a random sample from the “standard normal” distribution.

np.random.randn(2,2)>>> array([[ 0.29316526, -0.39606919],
[-1.09779477, 0.48463893]])

SOME ATTRIBUTE OF ARRAY

ARANGE

Returns an array with the interval defined by you, that is, [Start, Stop):

  • Start is the number (integer or decimal) that defines the first value in the array.
  • Stop is the number that defines the end of the array .Note that this values isn’t included in the array.
np.arange(0,10,3)>>> array([0, 3, 6, 9])#Another example np.arrange(10) #Here the 0 is default>>> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Reshape

Returns an array containing the same data with a new shape.

my_array = np.arange(0,100, 5)
print(my_array)
>>> [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95]#Applying reshape function to obtain an array 10x2my_array.reshape(10,2)>>> array([[ 0, 5],
[10, 15],
[20, 25],
[30, 35],
[40, 45],
[50, 55],
[60, 65],
[70, 75],
[80, 85],
[90, 95]])

BUT you should be careful when applying reshape function to your array because If the shape does not match the number of elements in the original array “ValueError” will occur. For example:

my_array = np.arange(0,100, 5)
print(my_array)
>>> [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95]#Applying reshape function to obtain an array 30x2my_array.reshape(30,2)>>>ValueError: cannot reshape array of size 20 into shape (30,2)

I leave you the article: NumPy: How to use reshape() and the meaning of -1 where you can find more example.

--

--

Physics and Data Science.Eagerly share insights and learn collaboratively in this growth-focused space.LinkedIn:www.linkedin.com/in/patrizia-castagno-diserafino