diff --git a/machine learning basics/numpy_basics_ds.html b/machine learning basics/numpy_basics_ds.html new file mode 100644 index 0000000..4a6bb3a --- /dev/null +++ b/machine learning basics/numpy_basics_ds.html @@ -0,0 +1,14348 @@ + + + + +numpy_basics_ds + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+

Tushar Goyal - IIT Mandi

+
+
+
+
+
+
+

Machine Learning Basic Libraries used in Algorithms and Code Implementations

Images and some explainations taken from Various Medium blogs

+
+
+
+
+
+
In [2]:
+
+
+
import numpy as np
+
+ +
+
+
+ +
+
+
+
In [4]:
+
+
+
a=np.array([[1],[2],[3],[4],[5]])
+print(a)
+print(a.shape)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[1]
+ [2]
+ [3]
+ [4]
+ [5]]
+(5, 1)
+
+
+
+ +
+
+ +
+
+
+
In [5]:
+
+
+
b=np.zeros((3,3))#array of zeroes
+print(b)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[0. 0. 0.]
+ [0. 0. 0.]
+ [0. 0. 0.]]
+
+
+
+ +
+
+ +
+
+
+
In [6]:
+
+
+
np.ones((2,2))# 2-D array of ones of desired shape
+
+ +
+
+
+ +
+
+ + +
+ +
Out[6]:
+ + + + +
+
array([[1., 1.],
+       [1., 1.]])
+
+ +
+ +
+
+ +
+
+
+
In [7]:
+
+
+
#Array of some constants
+np.full((3,2),5)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[7]:
+ + + + +
+
array([[5, 5],
+       [5, 5],
+       [5, 5]])
+
+ +
+ +
+
+ +
+
+
+
In [8]:
+
+
+
#Identity matrix-Size/Square matrix- Very Useful
+d=np.eye(4)
+print(d)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[1. 0. 0. 0.]
+ [0. 1. 0. 0.]
+ [0. 0. 1. 0.]
+ [0. 0. 0. 1.]]
+
+
+
+ +
+
+ +
+
+
+
In [10]:
+
+
+
#Random Matrix
+randomMatrix=np.random.random((2,3))
+print(randomMatrix)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[0.50360358 0.96281574 0.29079462]
+ [0.15206788 0.67035403 0.79278638]]
+
+
+
+ +
+
+ +
+
+
+
In [11]:
+
+
+
print(randomMatrix[:,2])
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[0.29079462 0.79278638]
+
+
+
+ +
+
+ +
+
+
+
+

Python List Slicing

[start : stop : steps]

+
    +
  • which means that slicing will start from index start +will go up to stop in step of steps. +Default value of start is 0, stop is last index of list +and for step it is 1
  • +
  • Negative value of steps shows right to left traversal instead of left to right traversal that is why [: : -1] prints list in reverse order.
  • +
  • [: stop] will slice list from starting till stop index and [start : ] will slice list from start index till end.
  • +
+ +
+
+
+
+
+
In [12]:
+
+
+
#Mathematical operations
+x=np.array([[1,2],[3,4]])
+y=np.array([[5,6],[7,8]])
+#Element wise addition
+print(x+y)
+print(np.add(x,y))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[ 6  8]
+ [10 12]]
+[[ 6  8]
+ [10 12]]
+
+
+
+ +
+
+ +
+
+
+
In [15]:
+
+
+
#Element wise Multiplication,Division; Square root of array
+#As arrays are already numpy arrays so we can do operations directly
+print(x*y)
+print(np.multiply(x,y))
+print(np.divide(x,y))
+print(np.sqrt(x))
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[[ 5 12]
+ [21 32]]
+[[ 5 12]
+ [21 32]]
+[[0.2        0.33333333]
+ [0.42857143 0.5       ]]
+[[1.         1.41421356]
+ [1.73205081 2.        ]]
+
+
+
+ +
+
+ +
+
+
+
In [21]:
+
+
+
#Matric multiplications/Dot product of matrices
+x.dot(y)
+# np.dot(x,y)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[21]:
+ + + + +
+
array([[19, 22],
+       [43, 50]])
+
+ +
+ +
+
+ +
+
+
+
In [22]:
+
+
+
x
+
+ +
+
+
+ +
+
+ + +
+ +
Out[22]:
+ + + + +
+
array([[1, 2],
+       [3, 4]])
+
+ +
+ +
+
+ +
+
+
+
In [23]:
+
+
+
print(np.sum(x))# sums all the elements of a matrix
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
10
+
+
+
+ +
+
+ +
+
+
+
In [24]:
+
+
+
# optional parameter axis
+np.sum(x,axis=0)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[24]:
+ + + + +
+
array([4, 6])
+
+ +
+ +
+
+ +
+
+
+
In [25]:
+
+
+
np.sum(x,axis=1)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[25]:
+ + + + +
+
array([3, 7])
+
+ +
+ +
+
+ +
+
+
+
+

