"# numpy has a square root function of its own...\n",
"numpy.sqrt(my_other_array)"
...
...
@@ -264,7 +264,7 @@
},
"outputs": [],
"source": [
"my_array = np.array([4,1,5,2])"
"my_array = np.array([4,1,5,2])"
]
},
{
...
...
@@ -310,10 +310,7 @@
"Source: Nunez-Iglesias, Juan, Stéfan Van Der Walt, and Harriet Dashnow. *Elegant SciPy: The Art of Scientific Python.* O'Reilly Media, 2017.\n",
"\n",
"\n",
"`numpy` can create up to _n_-dimensional arrays, but let's focus on two: this is a familiar way to shape data as it's how data is often is stored in spreadsheets (as rows and columns).\n",
"\n",
"\n",
"We can create a two-dimensional array in `numpy` with the `array()` function. This time we will place each 'row' of the array inside its own set of brackets."
"`numpy` can create up to _n_-dimensional arrays. You can think of one, two, and three-dimensional arrays as being like individual ranges, tables, and worksheets in Excel."
]
},
{
...
...
@@ -326,7 +323,7 @@
"source": [
"# Create a two-dimensional array with `np.array()`\n",
"\n",
"my_2d_array = np.array([[3,4,1],[2,5,0]])\n",
"my_2d_array = np.array([[3,4,1],[2,5,0]])\n",
"type(my_2d_array)"
]
},
...
...
@@ -346,7 +343,7 @@
"outputs": [],
"source": [
"# One-dimensional array\n",
"my_array = np.array([1,2,3,4,5,6])\n",
"my_array = np.array([1,2,3,4,5,6])\n",
"my_array\n"
]
},
...
...
@@ -381,8 +378,50 @@
"\n",
"Some attributes we can use to learn more about our `numpy` arrays are:\n",
"\n",
"`shape`: gives us the dimensions of the array. \n",
"`size`: gives us the number of elements of the array. \n",
"`shape`: gives us the dimensions of the array. \n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'my_reshaped_array' is not defined",
"\u001b[1;32m<ipython-input-1-35d51d988aea>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmy_reshaped_array\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'my_reshaped_array' is not defined"
]
}
],
"source": [
"my_reshaped_array.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`size`: gives us the number of elements of the array. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_reshaped_array.size"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`dtype`: gives us the data type of the elements of the array. Remember that all elements of a `numpy` array must be of the same type."
]
},
...
...
@@ -394,9 +433,7 @@
},
"outputs": [],
"source": [
"print(my_reshaped_array.shape)\n",
"print(my_reshaped_array.size)\n",
"print(my_reshaped_array.dtype)"
"my_reshaped_array.dtype"
]
},
{
...
...
@@ -423,11 +460,9 @@
},
"outputs": [],
"source": [
"print(my_reshaped_array)\n",
"\n",
"# Get the value in first row, first column\n",
"# Never forget zero-based indexing!\n",
"my_reshaped_array[0,0]"
"my_reshaped_array[0,0]"
]
},
{
...
...
@@ -437,7 +472,7 @@
"outputs": [],
"source": [
"# What about the second-last row/second-last column?\n",
"my_reshaped_array[-2,-2]"
"my_reshaped_array[-2,-2]"
]
},
{
...
...
@@ -454,7 +489,7 @@
"outputs": [],
"source": [
"# Get data from first through second rows and columns\n",