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 @@ + + +
+ +import numpy as np
+a=np.array([[1],[2],[3],[4],[5]])
+print(a)
+print(a.shape)
+b=np.zeros((3,3))#array of zeroes
+print(b)
+np.ones((2,2))# 2-D array of ones of desired shape
+#Array of some constants
+np.full((3,2),5)
+#Identity matrix-Size/Square matrix- Very Useful
+d=np.eye(4)
+print(d)
+#Random Matrix
+randomMatrix=np.random.random((2,3))
+print(randomMatrix)
+print(randomMatrix[:,2])
+[start : stop : steps]
+#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))
+#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))
+#Matric multiplications/Dot product of matrices
+x.dot(y)
+# np.dot(x,y)
+x
+print(np.sum(x))# sums all the elements of a matrix
+# optional parameter axis
+np.sum(x,axis=0)
+np.sum(x,axis=1)
+REFER THIS https://www.sharpsightlabs.com/blog/numpy-axes-explained/
+ +
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.
+ +
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
+ +
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.
+ +p=np.array([1,2,3,4])
+q=np.array([5,6,7,8])
+np.stack((p,q),axis=0)
+print(p)
+print(q)
+# Returns values from Standard Normal Distribution in form of array of given dimension
+np.random.randn(2,3)
+#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
+numpy.random.choice(a, size=None, replace=True, p=None)
++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)
+
+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.
+
+The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.
+
+ +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.
+
# Seed the RNG
+np.random.seed(42)
+# Generate random numbers
+np.random.random(size=10)
+# Re-seed the RNG
+np.random.seed(42)
+# Generate random numbers
+np.random.random(size=10)
++ +The random numbers are exactly the same. If we choose a different seed, we get totally different random numbers.
+
#Min,max along axis application
+a=np.array([[1,2,3,4],[7,6,2,0]])
+a
+np.min(a)
+#Specify axis for the direction
+np.min(a,axis=0)
+np.min(a,axis=1)
+c=np.array([1,2,3,4])
+np.mean(c)
+np.average(c,weights=[1,1,1,1])
+NumPy hstack is just a function for combining together other NumPy arrays.
+Specifically, it combines together NumPy arrays in the “horizontal” direction.
+
+
TUPLE-OF-INPUT-ARRAYS (REQUIRED)
+a=np.array([[2,2],[3,3]])
+b=np.ones((a.shape[0],1))
+b
+np.hstack((b,a))
+
+