{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Folding and Reshaping Matrices"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To really understand arrays (be they vectors, matrices, or arrays with more dimensions), it helps to understand how arrays are implemented in numpy.\n",
"\n",
"In numpy, arrays are thought of as a onedimensional string of entries + information about how that data should be \"folded\". \n",
"\n",
"Why? Because that's how array data is actually laid out in your computer's memory. Computer memory is organized—at least from the perspective of the operating system—in one dimension as a *really* long sequence of spots for storing 1s and 0s. So in a very concrete sense, the data in an array *is* laid out as a onedimensional string of entries + information about how numpy should \"fold\" that data when doing operations. \n",
"\n",
"The onedimensional vectors we've been working with, in other words, are just onedimensional strings of data + information saying they shouldn't be folded. A matrix is a onedimensional string of data that gets wrapped into rows and columns. That, in fact, is what is stored in `.shape`—directions on how numpy should think about the data being wrapped.\n",
"\n",
"One consequence of this is that it's very easy to refold data in lots of different ways with `numpy`. For example, I can take a 4 x 3 matrix, and make it 3 x 4:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3],\n",
" [ 4, 5, 6],\n",
" [ 7, 8, 9],\n",
" [10, 11, 12]])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"my_matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])\n",
"my_matrix"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8],\n",
" [ 9, 10, 11, 12]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix.reshape((3, 4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As you can see, numpy thinks of the data in `my_matrix` as all the numbers along one row (the first dimension) followed by the numbers in the second row, etc., and `reshape` is just changing where each row ends and where the next one begins. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indeed, we could also make our data 1 x 12:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix.reshape((1, 12))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But because matrices have to have a regular shape, you can't take any matrix and reshape it into just any shape. Our `my_matrix` above has 12 elements, which means that it can only be reshaped into dimensions that multiply to equal 12, like 1 x 12, 12 x 1, 3 x 4, or 4 x 3. If you tried to use `reshape` to make our matrix, say, 3 x 3, you'd get an error like this:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```python \n",
"my_matrix.reshape((3, 3))\n",
"\n",
"ValueError Traceback (most recent call last)\n",
"/22_reshaping_matrices.ipynb Cell 8' in ()\n",
"> 1 my_matrix.reshape((3, 3))\n",
"\n",
"ValueError: cannot reshape array of size 12 into shape (3,3)\n",
"```\n",
"\n",
"Basically, numpy is saying \"Hey, your matrix has 12 elements—if I tried to make it 3 x 3, my matrix would only have space for 9 of those 12 elements, and I wouldn't know what to do with the extra three elements!\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reshaping Dimensions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Numpy isn't limited to changing where each row ends and the next one begins, however; it can also change when one *dimension* ends and where the next one begins. So far we've only explicitly worked with one and twodimensional arrays, but as we noted before, arrays can organize data along arbitrarily many dimensions. We'll talk about this in detail later, but for the moment, I just want to show you how we can use `reshape` to make our 2dimensional matrix a 3dimensional array (namely a 2 x 2 x 3 array):"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 1, 2, 3],\n",
" [ 4, 5, 6]],\n",
"\n",
" [[ 7, 8, 9],\n",
" [10, 11, 12]]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix = my_matrix.reshape((2, 2, 3))\n",
"my_matrix"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 2, 3)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So remember: in numpy, an array is always just a string of data that is being folded, and the way the data is folded is indicated by `.shape`, and can always be changed with `.reshape()`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Reshape and arange"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One place this reshape trick can be very useful is in working with `np.arange()`. Unlike `ones` and `zeros`, to which you can pass the output dimensions you want, `np.arange()` will always return 1dimensional data. That means that if you want a sequence of numbers in a matrix, you have to combine `np.arange()` with `.reshape()`:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(20)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14],\n",
" [15, 16, 17, 18, 19]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(20).reshape((4, 5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transpose\n",
"\n",
"OK, one other cool trick that's related to reshape that often comes up when working with `np.arange()`: `.transpose()`. The transpose of a matrix is what you get when you make rows into columns and columns into rows. For example:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [4, 5, 6]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix = np.array([[1, 2, 3], [4, 5, 6]])\n",
"my_matrix"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 4],\n",
" [2, 5],\n",
" [3, 6]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_matrix.transpose()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Transpose is often combined with `np.array()` and `.reshape()` because otherwise, those two tools would always generate sequences that increase incrementally across rows before wrapping to the next row:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 1, 2],\n",
" [3, 4, 5]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(6).reshape((2, 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So if you want your sequence to move down your *columns* instead of across your *rows*, you have to transpose your result:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 3],\n",
" [1, 4],\n",
" [2, 5]])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(6).reshape((2, 3)).transpose()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indeed, `.transpose()` is so frequently used in numpy that you can call it with `.T`:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 3],\n",
" [1, 4],\n",
" [2, 5]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.arange(6).reshape((2, 3)).T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises \n",
"\n",
"1. Using `np.arange`, create a vector with all the integers from 0 to 23. \n",
"2. Using `.reshape`, convert that vector into a matrix with 4 rows and 6 columns where the numbers increase as you move from left to right along each row before wrapping to the next row.\n",
"3. Using `reshape`, try to convert this matrix into a 5 x 5 matrix. Why were you unsuccessful?\n",
"4. Using `np.arange`, create a *new* sequence that you *can* reshape into a 5 x 5 matrix."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.6 ('base')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/xpython",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "718fed28bf9f8c7851519acf2fb923cd655120b36de3b67253eeb0428bd33d2d"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
