#NumPy #NumPyTutorial #PythonForBeginners #NumPyInTelugu #PythonTutorialTelugu #NumPyBasics #PythonProgramming #PythonNumPy #TeluguPythonTutorial #PythonCodingInTelugu #NumPyPart5 #FinalPartNumPy #PythonLearning #NumPyForBeginners #DataScienceWithPython
====================================================
numpy part1 - • NumPy Part-1 ||in telugu|| NumPy Tuto...
numpy part2 - • NumPy Part-2 ||in telugu|| NumPy Tuto...
======================================================
1. Reshaping Arrays
Reshaping arrays means changing the shape or dimensions of an array without altering its data. This is useful when you need to adjust the structure of your data for different types of analysis or visualization.
*Example:*
```python
import numpy as np
array = np.array([1, 2, 3, 4, 5, 6])
reshaped_array = array.reshape(2, 3) # Reshaping to 2 rows and 3 columns
print(reshaped_array)
```
*Output:*
```
[[1 2 3]
[4 5 6]]
```
2. Flattening Arrays
Flattening an array means converting a multi-dimensional array into a one-dimensional array. This is useful for simplifying the structure of the data when only a single list of elements is needed.
*Example:*
```python
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
flattened_array = array.flatten()
print(flattened_array)
```
*Output:*
```
[1 2 3 4 5 6]
```
3. Transposing Arrays
Transposing an array means interchanging its rows and columns. This is particularly useful in linear algebra and data analysis to align data correctly.
*Example:*
```python
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
transposed_array = array.T
print(transposed_array)
```
*Output:*
```
[[1 4]
[2 5]
[3 6]]
```
4. Concatenating Arrays Vertically and Horizontally
Concatenating arrays means joining them along an existing axis. You can concatenate arrays vertically (stacking them row-wise) or horizontally (stacking them column-wise).
*Example (Vertical Concatenation):*
```python
import numpy as np
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])
vertically_concatenated = np.vstack((array1, array2))
print(vertically_concatenated)
```
*Output:*
```
[[1 2]
[3 4]
[5 6]
[7 8]]
```
*Example (Horizontal Concatenation):*
```python
import numpy as np
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])
horizontally_concatenated = np.hstack((array1, array2))
print(horizontally_concatenated)
```
*Output:*
```
[[1 2 5 6]
[3 4 7 8]]
```
5. Splitting Arrays
Splitting an array means dividing it into multiple sub-arrays. This can be useful for partitioning data into smaller chunks.
*Example:*
```python
import numpy as np
array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
split_array = np.hsplit(array, 2) # Splitting horizontally into 2 sub-arrays
for sub_array in split_array:
print(sub_array)
```
*Output:*
```
[[1 2]
[5 6]]
[[3 4]
[7 8]]
```
6. Flipping Arrays
Flipping an array means reversing the order of elements along a specified axis. This can be done vertically (up-down) or horizontally (left-right).
*Example (Vertical Flip):*
```python
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
flipped_array = np.flipud(array)
print(flipped_array)
```
*Output:*
```
[[4 5 6]
[1 2 3]]
```
*Example (Horizontal Flip):*
```python
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
flipped_array = np.fliplr(array)
print(flipped_array)
```
*Output:*
```
[[3 2 1]
[6 5 4]]
```
These array manipulation techniques are fundamental in data preprocessing and transformation, enabling you to organize and analyze data efficiently.