{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import numpy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "l=[1,2,3]\n", "arr=numpy.array(l)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.array(l)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "arr_1=np.array([1,2,3])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr_1" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 4])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr_1+1" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr_1*2" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 2, 3, 4]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l_1=[1,2,3]\n", "l_2=[2,3,4]\n", "l_1+l_2" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "arr_1=np.array(l_1)\n", "arr_2=np.array(l_2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([3, 5, 7])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr_1+arr_2" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr_1.sum()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Indexing\n", "arr_1[0]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr_1[2]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n" ] } ], "source": [ "for i in arr_1:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "arr_2D=np.array([[1,1],[2,1],[3,2]])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 1],\n", " [2, 1],\n", " [3, 2]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "arr_2D" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "rand_array=np.random.random(size=[3,2])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "zero_array=np.zeros(shape=[1,2])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0.]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zero_array" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "rand_array=np.random.random(size=[5,5])" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.14758131, 0.83260796, 0.40430879, 0.41555499, 0.63120785],\n", " [0.4924862 , 0.58400964, 0.42182289, 0.31476085, 0.26750594],\n", " [0.96549509, 0.591418 , 0.733441 , 0.86647242, 0.50206162],\n", " [0.82343266, 0.60601154, 0.91327087, 0.80994222, 0.56830348],\n", " [0.01198283, 0.65237893, 0.80848757, 0.06219124, 0.1512057 ]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_array" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5840096442228575" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_array[1,1]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.14758131, 0.83260796],\n", " [0.4924862 , 0.58400964]])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_array[0:2,0:2]" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13.577941591636831" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_array.sum()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1.47581315, 8.32607961, 4.04308794, 4.15554993, 6.31207848],\n", " [4.924862 , 5.84009644, 4.21822889, 3.14760848, 2.67505941],\n", " [9.65495086, 5.91418003, 7.33441002, 8.66472415, 5.02061621],\n", " [8.2343266 , 6.06011542, 9.13270869, 8.09942221, 5.68303475],\n", " [0.11982825, 6.52378932, 8.08487569, 0.62191235, 1.51205703]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_array*10" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.14758131, 0.83260796, 0.40430879, 0.41555499, 0.63120785],\n", " [0.4924862 , 0.58400964, 0.42182289, 0.31476085, 0.26750594],\n", " [0.96549509, 0.591418 , 0.733441 , 0.86647242, 0.50206162],\n", " [0.82343266, 0.60601154, 0.91327087, 0.80994222, 0.56830348],\n", " [0.01198283, 0.65237893, 0.80848757, 0.06219124, 0.1512057 ]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand_array" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.14758131461879764\n", "0.8326079613031313\n", "0.40430879365867045\n", "0.41555499300559384\n", "0.6312078477007262\n", "0.4924862000813247\n", "0.5840096442228575\n", "0.42182288947930546\n", "0.3147608476732785\n", "0.26750594058100785\n", "0.9654950861057456\n", "0.5914180033482038\n", "0.7334410021414434\n", "0.8664724152019767\n", "0.5020616206983554\n", "0.8234326602713046\n", "0.606011541851732\n", "0.9132708688228633\n", "0.8099422209030873\n", "0.5683034752295326\n", "0.011982825453687562\n", "0.6523789316439983\n", "0.8084875690671237\n", "0.06219123511786995\n", "0.15120570345521356\n" ] } ], "source": [ "for row in rand_array:\n", " for i in row:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "# Exercises." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "x=np.linspace(0,10,100)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "#Calculate y=7x+2" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "#Calculate y=x**2 for the first 10 elements" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "#Calculate " ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "#Define a 100 equally spaced points using linspace. And then another 100 random points. And then add x and y together\n", "x=np.linspace(0,10,100)\n", "y=np.random.random(size=100)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "#Add x and y together and save in z" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "#Calculate the sum of of x,y and z and tell me which one is the largest" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }