{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to NumPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Update/Upgrade the NumPy package to the latest version" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting numpy\n", " Using cached numpy-1.19.2-cp37-cp37m-win_amd64.whl (12.9 MB)\n", "Installing collected packages: numpy\n", "Successfully installed numpy-1.19.2\n", "Note: you may need to restart the kernel to use updated packages.\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " WARNING: The script f2py.exe is installed in 'C:\\Users\\vikto\\AppData\\Roaming\\Python\\Python37\\Scripts' which is not on PATH.\n", " Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.\n" ] } ], "source": [ "# Parts 1 and 2 can be executed in Jupyter or the Anacodna Prompt\n", "\n", "pip install numpy --upgrade\n", "# pip install numpy --upgrade --user # You may need to specify this if you're not running it as admin" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Install/Upgrade another package (e.g. Pandas) " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already up-to-date: pandas in c:\\programdata\\anaconda3\\lib\\site-packages (1.1.2)\n", "Requirement already satisfied, skipping upgrade: pytz>=2017.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas) (2019.3)\n", "Requirement already satisfied, skipping upgrade: python-dateutil>=2.7.3 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas) (2.8.1)\n", "Requirement already satisfied, skipping upgrade: numpy>=1.15.4 in c:\\users\\vikto\\appdata\\roaming\\python\\python37\\site-packages (from pandas) (1.19.2)\n", "Requirement already satisfied, skipping upgrade: six>=1.5 in c:\\programdata\\anaconda3\\lib\\site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install pandas --upgrade\n", "#pip install pandas --upgrade --user # You may need to specify this based if you're not running it as admin" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Import NumPy as np " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Use the np.array() function to create an array variable - array_hw_1 with the numbers 4,5,7 and 10 in it" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "array_hw_1 = np.array([4,5,7,10])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. Display the new array on screen" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 4, 5, 7, 10])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_hw_1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 6. Create a 2x2 array with the same values as array_hw_1 and display it on the screen" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "array_hw_2 = np.array([[4,5],[7,10]])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 4, 5],\n", " [ 7, 10]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_hw_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 7. Call the np.mean() function on each array" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.5" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(array_hw_1)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6.5" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(array_hw_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 8. Use the axis argument to find the mean for all the rows of array_hw_2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([4.5, 8.5])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(array_hw_2, axis = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 9. Use the axis argument to find the mean for all the columns of array_hw_2" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5.5, 7.5])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(array_hw_2, axis = 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 10. Use the Shift + Tab shorcut on np.mean() to check what possible arguments we can specify. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 11. Set the dtype argument equal to np.int and compare the results " ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 7])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(array_hw_2, axis = 0, dtype = np.int)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }