From 9f0f50fe4803009d1d4968a0e2f4722b061751a2 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Thu, 30 May 2024 19:27:15 +0530 Subject: [PATCH 1/5] Added index --- contrib/numpy/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index b2d459ad..df759d97 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -3,6 +3,7 @@ - [Installing NumPy](installing-numpy.md) - [Introduction](introduction.md) - [NumPy Data Types](datatypes.md) +- [Numpy Array Shape and Reshape](array_reshape.md) - [Basic Mathematics](basic_math.md) - [Operations on Arrays in NumPy](operations-on-arrays.md) - [Loading Arrays from Files](loading_arrays_from_files.md) From ed9e0652008dc706d96fbbc0833d7458bcc2e386 Mon Sep 17 00:00:00 2001 From: Rupa-Rd Date: Thu, 30 May 2024 19:55:02 +0530 Subject: [PATCH 2/5] Numpy array reshape() is added --- contrib/numpy/array_reshape.md | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 contrib/numpy/array_reshape.md diff --git a/contrib/numpy/array_reshape.md b/contrib/numpy/array_reshape.md new file mode 100644 index 00000000..d3d7f595 --- /dev/null +++ b/contrib/numpy/array_reshape.md @@ -0,0 +1,54 @@ +# Numpy Array Shape and Reshape +In NumPy, the primary data structure is the ndarray (N-dimensional array). An array can have one or more dimensions, and it organizes your data efficiently. + +Code to create a 2D array +``` python +import numpy as np + +numbers = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) +print(numbers) + +# Output: +# array([[1, 2, 3, 4],[5, 6, 7, 8]]) +``` + +## Changing Array Shape using Reshape() +The `reshape()` function allows you to rearrange the data within a NumPy array. +It take 2 arguements, row and columns. The `reshape()` can add or remove the dimensions. For instance, array can convert a 1D array into a 2D array or vice versa. + +``` python +arr_1d = np.array([1, 2, 3, 4, 5, 6]) # 1D array +arr_2d = arr_1d.reshape(2, 3) # Reshaping with 2rows and 3cols + +print(arr_2d) + +# Output: +# array([[1, 2, 3],[4, 5, 6]]) + +``` + +## Changing Array Shape using Resize() +The `resize()` function allows you to modify the shape of a NumPy array directly. +It take 2 arguements, row and columns. + +``` python +import numpy as np +arr_1d = np.array([1, 2, 3, 4, 5, 6]) + +arr_1d.resize((2, 3)) # 2rows and 3cols +print(arr_1d) + +# Output: +# array([[1, 2, 3],[4, 5, 6]]) + +``` + +## Reshape() VS Resize() + +| Reshape | Resize | +| ----------- | ----------- | +| Does not modify the original array | Modifies the original array in-place | +| Creates a new array | Changes the shape of the array | +| Returns a reshaped array | Doesn't return anything | +| Compatibility between dimensions | Does not compatibility between dimensions | +| Syntax: reshape(row,col) | Syntax: resize((row,col)) | From 73955bd969f1f799832976af4177d24c3f09b9ff Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Fri, 31 May 2024 07:10:57 +0530 Subject: [PATCH 3/5] Update index.md --- contrib/numpy/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/numpy/index.md b/contrib/numpy/index.md index df759d97..2e418abc 100644 --- a/contrib/numpy/index.md +++ b/contrib/numpy/index.md @@ -3,7 +3,7 @@ - [Installing NumPy](installing-numpy.md) - [Introduction](introduction.md) - [NumPy Data Types](datatypes.md) -- [Numpy Array Shape and Reshape](array_reshape.md) +- [Numpy Array Shape and Reshape](reshape-array.md) - [Basic Mathematics](basic_math.md) - [Operations on Arrays in NumPy](operations-on-arrays.md) - [Loading Arrays from Files](loading_arrays_from_files.md) From bf4fa5a067c550d3a76403789e33839827c96021 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Fri, 31 May 2024 07:11:13 +0530 Subject: [PATCH 4/5] Rename array_reshape.md to reshape-array.md --- contrib/numpy/{array_reshape.md => reshape-array.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contrib/numpy/{array_reshape.md => reshape-array.md} (100%) diff --git a/contrib/numpy/array_reshape.md b/contrib/numpy/reshape-array.md similarity index 100% rename from contrib/numpy/array_reshape.md rename to contrib/numpy/reshape-array.md From 02c7f2c3a202fc7234c5000dd010b1f3df6b9910 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Fri, 31 May 2024 07:19:32 +0530 Subject: [PATCH 5/5] Update reshape-array.md --- contrib/numpy/reshape-array.md | 47 ++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/contrib/numpy/reshape-array.md b/contrib/numpy/reshape-array.md index d3d7f595..91da3660 100644 --- a/contrib/numpy/reshape-array.md +++ b/contrib/numpy/reshape-array.md @@ -1,54 +1,57 @@ # Numpy Array Shape and Reshape + In NumPy, the primary data structure is the ndarray (N-dimensional array). An array can have one or more dimensions, and it organizes your data efficiently. -Code to create a 2D array +Let us create a 2D array + ``` python import numpy as np numbers = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(numbers) +``` + +#### Output: -# Output: -# array([[1, 2, 3, 4],[5, 6, 7, 8]]) +``` python +array([[1, 2, 3, 4],[5, 6, 7, 8]]) ``` -## Changing Array Shape using Reshape() +## Changing Array Shape using `reshape()` + The `reshape()` function allows you to rearrange the data within a NumPy array. -It take 2 arguements, row and columns. The `reshape()` can add or remove the dimensions. For instance, array can convert a 1D array into a 2D array or vice versa. + +It take 2 arguments, row and columns. The `reshape()` can add or remove the dimensions. For instance, array can convert a 1D array into a 2D array or vice versa. ``` python arr_1d = np.array([1, 2, 3, 4, 5, 6]) # 1D array -arr_2d = arr_1d.reshape(2, 3) # Reshaping with 2rows and 3cols +arr_2d = arr_1d.reshape(2, 3) # Reshaping with 2 rows and 3 cols print(arr_2d) +``` -# Output: -# array([[1, 2, 3],[4, 5, 6]]) +#### Output: +``` python +array([[1, 2, 3],[4, 5, 6]]) ``` -## Changing Array Shape using Resize() +## Changing Array Shape using `resize()` + The `resize()` function allows you to modify the shape of a NumPy array directly. + It take 2 arguements, row and columns. ``` python import numpy as np arr_1d = np.array([1, 2, 3, 4, 5, 6]) -arr_1d.resize((2, 3)) # 2rows and 3cols +arr_1d.resize((2, 3)) # 2 rows and 3 cols print(arr_1d) - -# Output: -# array([[1, 2, 3],[4, 5, 6]]) - ``` -## Reshape() VS Resize() +#### Output: -| Reshape | Resize | -| ----------- | ----------- | -| Does not modify the original array | Modifies the original array in-place | -| Creates a new array | Changes the shape of the array | -| Returns a reshaped array | Doesn't return anything | -| Compatibility between dimensions | Does not compatibility between dimensions | -| Syntax: reshape(row,col) | Syntax: resize((row,col)) | +``` python +array([[1, 2, 3],[4, 5, 6]]) +```