pycharmers.utils.numpy_utils module¶
-
pycharmers.utils.numpy_utils.
take_centers
(a)[source]¶ Take a center values.
- Parameters
a (array) – Array
Example
>>> from pycharmers.utils import take_centers >>> take_centers([0,1,2,3,4,5]) array([0.5, 1.5, 2.5, 3.5, 4.5]) >>> take_centers([[0,1,2,3,4,5],[1,2,3,4,5,6]]) array([[0.5, 1.5, 2.5, 3.5, 4.5], [1.5, 2.5, 3.5, 4.5, 5.5]])
-
pycharmers.utils.numpy_utils.
confusion_matrix
(y_true, y_pred)[source]¶ Compute confusion matrix to evaluate the accuracy of a classification.
By definition a confusion matrix \(C\) is such that \(C_{i, j}\) is equal to the number of observations known to be in group \(i\) and predicted to be in group \(j\).
Thus in binary classification, the count of true negatives is \(C_{0,0}\), false negatives is \(C_{1,0}\), true positives is \(C_{1,1}\) and false positives is \(C_{0,1}\).
- Parameters
y_true (array) – Ground truth (correct) target values.
y_pred (array) – Estimated targets as returned by a classifier.
- Returns
- Confusion matrix whose i-th row and j-th column entry indicates
the number of samples with true label being i-th class and prediced label being j-th class.
- Return type
cm (array)
References
Wikipedia entry for the Confusion matrix (Wikipedia and other references may use a different convention for axes)
Examples
>>> from pycharmers import confusion_matrix >>> y_true = [2, 0, 2, 2, 0, 1] >>> y_pred = [0, 0, 2, 2, 0, 2] >>> confusion_matrix(y_true, y_pred) array([[2, 0, 0], [0, 0, 1], [1, 0, 2]]) >>> # In the binary case, we can extract true positives, etc as follows: >>> tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel() >>> (tn, fp, fn, tp) (0, 2, 1, 1)
-
pycharmers.utils.numpy_utils.
rotate2d
(a, theta)[source]¶ Rotate 2d vectors using Rotation matrix \(R(\theta)\)
\[\begin{split}R(\theta) = \left( \begin{array}{c} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{array} \right)\end{split}\]Using this matrix, the rotation of the vector can be expressed as follows.
\[\begin{split}\left( \begin{array}{c} x^{\prime} \\ y^{\prime} \end{array} \right) = \left( \begin{array}{c} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{array} \right)\left( \begin{array}{c} x \\ y \end{array} \right)\end{split}\]- Parameters
a (ndarray) – Array
theta (float) – float value like 1/2*np.pi
-
pycharmers.utils.numpy_utils.
replaceArray
(a, old=255, 255, 255, new=0, 0, 0)[source]¶ Replace an Array from
old
tonew
- Parameters
old (tuple) – Old value.
new (tuple) – New value.
- Returns
New Array
- Return type
np.ndarray
Examples
>>> import cv2 >>> import numpy as np >>> from pycharmers.opencv import SAMPLE_LENA_IMG, cv2plot >>> from pycharmers.utils import replaceArray >>> img = cv2.imread(SAMPLE_LENA_IMG) >>> img = replaceArray(img, old=[77, 66, 176], new=[0,0,0]).astype(np.uint8) >>> cv2plot(img, is_cv2=True)
-
pycharmers.utils.numpy_utils.
fill_between_angle
(arr, s, e, center=None, is_radian=True)[source]¶ Fill Between
s
ande
.- Parameters
arr (np.ndarray) – Input array.
s (Number) – Start angle of fill.
e (Number) – End angle of fill.
center (tuple) – Center coordinates.
is_radian (bool, optional) – whether
s
ande
are defined in radians.
- Returns
Whether it is a place to be filled.
- Return type
np.ndarray
Examples
>>> import numpy as np >>> from PIL import Image >>> from pycharmers.utils import fill_between_angle >>> arr = np.zeros(shape=(100,100,3)).astype(np.uint8) >>> flag = fill_between_angle(arr, s=30, e=120, is_radian=False) >>> Image.fromarray(np.where(flag, arr, 255))