"This cell is a *Markdown cell*. To create a markdown cell you have to go on the top menu and select *Markdown* from the curtain. For an overview of the basic commands of *Markdown cells* visit [this page](https://www.ibm.com/support/knowledgecenter/en/SSGNPV_1.1.3/dsx/markd-jupyter.html). You can modify this cell with a double click"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# NumPy\n",
"`numpy` is a fundamental Python package and it is mainly used for scientific calculations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Import `numpy` package calling it `np`. From now on everytime we want to use a `numpy` command we must refer to `numpy` as `np` "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the following there are some examples for:\n",
"* [array creation](#sec1)\n",
"* [array manipulation](#sec2)\n",
" * [operations](#operations)\n",
" * [work with array](#arr)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***\n",
"<a id='sec1'></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Array Creation\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`numpy` offers many different tools for array creation. In the following there are some examples. I suggest to run all of them, see the output and try to modify something to understand better\n",
"### 1.direct definition"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5])"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = np.array([1,2,3,4,5])\n",
"#the solo word corresponds to a print statement\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"get array shape:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(5,)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"get array length:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"get array elements:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*<font color=red> ATTENTION </font>: array elements start from 0*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"in the same way we can create a matrix:"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 3],\n",
" [ 10, 20, 30],\n",
" [100, 200, 300]])"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y = np.array([[1,2,3],[10,20,30],[100,200,300]])\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 3)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y.shape"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3])"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[0]\n",
"#same as y[0,:]"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 10, 100])"
]
},
"execution_count": 72,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[:,0]"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y[0][2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*<font color=red> ATTENTION </font>: the first index refers to the line and the second to the colum*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"### 2.`np.zeros` and `np.ones`\n",
"\n",
"creates an array of zeros [ones] of the specified length"