<jupyter_start><jupyter_text>Reshaping arrays<jupyter_code>Reshaping means changing the shape of an array.
The shape of an array is the number of elements in each dimension.
By reshaping we can add or remove dimensions or change number of elements in each dimension.<jupyter_output><empty_output><jupyter_text>Reshape From 1-D to 2-D Convert the following 1-D array with 12 elements into a 2-D array.The outermost dimension will have 4 arrays, each with 3 elements:<jupyter_code>import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = arr.reshape(4, 3)

print(newarr)<jupyter_output>[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]<jupyter_text>Reshape From 1-D to 3-D Convert the following 1-D array with 12 elements into a 3-D array.The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements:<jupyter_code>import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

newarr = arr.reshape(2, 3, 2)

print(newarr)<jupyter_output>[[[ 1  2]
  [ 3  4]
  [ 5  6]]

 [[ 7  8]
  [ 9 10]
  [11 12]]]<jupyter_text>Can We Reshape Into any Shape?<jupyter_code>Yes, as long as the elements required for reshaping are equal in both shapes.
We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.<jupyter_output><empty_output><jupyter_text>Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension (will raise an error):<jupyter_code>import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

newarr = arr.reshape(3, 3)

print(newarr)<jupyter_output><empty_output><jupyter_text>Returns Copy or View? Check if the returned array is a copy or a view:<jupyter_code>import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])

print(arr.reshape(2, 4).base)<jupyter_output>[1 2 3 4 5 6 7 8]<jupyter_text>The example above returns the original array, so it is a view.<jupyter_code># Unknown Dimension<jupyter_output><empty_output><jupyter_text>You are allowed to have one "unknown" dimensio Meaning that you do not have to specify an exact number for one of the dimensions in the reshape method. Pass -1 as the value, and NumPy will calculate this number for you.<jupyter_code>Convert 1D array with 8 elements to 3D array with 2x2 elements:<jupyter_output><empty_output><jupyter_text>import numpy as nparr = np.array([1, 2, 3, 4, 5, 6, 7, 8])newarr = arr.reshape(2, 2, -1)print(newarr)<jupyter_code>NOTE: We can not pass -1 to more than one dimension.<jupyter_output><empty_output><jupyter_text>Flattening the arrays<jupyter_code>|Flattening array means converting a multidimensional array into a 1D array.
We can use reshape(-1) to do this.<jupyter_output><empty_output><jupyter_text>Convert the array into a 1D array:<jupyter_code>import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])

newarr = arr.reshape(-1)

print(newarr)<jupyter_output>[1 2 3 4 5 6]
{'fraction_non_alphanumeric': 0.09908998988877654, 'fraction_numerical': 0.05358948432760364, 'mean_word_length': 4.831041257367387, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '429610', 'license': 'MIT', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1058, 'n_words': 416, 'path': '8. NumPy Array Reshaping.ipynb', 'repo_name': 'Vinaykumargond/Python-Numpy-Tutorial', 'size': '2967'}