{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3, 4],\n", " [5, 6, 7, 8]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#creating array by providing numerical values\n", "arr = np.array([[1,2,3,4],[5,6,7,8]])\n", "arr" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#numpy supports multidimensional array\n", "arr = np.arange(12)\n", "arr" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#we can format the array to different dimensions\n", "#Let's convert the arrray into 3 x 4\n", "arr.reshape(3,4)\n", "arr" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#The array is not reshaped as it returns a different objet\n", "#To get what we intend let's assign the returned the object to arr itself\n", "arr=arr.reshape(3,4)\n", "arr" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "10\n", "11\n" ] } ], "source": [ "#iterating over the array\n", "for x in np.nditer(arr):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 4, 9],\n", " [ 16, 25, 36, 49],\n", " [ 64, 81, 100, 121]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#multiplication\n", "arr*arr" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4, 6],\n", " [ 8, 10, 12, 14],\n", " [16, 18, 20, 22]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#addition\n", "arr+arr" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#sum of all elements\n", "arr.sum()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([12, 15, 18, 21])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#sum of each row separately\n", "arr.sum(axis=0)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 6, 22, 38])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#sum of each column separately\n", "arr.sum(axis=1)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2, 3],\n", " [4, 5, 6, 7]])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#slicing\n", "arr[0:2]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 8, 9, 10, 11])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#last row\n", "arr[-1]" ] } ], "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }