Harsh Dhamecha
Harsh Dhamecha

@HarshDhamecha3

29 Tweets 2 reads May 17, 2022
NumPy for Data Science ๐Ÿ“Š ๐Ÿ”ฅ
A Mega Thread ๐Ÿงต๐Ÿ‘‡
NumPy stands for Numerical Python that is one of the most fundamental libraries in Python. You must have a solid grasp of NumPy to be a better Data Scientist.
So, let's get started!
๐ŸŸ  Creating Arrays
๐Ÿ”ถ Basic ndarrays
๐Ÿ”น np.array()
- Create an array.
- You can also specify the data type.
๐Ÿ”ถ Evenly Spaced ndarrays
๐Ÿ”น np.arange(start, end, step)
- Create an array between two numbers(start and end) with a given step size.
- Interval: [start, end) --> Excludes last number
๐Ÿ”น np.linspace(start, end, num)
- Create an array of total num elements between two numbers(start and end).
- Interval: [start, end] --> Includes last number
๐Ÿ”ถ Miscellaneous
๐Ÿ”น np.zeros(shape)
- Create an array of all 0s.
๐Ÿ”น np.ones(shape)
- Create an array of all 1s.
๐Ÿ”น np.eye(shape)
- Create an Identity matrix.
๐Ÿ”น np.random.rand(shape)
- Create an array with random values.
๐ŸŸ  Reshaping Arrays
๐Ÿ”น .shape
- Return shape of an array.
๐Ÿ”น .ndim
- Return dimension of an array. (1, 2, 3, etc.)
๐Ÿ”น .size
- Return the size of an array.
- Size is the total number of elements in an array.
Note: The above three are attributes and not the methods.
๐Ÿ”น np.reshape(a, new_shape)
- Changes the shape of an array without changing the data.
๐ŸŸ  Flattening Arrays
๐Ÿ”น np.flatten()
- Flattens an array (Convert Multidimensional arrays into single-dimensional)
๐Ÿ”น np.ravel()
- Flattens an array (Convert Multidimensional arrays into single-dimensional)
๐Ÿ”ถ Difference between flatten() and ravel()
- flatten() returns a copy of the original array and ravel() returns a reference to the original array. Any changes made to the array returned from ravel() will be reflected in the original array but this will not be true for flatten()
๐ŸŸ  Transposing Arrays
๐Ÿ”น .T
- Return transposed array (Swaps rows and columns).
- It also changes the shape.
๐ŸŸ  Squeezing and Expanding
๐Ÿ”น np.expand_dims()
- Add a new axis to an array along the specified axis.
๐Ÿ”น np.squeeze()
- Remove axis of length one from an array.
๐ŸŸ  Slicing and Indexing
๐Ÿ”ถ 1D arrays
- Work in a pretty similar fashion to the list indexing and slicing.
๐Ÿ”ถ 2D arrays
- It is an array of arrays.
๐Ÿ”ถ 3D arrays
- It can be thought of as an n 2-dimensional array.
๐ŸŸ  Stacking and Concatenating
๐Ÿ”น np.vstack()
- Combines arrays vertically and thus increases the total rows.
๐Ÿ”น np.hstack()
- Combines arrays horizontally and thus increases the total columns.
๐Ÿ”น np.concatenate()
- Concatenate arrays along the specified axis.
๐ŸŸ  Broadcasting
- It lets you perform arithmetics operations between arrays of different sizes or between an array and a scaler number! woohoo!
- Hypothetically, it stretches a smaller array to match the shape of a larger array.
๐ŸŸ  Maths with NumPy
๐Ÿ”ถ Basic Arithmetic Operations
- With the power of Broadcasting, you can perform all the basic arithmetic operations as shown below.
๐Ÿ”ถ Statistical
๐Ÿ”น np.mean(a, axis)
- Return the mean of an array along the specified axis.
๐Ÿ”น np.median(a, axis)
- Return the median of an array along the specified axis.
๐Ÿ”น np.std(a, axis)
- Return the standard deviation of an array along the specified axis.
๐Ÿ”น np.min(a, axis)
- Return the minimum value of an array along the specified axis.
๐Ÿ”น np.max(a, axis)
- Return the maximum value of an array along the specified axis.
๐Ÿ”น np.argmin(a, axis)
- Return an index of the minimum value of an array along the specified axis.
๐Ÿ”น np.argmax(a, axis)
- Return an index of the maximum value of an array along the specified axis.
Did I miss something important?
Let me know in the comments section below ๐Ÿ‘‡
Enjoyed the thread? Make sure to
1. Follow me @HarshDhamecha3
2. Retweet this.
3. Turn on notification to never miss out on threads on Python, Machine Learning, and Data Science.
Have a great day, See you soon.

Loading suggestions...