NUMPY AXES

NUMPY AXES ARE LIKE AXES IN A COORDINATE SYSTEM :)

+
+
+
+ +
+
+
+

title

+ +
+
+
+
+
+
+
    +
  • In a 2-dimensional NumPy array, the axes are the directions along the rows and columns.
  • +
  • ##### AXIS 0 IS THE DIRECTION ALONG THE ROWS
  • +
  • In a NumPy array, axis 0 is the “first” axis.
  • +
  • #### AXIS 1 IS THE DIRECTION ALONG THE COLUMNS
  • +
  • In a multi-dimensional NumPy array, axis 1 is the second axis.
  • +
+ +
+
+
+
+
+
+

Once again, keep in mind that 1-d arrays work a little differently. Technically, 1-d arrays don’t have an axis 1. +Essentially all Python sequences work like this. In any Python sequence – like a list, tuple, or string – the index starts at 0.

+

Numbering of NumPy axes essentially works the same way. They are numbered starting with 0. So the “first” axis is actually “axis 0.” The “second” axis is “axis 1,” and so on.

+ +
+
+
+
+
+
+

title

+ +
+
+
+
+
+
+

So when we set axis = 0, we’re not summing across the rows. When we set axis = 0, we’re aggregating the data such that we collapse the rows … we collapse axis 0

+ +
+
+
+
+
+
+

title

+ +
+
+
+
+
+
+

As I mentioned earlier, this confuses many beginners. They expect that by setting axis = 1, NumPy would sum down the columns, but that’s not how it works.

+

The code has the effect of summing across the columns. It collapses axis 1.

+ +
+
+
+
+
+
+

numpy.stack() in Python

- numpy.stack() function is used to join a sequence of same dimension arrays along a new axis

+
+
+
+
+
+
In [31]:
+
+
+
p=np.array([1,2,3,4])
+q=np.array([5,6,7,8])
+
+ +
+
+
+ +
+
+
+
In [32]:
+
+
+
np.stack((p,q),axis=0)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[32]:
+ + + + +
+
array([[1, 2, 3, 4],
+       [5, 6, 7, 8]])
+
+ +
+ +
+
+ +
+
+
+
In [34]:
+
+
+
print(p)
+print(q)
+
+ +
+
+
+ +
+
+ + +
+ +
+ + +
+
[1 2 3 4]
+[5 6 7 8]
+
+
+
+ +
+
+ +
+
+
+
In [3]:
+
+
+
# Returns values from Standard Normal Distribution in form of array of given dimension
+np.random.randn(2,3)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[3]:
+ + + + +
+
array([[0.74957841, 0.77790306, 0.7527411 ],
+       [0.44840246, 1.40156779, 0.25570669]])
+
+ +
+ +
+
+ +
+
+
+
In [46]:
+
+
+
#Returns integer number from [low,high)
+# Return random integers from low (inclusive) to high (exclusive).
+np.random.randint(5,10,3) # third parameter is the number of elements to be returned
+
+ +
+
+
+ +
+
+ + +
+ +
Out[46]:
+ + + + +
+
array([9, 9, 8])
+
+ +
+ +
+
+ +
+
+
+
+
numpy.random.choice

numpy.random.choice(a, size=None, replace=True, p=None)

+
    +
  • a : 1-D array-like or int

    If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a were np.arange(a)

    +
    +
  • +
  • size : int or tuple of ints, optional

    Output shape. If the given shape is, e.g., (m, n, k), then m n k samples are drawn. Default is None, in which case a single value is returned.

    +
    +
  • +
  • p : 1-D array-like, optional

    The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

    +
    +
  • +
  • Returns: samples > single item or ndarray
  • +
+ +
+
+
+
+
+
+

The random numbers are actually pseudorandom. They depend entirely on an input seed and are then generated by a deterministic algorithm from that seed.

A pseudorandom number generator's number sequence is completely determined by the seed: thus, if a pseudorandom number generator is reinitialized with the same seed, it will produce the same sequence of numbers.

+
+ +
+
+
+
+
+
+

Seeding random number generators

    +
  • we will explicitly seed the random number generator to show that we get the same random numbers.
  • +
