re.sub('[^A-Za-z0-9-_ ]+', '', inp_string)
b = a
a[:] = [col.lower() for col in a]
print(b)
from itertools import combinations
list(combinations(range(4), 2)) # combinations(iterable, r) # n C r
# output [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
max(X_train, key=len) # key is a one argument function
student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
sorted(student_tuples, key=lambda student: student[2]) # sort by age
# output: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
# using operator module in python to extend sorting features
from operator import itemgetter, attrgetter # attrgetter can be used to access named attributes of an instance (e.g. of a class)
sorted(student_tuples, key=itemgetter(2)) # sorting by the item at index 2
# output: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
sorted(student_tuples, key=itemgetter(1,2)) # sorting by item at index 1 and then index 2
# output: [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
sorted(student_tuples, key=itemgetter(2), reverse=True)
# output: [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
x = np.arange(9)
x
# array([0, 1, 2, 3, 4, 5, 6, 7, 8])
y = x.reshape(3, 3)
y
# array([[0, 1, 2],
# [3, 4, 5],
# [6, 7, 8]])
y.base # .reshape() creates a view
# array([0, 1, 2, 3, 4, 5, 6, 7, 8])
z = y[[2, 1]]
z
# array([[6, 7, 8],
# [3, 4, 5]])
z.base is None # advanced indexing creates a copy
# True
Reshape array. Useful to convert 1D array into a 2D array
a = np.arange(5)
print("Current shape {}".format(a.shape))
print(a.reshape(-1, 1).shape) # -1 is the unknown dimension, it means as many rows as there are elements in the array
print(a.reshape(1, -1).shape) # 1 row, as many columns as there are elements in the array
Check if two arrays are element-wise equal with a tolerance
# np.allcose in an assert statement
assert np.allclose(avg, [0.1666667, 2.0]), "Debug the last example"
Random number generator
# generates array of random numbers with shape (d0, d1, d2) from a normal distribution
np.random.randn(d0, d1, d2)
# generates a single floating point
np.random.randn()