<jupyter_start><jupyter_text>__Shape Manipulation__: Is nothing but changing the shape or dimension of an array.<jupyter_code>#1.reshape().
import numpy as np
a = np.arange(1, 13).reshape(4, 3)
print(a)<jupyter_output>[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]<jupyter_text>__Note__: Above function does not affect the original array. And this function also converts single array vector to multidimensional array.<jupyter_code>#2.resize().
import numpy as np
a = np.arange(12)
a.resize(3, 4)
print(a)<jupyter_output>[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]<jupyter_text>__Note__: Vector 'a' gets converted to multi-dimensional array. And original array gets modified. __Review__: reshape(): Is used to change the structure of data from 1D to ND where N is (2, 3, 4, ...) without affecting the original array.resize(): Is used to change the structure of data from 1D to ND where N is (2, 3, 4, ...) and will affect the original array.ravel(): Used to change the structure of data from ND to 1D. And this function affects the original data.<jupyter_code>#3.ravel():example for ravel function.
import numpy as np
a = np.arange(1, 13).reshape(3, 4)
b = a.ravel()
print(b)
print(a)
#few important examples for reshape, resize and ravel.
import numpy as np
a = np.arange(1, 13)
b = a.reshape(4, 3)
print(a)
print(b)
import numpy as np
a = np.arange(1, 13)
a.resize(3, 4)
print(a)
import numpy as np
a = np.arange(1, 13).reshape(3, 4)
np.ravel(a)
print(a)
import numpy as np
a = np.floor(10*np.random.random(10))
print(a)
print(a.shape)
print(a.ravel())
print(a.reshape(2, 5))
print(a.resize(5, 2))
#4.flatten()-This function is also used to convert Multidimensional array to 1D(same as ravel function but will not affect the original data).
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.flatten())
print(a)<jupyter_output>[1 2 3 4]
[[1 2]
 [3 4]]<jupyter_text>Note: 'C' argument specified inside the paranthesis refer rowise flattening.'F' argument specified inside the paranthesis refer to columnwise flattening.Whereas default argument is 'C'<jupyter_code>#example demonstrating the above note.
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.flatten('C'))
print(a.flatten('A'))
print(a.flatten('K'))
print(a.flatten('F'))
print(a)<jupyter_output>[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 3 2 4]
[[1 2]
 [3 4]]
{'fraction_non_alphanumeric': 0.1356223175965665, 'fraction_numerical': 0.04978540772532189, 'mean_word_length': 4.6440677966101696, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '641321', 'license': 'Apache-2.0', 'n_tokens_mistral': 950, 'n_tokens_neox': 857, 'n_words': 319, 'path': 'Shape_Manipulations.ipynb', 'repo_name': 'Raghu010/Learn-Numpy-Numerical-Python-', 'size': '2330'}