Member-only story

Mastering Numpy: 100 Tips with Python One-liners

btd
3 min readDec 6, 2023

--

Here are 100 tips and tricks for using NumPy, a powerful library for numerical computing in Python:

Array Creation and Manipulation:

  1. Create an array: numpy.array([1, 2, 3])
  2. Create a range: numpy.arange(start, stop, step)
  3. Create evenly spaced values: numpy.linspace(start, stop, num)
  4. Create an array filled with zeros: numpy.zeros(shape)
  5. Create an array filled with ones: numpy.ones(shape)
  6. Create an identity matrix: numpy.eye(n)
  7. Reshape an array: array.reshape(new_shape)
  8. Flatten an array: array.flatten()
  9. Transpose an array: array.T
  10. Concatenate arrays horizontally: numpy.hstack((array1, array2))
  11. Concatenate arrays vertically: numpy.vstack((array1, array2))
  12. Split an array: numpy.split(array, indices_or_sections)

Array Indexing and Slicing:

  1. Access an element: array[i]
  2. Slice an array: array[start:stop:step]
  3. Access the last element: array[-1]
  4. Access multiple elements: array[[1, 3, 5]]

--

--

btd
btd

No responses yet