<jupyter_start><jupyter_text>Numpy ufuncs A universal function is a function that operates on ndarrays in an element-by-element fashion.  A ufunc is a “vectorized” wrapper for a function that takes a fixed number of specific inputs and produces a fixed number of specific outputs.<jupyter_code>from typing import Iterable

import numpy as np
np.random.seed(0)<jupyter_output><empty_output><jupyter_text>Native Python Loops<jupyter_code>def reciprocal(values: Iterable[float]) -> Iterable[float]:
    output = np.empty(len(values))
    for i in range(len(values)):
        output[i] = 1.0 / values[i]
    return output
small_array = np.random.randint(low=1, high=10, size=5)
print(reciprocal(small_array))<jupyter_output>[0.16666667 1.         0.25       0.25       0.125     ]<jupyter_text>| Multiple of a second | Unit | Symbol | Definition | Comparative examples & common units ||-|-|-|-|-| | 10−9 | 1 nanosecond | ns | One billionth of one second | 1 ns: Time to execute one machine cycle by a 1 GHz microprocessor 1 ns: Light travels 30 cm || 10−6 | 1 microsecond | µs | One millionth of one second | 1 µs: Time to execute one machine cycle by an Intel 80186 microprocessor 2.2 µs: Lifetime of a muon 4–16 µs: Time to execute one machine cycle by a 1960s minicomputer || 10−3 | 1 millisecond | ms | One thousandth of one second | 1 ms: time for a neuron in human brain to fire one impulse and return to rest 4–8 ms: typical seek time for a computer hard disk |<jupyter_code>small_array = np.random.randint(low=1, high=10, size=5)

%timeit reciprocal(small_array)
big_array = np.random.randint(low=1, high=10, size=100_000)

%timeit reciprocal(big_array)
200*1000 / 11.8<jupyter_output><empty_output><jupyter_text>Introducing UFuncs<jupyter_code>print(reciprocal(small_array))
print(1.0 / small_array)
%timeit (1.0 / big_array)
%timeit np.reciprocal(big_array)
200*1000 / 103  # Speedup
x = np.arange(4)

print(x)
print(x + 2)
print(x - 2)
print(x * 2)
print(x / 2)
print(x + 2.0)
print(x - 2.0)
print(x * 2.0)
print(x / 2.0)
print(np.add(x, 2))<jupyter_output>[2 3 4 5]<jupyter_text>| Name | Description ||-|-|| add | Add arguments element-wise. || subtract | Subtract arguments, element-wise. || multiply | Multiply arguments element-wise. || matmul | Matrix product of two arrays. || divide | Returns a true division of the inputs, element-wise. || negative | Numerical negative, element-wise. || positive | Numerical positive, element-wise. || power | First array elements raised to powers from second array, element-wise. || mod | Return element-wise remainder of division. || absolute | Calculate the absolute value element-wise. || fabs | Compute the absolute values element-wise. || sign | Returns an element-wise indication of the sign of a number. || exp | Calculate the exponential of all elements in the input array. || log | Natural logarithm, element-wise. || sqrt | Return the non-negative square-root of an array, element-wise. || square | Return the element-wise square of the input. || reciprocal | Return the reciprocal of the argument, element-wise. || gcd | Returns the greatest common divisor of \|x1\| and \|x2\| || lcm | Returns the lowest common multiple of \|x1\| and \|x2\| |<jupyter_code>x = np.array([-2, -1, 0, 1, 2])
abs(x)
print(np.abs(x))
print(np.fabs(x))
x = np.array([-2, -1, 0, 1, 2], dtype=np.float32)

print(np.abs(x))
print(np.fabs(x))<jupyter_output>[2. 1. 0. 1. 2.]
[2. 1. 0. 1. 2.]<jupyter_text>Trigonometric functions | Name | Description ||-|-|| sin | Trigonometric sine, element-wise. || cos | Cosine element-wise. || tan | Compute tangent element-wise. |<jupyter_code>theta = np.linspace(0, 2.0 * np.pi, 7)

print(theta)
print(np.sin(theta))
print(np.cos(theta))<jupyter_output>[ 0.00000000e+00  8.66025404e-01  8.66025404e-01  1.22464680e-16
 -8.66025404e-01 -8.66025404e-01 -2.44929360e-16]
[ 1.   0.5 -0.5 -1.  -0.5  0.5  1. ]<jupyter_text>Bit-twiddling functions | Name | Description ||-|-|| bitwise_and | Compute the bit-wise AND of two arrays element-wise. || bitwise_or | Compute the bit-wise OR of two arrays element-wise. || bitwise_xor | Compute the bit-wise XOR of two arrays element-wise. |<jupyter_code>print(np.bitwise_and(14, 13))
print(np.binary_repr(12))
print(np.bitwise_and([14, 3], 13))<jupyter_output>12
1100
[12  1]<jupyter_text>Comparison functions | Name | Description ||-|-|| greater | Return the truth value of (x1 > x2) element-wise. || greater_equal | Return the truth value of (x1 >= x2) element-wise. || less | Return the truth value of (x1 < x2) element-wise. || less_equal | Return the truth value of (x1 <= x2) element-wise. || not_equal | Return (x1 != x2) element-wise. || equal | Return (x1 == x2) element-wise. |<jupyter_code>print(np.greater([4, 2], [2, 2]))
a = np.array([4, 2])
b = np.array([2, 2])

print(a > b)<jupyter_output>[ True False]
{'fraction_non_alphanumeric': 0.1355826950941834, 'fraction_numerical': 0.05692403229145104, 'mean_word_length': 4.304061470911087, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '496549', 'license': 'MIT', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1651, 'n_words': 542, 'path': '2_Numpy/03_Ufuncs.ipynb', 'repo_name': 'tkaya94/UdemyDataScience', 'size': '4831'}