+ +
+
+
+
+
+
In [48]:
+
+
+
# Seed the RNG
+np.random.seed(42)
+# Generate random numbers
+np.random.random(size=10)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[48]:
+ + + + +
+
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,
+       0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258])
+
+ +
+ +
+
+ +
+
+
+
In [49]:
+
+
+
# Re-seed the RNG
+np.random.seed(42)
+# Generate random numbers
+np.random.random(size=10)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[49]:
+ + + + +
+
array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,
+       0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258])
+
+ +
+ +
+
+ +
+
+
+
+

The random numbers are exactly the same. If we choose a different seed, we get totally different random numbers.

+
+ +
+
+
+
+
+
In [50]:
+
+
+
#Min,max along axis application
+
+ +
+
+
+ +
+
+
+
In [52]:
+
+
+
a=np.array([[1,2,3,4],[7,6,2,0]])
+
+ +
+
+
+ +
+
+
+
In [53]:
+
+
+
a
+
+ +
+
+
+ +
+
+ + +
+ +
Out[53]:
+ + + + +
+
array([[1, 2, 3, 4],
+       [7, 6, 2, 0]])
+
+ +
+ +
+
+ +
+
+
+
In [56]:
+
+
+
np.min(a)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[56]:
+ + + + +
+
0
+
+ +
+ +
+
+ +
+
+
+
In [58]:
+
+
+
#Specify axis for the direction 
+np.min(a,axis=0)
+np.min(a,axis=1)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[58]:
+ + + + +
+
array([1, 0])
+
+ +
+ +
+
+ +
+
+
+
In [59]:
+
+
+
c=np.array([1,2,3,4])
+
+ +
+
+
+ +
+
+
+
In [60]:
+
+
+
np.mean(c)
+
+ +
+
+
+ +
+
+ + +
+ +
Out[60]:
+ + + + +
+
2.5
+
+ +
+ +
+
+ +
+
+
+
In [62]:
+
+
+
np.average(c,weights=[1,1,1,1])
+
+ +
+
+
+ +
+
+ + +
+ +
Out[62]:
+ + + + +
+
2.5
+
+ +
+ +
+
+ +
+
+
+
+

NUMPY HSTACK

    +
  • NumPy hstack is just a function for combining together other NumPy arrays.

    +
  • +
  • Specifically, it combines together NumPy arrays in the “horizontal” direction.

    +
  • +
  • Another way of saying this is that it combines together NumPy arrays “column wise.” +title +title
  • +
+ +
+
+
+
+
+
+

TUPLE-OF-INPUT-ARRAYS (REQUIRED)

+
    +
  • The only input to np.hstack is a group of arrays, organized into a Python tuple
  • +
+ +
+
+
+
+
+
In [1]:
+
+
+
a=np.array([[2,2],[3,3]])
+
+ +
+
+
+ +
+
+
+
In [2]:
+
+
+
b=np.ones((a.shape[0],1))
+b
+
+ +
+
+
+ +
+
+ + +
+ +
Out[2]:
+ + + + +
+
array([[1.],
+       [1.]])
+
+ +
+ +
+
+ +
+
+
+
In [3]:
+
+
+
np.hstack((b,a))
+
+ +
+
+
+ +
+
+ + +
+ +
Out[3]:
+ + + + +
+
array([[1., 2., 2.],
+       [1., 3., 3.]])
+
+ +
+ +
+
+ +
+
+
+
In [ ]:
+
+
+
 
