<jupyter_start><jupyter_text>Remove rows with NaN's from numpy array<jupyter_code>a = np.array([[1, 8], [2,9], [3,10], [4, np.NaN], [5, 12], [6, np.NaN]])
print('Original array:\n', a)
a = a[~np.isnan(a)[:, 1]]
print('With rows containing NaN removed:\n', a)<jupyter_output>Original array:
 [[ 1.  8.]
 [ 2.  9.]
 [ 3. 10.]
 [ 4. nan]
 [ 5. 12.]
 [ 6. nan]]
With rows containing NaN removed:
 [[ 1.  8.]
 [ 2.  9.]
 [ 3. 10.]
 [ 5. 12.]]<jupyter_text>Find index of nearest value in an arraySee Nov. 20, 2014 answer to [Finding the nearest value and return the index of array in Python](http://stackoverflow.com/questions/8914491/finding-the-nearest-value-and-return-the-index-of-array-in-python)<jupyter_code>def get_index(array, value):
    idx = (np.abs(array - value)).argmin()
    return idx

a = np.linspace(0., 10., 11)
print(a)
values = [1.2, 5.1, 5.6]
for value in values:
    idx = get_index(a, value)
    print(value, idx, a[idx])<jupyter_output>[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
1.2 1 1.0
5.1 5 5.0
5.6 6 6.0<jupyter_text>Boolean indexing<jupyter_code>a = np.random.randint(0, 100, 10)
a
# Select elements greater than 60
a[a>60]
# Select elements greater than 60 and odd
a[(a>60) & (a%2 == 1)]
a.mean()
a.std()
# Select all of the even numbers greater than the mean
a[a>a.mean()]
# Select all numbers that are within one standard deviation of the mean
a[(a<(a.mean() + a.std())) & (a>(a.mean() - a.std()))]<jupyter_output><empty_output><jupyter_text>Clip values in 1D and 2D arrays<jupyter_code>a = np.array([1.0, 0.1, 1.e-3, 0.0, -1.e-3, -0.1, -1.0])
print(a)
a = a.clip(min=0)
print(a)
b = np.array([[0, 1.0], [1, 0.1], [1, 1.e-3], [3, 0.0], [4, -1.e-3], [5, -0.1], [6, -1.0]])
print(b)
print()
b[:,1] = b[:,1].clip(min=0)
print(b)<jupyter_output>[[ 0.e+00  1.e+00]
 [ 1.e+00  1.e-01]
 [ 1.e+00  1.e-03]
 [ 3.e+00  0.e+00]
 [ 4.e+00 -1.e-03]
 [ 5.e+00 -1.e-01]
 [ 6.e+00 -1.e+00]]

[[0.e+00 1.e+00]
 [1.e+00 1.e-01]
 [1.e+00 1.e-03]
 [3.e+00 0.e+00]
 [4.e+00 0.e+00]
 [5.e+00 0.e+00]
 [6.e+00 0.e+00]]<jupyter_text>Slice objects[How can I create a slice object for Numpy array?](https://stackoverflow.com/questions/38917173/how-can-i-create-a-slice-object-for-numpy-array)  [numpy.s_](https://docs.scipy.org/doc/numpy/reference/generated/numpy.s_.html) Use to give slices meaningful names<jupyter_code>a = np.arange(20).reshape(4, 5)

middle = np.s_[1:3, 1:4]
lowerright = np.s_[2:, 3:]
row2everyother = np.s_[2, 0::2]
lastrow = np.s_[-1, :]

print(a)
print("middle:")
print(a[middle])
print("lowerright:")
print(a[lowerright])
print("row2everyother:")
print(a[row2everyother])
print("lastrow:")
print(a[lastrow])
print(middle)
print(row2everyother)
print(lastrow)<jupyter_output>(slice(1, 3, None), slice(1, 4, None))
(2, slice(0, None, 2))
(-1, slice(None, None, None))<jupyter_text>Accessing indices in slice objects<jupyter_code>print(middle)
xslice, yslice = middle[1], middle[0]
print(xslice, yslice)
print(xslice.start, xslice.stop, xslice.step)
print(yslice.start, yslice.stop, yslice.step)<jupyter_output>(slice(1, 3, None), slice(1, 4, None))
slice(1, 4, None) slice(1, 3, None)
1 4 None
1 3 None<jupyter_text>Get name of numpy variableSee https://stackoverflow.com/questions/34980833/python-name-of-np-array-variable-as-string<jupyter_code>def namestr(obj, namespace):
    return [name for name in namespace if namespace[name] is obj][0]
temp_1D_array = np.linspace(0, 1, 101)

namestr(temp_1D_array, globals())<jupyter_output><empty_output><jupyter_text>From articleSee [Top 4 Numpy Functions You Don’t Know About (Probably)](https://towardsdatascience.com/top-4-numpy-functions-you-dont-know-about-probably-28fcd5d7174f)  Wherewhere() function will return the index of elements from an array that satisfy a certain condition<jupyter_code>grades = np.array([1, 3, 4, 2, 5, 5])
np.where(grades > 3)<jupyter_output><empty_output><jupyter_text>Replace values that do and don't satisfy the given condition<jupyter_code>np.where(grades > 3, 'gt3', 'lt3')<jupyter_output><empty_output><jupyter_text>argmin(), argmax(), argsort()  allclose()*It will return True if items in two arrays are equal within a tolerance. It will provide you with a great way of checking if two arrays are similar*<jupyter_code>arr1 = np.array([0.15, 0.20, 0.25, 0.17])
arr2 = np.array([0.14, 0.21, 0.27, 0.15])

np.allclose(arr1, arr2, 0.1)
np.allclose(arr1, arr2, 0.2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18947129566598594, 'fraction_numerical': 0.07805763557975948, 'mean_word_length': 4.579746835443038, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 42, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '485810', 'license': 'MIT', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1877, 'n_words': 443, 'path': 'python/numpy_notes.ipynb', 'repo_name': 'gregnordin/python-notes_to_self', 'size': '4407'}