My Book of Python Computing by Kar Gupta Abhijit & Kar Gupta Abhijit

My Book of Python Computing by Kar Gupta Abhijit & Kar Gupta Abhijit

Author:Kar Gupta, Abhijit & Kar Gupta, Abhijit [Kar Gupta, Abhijit]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2021-04-25T16:00:00+00:00


The centroid is also shown at (2, 1.666 …). [The plotting can be done with Polygon(points) or with plot(x, y) function frommatplotlib.pyplot module.]

# Variance, Standard deviation, Peak-to-Peak

>>> x = np.array([0, 1, -1, 2, 3,

-2, 0.5, 1.5, 2.5])

>>> x.var()

2.388888888888889

>>> x.std()

1.5456030825826172

# Peak-to-Peak value: Highest - Lowest >>> x.ptp()

5.0

>>> X = x.reshape(3, 3)

>>> X

array([[ 0. , 1. , -1. ],

[ 2. , 3. , -2. ],

[ 0.5, 1.5, 2.5]])

>>> X.var(0) # Along axis = 0 array([0.72222222, 0.72222222, 3.72222222]) >>> X.var(1) # Along axis = 1 array([0.66666667, 4.66666667, 0.66666667])

>>> X.std(0)

array([0.84983659, 0.84983659,

1.92930615]) >>> X.std(1)

array([0.81649658, 2.1602469 ,

0.81649658])

>>> X.ptp(0)

array([2. , 2. , 4.5]) >>> X.ptp(1)

array([2., 5., 2.]) # Cov, Correlation Coefficient

>>> x = np.random.uniform(0, 1, 1000)

>>> y = np.random.uniform(0, 1, 1000)

# Covariant Matrix

>>> np.cov(x, y)

array([[0.08178297, 0.00351387], [0.00351387, 0.083107 ]])

# Correlation Coefficient Matrix >>> np.corrcoef(x, y)

array([[1., 0.04262211],

[0.04262211, 1. ]])

Note: Two sets of 1000 random data points are taken. Correlation and Covariance are seen among them. The Correlation Coefficient Matrix is calculated with Pearson product-moment correlation coefficients between two sets of data.

8.2.10 Product, Difference

>>> x = np.array([1, 2, -1, 3, -2])

# Product of elements >>> np.prod(x)

12

# Essentially, factorial of 5 >>> np.prod(range(1, 6)) 120

# Diff between successive elements >>> np.diff(x)

array([1, -3, 4, -5])

Note: The difference array can be obtained through slicing also.

>>> x = np.array([1, 2, -1, 3, -2])

>>> x[1:] - x[:-1]

array([ 1, -3, 4, -5])

Note: For multidimensional arrays, the axis reference in prod() ordiff() is similar to that of any other NumPy methods. The same is true for other methods like np.add() and no.divide() for addition and divisions. NumPy arrays are vectorized. Element wise mathematical operations between NumPy arrays can be done with mathematical operators like +, -, *, /, ** and so on. But the NumPy methods are particularly useful because we can give axis reference and manipulate through other keyword arguments.

Application:

Consider the following �� vs. �� data [A sigmoidal curve created through NumPy function and array, for demonstration.]. The first derivative, �� = ����/���� is computed through np.diff() .

# Python Script

x = np.linspace(-5, 5, 100) y = 1.0/(1 + np.exp(-x)) z = np.diff(y)/np.diff(x)

# For plotting

import matplotlib.pyplot as plt

plt.subplot(211) plt.plot(x, y)

plt.subplot(212)

plt.plot(x[:-1], z)

plt.show()



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.