+
+ +
+
+
+ +
+
+
+ + + + + + diff --git a/machine learning basics/numpy_basics_for_ml.ipynb b/machine learning basics/numpy_basics_for_ml.ipynb new file mode 100644 index 0000000..fd80189 --- /dev/null +++ b/machine learning basics/numpy_basics_for_ml.ipynb @@ -0,0 +1,881 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tushar Goyal - IIT Mandi" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Machine Learning Basic Libraries used in Algorithms and Code Implementations\n", + "#### Images and some explainations taken from Various Medium blogs" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1]\n", + " [2]\n", + " [3]\n", + " [4]\n", + " [5]]\n", + "(5, 1)\n" + ] + } + ], + "source": [ + "a=np.array([[1],[2],[3],[4],[5]])\n", + "print(a)\n", + "print(a.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0. 0. 0.]\n", + " [0. 0. 0.]\n", + " [0. 0. 0.]]\n" + ] + } + ], + "source": [ + "b=np.zeros((3,3))#array of zeroes\n", + "print(b)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1., 1.],\n", + " [1., 1.]])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones((2,2))# 2-D array of ones of desired shape" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[5, 5],\n", + " [5, 5],\n", + " [5, 5]])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Array of some constants\n", + "np.full((3,2),5)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 0. 0. 0.]\n", + " [0. 1. 0. 0.]\n", + " [0. 0. 1. 0.]\n", + " [0. 0. 0. 1.]]\n" + ] + } + ], + "source": [ + "#Identity matrix-Size/Square matrix- Very Useful\n", + "d=np.eye(4)\n", + "print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[0.50360358 0.96281574 0.29079462]\n", + " [0.15206788 0.67035403 0.79278638]]\n" + ] + } + ], + "source": [ + "#Random Matrix\n", + "randomMatrix=np.random.random((2,3))\n", + "print(randomMatrix)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.29079462 0.79278638]\n" + ] + } + ], + "source": [ + "print(randomMatrix[:,2])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Python List Slicing\n", + "[start : stop : steps] \n", + "- which means that slicing will start from index start\n", + " will go up to stop in step of steps. \n", + " Default value of start is 0, stop is last index of list\n", + " and for step it is 1 \n", + "- Negative value of steps shows right to left traversal instead of left to right traversal that is why [: : -1] prints list in reverse order.\n", + "- [: stop] will slice list from starting till stop index and [start : ] will slice list from start index till end." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 6 8]\n", + " [10 12]]\n", + "[[ 6 8]\n", + " [10 12]]\n" + ] + } + ], + "source": [ + "#Mathematical operations\n", + "x=np.array([[1,2],[3,4]])\n", + "y=np.array([[5,6],[7,8]])\n", + "#Element wise addition\n", + "print(x+y)\n", + "print(np.add(x,y))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 5 12]\n", + " [21 32]]\n", + "[[ 5 12]\n", + " [21 32]]\n", + "[[0.2 0.33333333]\n", + " [0.42857143 0.5 ]]\n", + "[[1. 1.41421356]\n", + " [1.73205081 2. ]]\n" + ] + } + ], + "source": [ + "#Element wise Multiplication,Division; Square root of array\n", + "#As arrays are already numpy arrays so we can do operations directly\n", + "print(x*y)\n", + "print(np.multiply(x,y))\n", + "print(np.divide(x,y))\n", + "print(np.sqrt(x))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[19, 22],\n", + " [43, 50]])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Matric multiplications/Dot product of matrices\n", + "x.dot(y)\n", + "# np.dot(x,y)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [3, 4]])" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "print(np.sum(x))# sums all the elements of a matrix" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([4, 6])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# optional parameter axis\n", + "np.sum(x,axis=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([3, 7])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.sum(x,axis=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## NUMPY AXES\n", + "#### NUMPY AXES ARE LIKE AXES IN A COORDINATE SYSTEM :)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "REFER THIS https://www.sharpsightlabs.com/blog/numpy-axes-explained/" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![title](https://vrzkj25a871bpq7t1ugcgmn9-wpengine.netdna-ssl.com/wp-content/uploads/2018/11/numpy-arrays-have-axes.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- In a 2-dimensional NumPy array, the axes are the directions along the rows and columns.\n", + "- ##### AXIS 0 IS THE DIRECTION ALONG THE ROWS\n", + "- In a NumPy array, axis 0 is the “first” axis.\n", + "- #### AXIS 1 IS THE DIRECTION ALONG THE COLUMNS\n", + "- In a multi-dimensional NumPy array, axis 1 is the second axis.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once again, keep in mind that 1-d arrays work a little differently. Technically, 1-d arrays don’t have an axis 1.\n", + "Essentially all Python sequences work like this. In any Python sequence – like a list, tuple, or string – the index starts at 0.\n", + "\n", + "Numbering of NumPy axes essentially works the same way. They are numbered starting with 0. So the “first” axis is actually “axis 0.” The “second” axis is “axis 1,” and so on." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![title](https://vrzkj25a871bpq7t1ugcgmn9-wpengine.netdna-ssl.com/wp-content/uploads/2018/12/numpy-axes-np-sum-axis-0.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So when we set axis = 0, we’re not summing across the rows. When we set axis = 0, we’re aggregating the data such that we collapse the rows … we collapse axis 0" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![title](https://vrzkj25a871bpq7t1ugcgmn9-wpengine.netdna-ssl.com/wp-content/uploads/2018/11/numpy-axes-np-sum-axis-1.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As I mentioned earlier, this confuses many beginners. They expect that by setting axis = 1, NumPy would sum down the columns, but that’s not how it works.\n", + "\n", + "The code has the effect of summing across the columns. It collapses axis 1." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### numpy.stack() in Python\n", + "- numpy.stack() function is used to join a sequence of same dimension arrays along a new axis\n", + "-" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [], + "source": [ + "p=np.array([1,2,3,4])\n", + "q=np.array([5,6,7,8])" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3, 4],\n", + " [5, 6, 7, 8]])" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.stack((p,q),axis=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1 2 3 4]\n", + "[5 6 7 8]\n" + ] + } + ], + "source": [ + "print(p)\n", + "print(q)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0.74957841, 0.77790306, 0.7527411 ],\n", + " [0.44840246, 1.40156779, 0.25570669]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Returns values from Standard Normal Distribution in form of array of given dimension\n", + "np.random.randn(2,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([9, 9, 8])" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Returns integer number from [low,high)\n", + "# Return random integers from low (inclusive) to high (exclusive).\n", + "np.random.randint(5,10,3) # third parameter is the number of elements to be returned" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### numpy.random.choice\n", + "numpy.random.choice(a, size=None, replace=True, p=None)\n", + "- a : 1-D array-like or int \n", + ">If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a were np.arange(a)\n", + "- size : int or tuple of ints, optional\n", + ">Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.\n", + "- p : 1-D array-like, optional \n", + ">The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.\n", + "- Returns:\tsamples > single item or ndarray" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The random numbers are actually pseudorandom. They depend entirely on an input seed and are then generated by a deterministic algorithm from that seed.\n", + ">A pseudorandom number generator's number sequence is completely determined by the seed: thus, if a pseudorandom number generator is reinitialized with the same seed, it will produce the same sequence of numbers." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Seeding random number generators\n", + "- we will explicitly seed the random number generator to show that we get the same random numbers." + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,\n", + " 0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258])" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Seed the RNG\n", + "np.random.seed(42)\n", + "# Generate random numbers\n", + "np.random.random(size=10)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.37454012, 0.95071431, 0.73199394, 0.59865848, 0.15601864,\n", + " 0.15599452, 0.05808361, 0.86617615, 0.60111501, 0.70807258])" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Re-seed the RNG\n", + "np.random.seed(42)\n", + "# Generate random numbers\n", + "np.random.random(size=10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ">The random numbers are exactly the same. If we choose a different seed, we get totally different random numbers." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "#Min,max along axis application" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [], + "source": [ + "a=np.array([[1,2,3,4],[7,6,2,0]])" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3, 4],\n", + " [7, 6, 2, 0]])" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.min(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 0])" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Specify axis for the direction \n", + "np.min(a,axis=0)\n", + "np.min(a,axis=1)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [], + "source": [ + "c=np.array([1,2,3,4])" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.5" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.mean(c)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2.5" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.average(c,weights=[1,1,1,1])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### NUMPY HSTACK\n", + "- NumPy hstack is just a function for combining together other NumPy arrays.\n", + "\n", + "- Specifically, it combines together NumPy arrays in the “horizontal” direction.\n", + "- Another way of saying this is that it combines together NumPy arrays __“column wise.”__\n", + "![title](https://vrzkj25a871bpq7t1ugcgmn9-wpengine.netdna-ssl.com/wp-content/uploads/2019/06/numpy-hstack-visual-example_v2.png)\n", + "![title](https://vrzkj25a871bpq7t1ugcgmn9-wpengine.netdna-ssl.com/wp-content/uploads/2019/06/numpy-hstack-syntax-explanation-1024x589.png)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "__TUPLE-OF-INPUT-ARRAYS (REQUIRED)__\n", + "- The only input to np.hstack is a group of arrays, organized into a Python tuple" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "a=np.array([[2,2],[3,3]])" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1.],\n", + " [1.]])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b=np.ones((a.shape[0],1))\n", + "b" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1., 2., 2.],\n", + " [1., 3., 3.]])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.hstack((b,a))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + }, + "varInspector": { + "cols": { + "lenName": 16, + "lenType": 16, + "lenVar": 40 + }, + "kernels_config": { + "python": { + "delete_cmd_postfix": "", + "delete_cmd_prefix": "del ", + "library": "var_list.py", + "varRefreshCmd": "print(var_dic_list())" + }, + "r": { + "delete_cmd_postfix": ") ", + "delete_cmd_prefix": "rm(", + "library": "var_list.r", + "varRefreshCmd": "cat(var_dic_list()) " + } + }, + "types_to_exclude": [ + "module", + "function", + "builtin_function_or_method", + "instance", + "_Feature" + ], + "window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}