{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "EL5x4EOKO83H" }, "source": [ "# Numpy" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "rl1JBrOMOwPq" }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "7tUFKhLoPgcV" }, "source": [ "An array object represents a multidimensional, homogeneous array of fixed-size items" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 1615, "status": "ok", "timestamp": 1590618246737, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "Au-6VqLfOwMQ", "outputId": "9b25fd46-d76c-4d2c-929c-752712c52c21" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['USA' 'Japan' 'UK' 'Celestopoli' 'India' 'China']\n" ] } ], "source": [ "country = np.array(['USA', 'Japan', 'UK', 'Celestopoli', 'India', 'China'])\n", "print(country)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "08Sk1tNMOu3h" }, "source": [ "When you make numpy array using np.array than it would create a copy of the object array and would not reflect changes to the original array (with default parameters). On the other hand if you use np.asarray() than it would reflect changes to the original array.\n", "\n" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "YNj3RsPqOupy" }, "outputs": [], "source": [ "x = np.array([2,3,1,0])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 1042, "status": "ok", "timestamp": 1590618309931, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "OByLByscPTaK", "outputId": "d8f10b8c-cd81-433a-9dc2-8b0760ccbf24" }, "outputs": [ { "data": { "text/plain": [ "array([2, 3, 1, 0])" ] }, "execution_count": 11, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 976, "status": "ok", "timestamp": 1590618303461, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "th_lDhgVOunW", "outputId": "7d2ddd8d-7def-4505-8eda-7d4176075e9c" }, "outputs": [ { "data": { "text/plain": [ "(4,)" ] }, "execution_count": 10, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "ccadV7obOulz" }, "outputs": [], "source": [ "y = np.asarray([4,5,3,1])" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 1540, "status": "ok", "timestamp": 1590618298570, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "YlR7-LYiOujJ", "outputId": "5cfad3d5-402b-491a-8dc0-1953fa207b58" }, "outputs": [ { "data": { "text/plain": [ "array([4, 5, 3, 1])" ] }, "execution_count": 8, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "y" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 1124, "status": "ok", "timestamp": 1590618325436, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "8h_oURLgOugi", "outputId": "a1494f1d-542e-406c-c9b5-bec737feb8dc" }, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 12, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 828, "status": "ok", "timestamp": 1590618383522, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "TF9QLCABOueb", "outputId": "60449902-d401-4495-f96a-ee01959b7690" }, "outputs": [ { "data": { "text/plain": [ "(2, 4)" ] }, "execution_count": 13, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "y = np.array([[2,3,1,0],[1,1,1,1]])\n", "y.shape" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 621, "status": "ok", "timestamp": 1590618393589, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "wFupzE1rOucF", "outputId": "bea5e9e7-5ada-4791-eed7-ce2eb46a55fa" }, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0.],\n", " [0., 0., 0.]])" ] }, "execution_count": 14, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "np.zeros((2, 3))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 490, "status": "ok", "timestamp": 1590618399842, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "X28Vq7LDOuZx", "outputId": "0a6ff91c-b74b-427f-bee7-92857fb2199a" }, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1.],\n", " [1., 1., 1.]])" ] }, "execution_count": 15, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "k =np.ones((2, 3))\n", "k" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 497, "status": "ok", "timestamp": 1590618406549, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "SfGGArm4OuXX", "outputId": "88d65468-738d-43ef-d243-4a90e0176f90" }, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 16, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "k.shape" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 477, "status": "ok", "timestamp": 1590618425927, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "aYLaxl4VOuTK", "outputId": "6821243a-94da-4fab-f946-c18c423e7a46" }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 18, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "np.arange(10)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 513, "status": "ok", "timestamp": 1590618432844, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "y9FHJS65OuQN", "outputId": "fbcb3007-bda0-4785-b64c-0bab32474a0e" }, "outputs": [ { "data": { "text/plain": [ "array([2., 3., 4., 5., 6., 7., 8., 9.])" ] }, "execution_count": 19, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "np.arange(2, 10, dtype=float) # default strp=1" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 487, "status": "ok", "timestamp": 1590618527134, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "7KJ6_9HJOuOk", "outputId": "0b4dedb1-614d-44b5-8f2a-6ec7cfb6ff99" }, "outputs": [ { "data": { "text/plain": [ "array([1. , 1.33333333, 1.66666667, 2. , 2.33333333,\n", " 2.66666667, 3. , 3.33333333, 3.66666667, 4. ])" ] }, "execution_count": 26, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "np.linspace(1., 4.,num=10)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 554, "status": "ok", "timestamp": 1590618546519, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "8X5A5Pj0OuLq", "outputId": "3e60463f-d2f7-4b95-babb-c6b333049ef1" }, "outputs": [ { "data": { "text/plain": [ "array([1. , 1.33333333, 1.66666667, 2. , 2.33333333,\n", " 2.66666667, 3. , 3.33333333, 3.66666667, 4. ])" ] }, "execution_count": 27, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "np.linspace(1., 4.,10)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "q_z1G2gAOuJd" }, "outputs": [], "source": [ "x = np.array([3, 6, 9, 12])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 831, "status": "ok", "timestamp": 1590618567293, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "At6xpes-OuEB", "outputId": "661dec8e-18ce-4a1c-8ac3-ad2213cdcf0e" }, "outputs": [ { "data": { "text/plain": [ "array([ 3, 6, 9, 12])" ] }, "execution_count": 29, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "x/3.0\n", "x" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 516, "status": "ok", "timestamp": 1590618574431, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "6lYADstuOt6C", "outputId": "a73ac86b-62a2-4282-ca96-6e7bcc16c874" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 3 6 9 12]\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "D79yFwpnOt0x" }, "outputs": [], "source": [ "z = np.array([2,3,1,0])" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 479, "status": "ok", "timestamp": 1590618598743, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "7YE04QkWQX8O", "outputId": "ce96c8b3-026a-4c8a-d8b2-94eb4a9e804f" }, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3])" ] }, "execution_count": 32, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "np.sort(z)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "j2t6ZWPgQX4r" }, "outputs": [], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6]])\n", "b = np.array([[9, 8, 7], [6, 5, 4]])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 85 }, "colab_type": "code", "executionInfo": { "elapsed": 494, "status": "ok", "timestamp": 1590618613602, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "ieZI9-QSQX3J", "outputId": "5122dfa0-2372-488c-c589-ac1a499a4d0e" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6],\n", " [9, 8, 7],\n", " [6, 5, 4]])" ] }, "execution_count": 34, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "c = np.concatenate((a, b))\n", "c" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 7210, "status": "ok", "timestamp": 1590618625669, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "rZ155Lm0QXz2", "outputId": "fb1e1fe4-8d34-4022-e58c-7d0c69b594c1" }, "outputs": [ { "data": { "text/plain": [ "(4, 3)" ] }, "execution_count": 35, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "c.shape" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 1678, "status": "ok", "timestamp": 1590618626490, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "TITcACTpQXxm", "outputId": "a65e6a0c-2629-4c3f-d356-7bc6a27a1333" }, "outputs": [ { "data": { "text/plain": [ "array([[ True, True],\n", " [ True, True]])" ] }, "execution_count": 36, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "np.ones((2, 2), dtype=bool)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 8234, "status": "ok", "timestamp": 1590618695903, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "mSlfuZtuQXu_", "outputId": "800918bb-6e32-45b2-e27e-0ecf6cade8e0" }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7, 8])" ] }, "execution_count": 39, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "## Conversion to an array:\n", "\n", "import numpy as np\n", "\n", "# from list\n", "my_list = [1, 2, 3, 4, 5, 6, 7, 8]\n", "\n", "my_list = np.asarray(my_list)\n", "#type(my_list)\n", "my_list" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 564, "status": "ok", "timestamp": 1590618787548, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "BBDNDkRtQXsk", "outputId": "9a43b7da-b536-47c3-8be6-288a9ff55f2c" }, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 40, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "# from tuple\n", "my_tuple = ([8, 4, 6], [1, 2, 3])\n", "type(my_tuple)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 497, "status": "ok", "timestamp": 1590618800975, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "wqAxmG5SQXqs", "outputId": "176c7a31-8076-4fe6-e8e2-11ef145dbd57" }, "outputs": [ { "data": { "text/plain": [ "array([[8, 4, 6],\n", " [1, 2, 3]])" ] }, "execution_count": 41, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "my_tuple = np.asarray(my_tuple)\n", "my_tuple" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 527, "status": "ok", "timestamp": 1590619323810, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "rTV9CV3FTLae", "outputId": "3efe68b1-a861-4aba-def7-faaffdea1337" }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 65, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "#Array to list\n", "a = np.array([1, 2, 3, 4, 5])\n", "\n", "b = a.tolist()\n", "b" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "AqoF1D8tTPbe" }, "outputs": [], "source": [ "#save array to drive\n", "a = np.array([1, 2, 3, 4, 5])\n", "\n", "np.savetxt(\"il_mio_array.csv\", a)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "ccH03XybTPYC" }, "outputs": [], "source": [ "# il filse csv deve essere sul drive\n", "from google.colab import files\n", "files.download(\"il_mio_array.csv\")" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "XpoOO6RfQXpb" }, "outputs": [], "source": [ "#######################################################" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 530, "status": "ok", "timestamp": 1590618880156, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "y6pTK3_WQXmI", "outputId": "12eee9ee-cc27-4f71-e6f6-89e971b15ba0" }, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3])" ] }, "execution_count": 42, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "a = np.array([1,2,3])\n", "a" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 627, "status": "ok", "timestamp": 1590618887458, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "0EL_fH1cQXjT", "outputId": "ada75bb7-704a-43d9-bb2b-623fb7ab5c24" }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 10, 11, 12, 13])" ] }, "execution_count": 43, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "b =np.append(a,[10,11,12,13])\n", "b" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 820, "status": "ok", "timestamp": 1590618898520, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "f3_X8PY7QXgw", "outputId": "935b146d-6ebd-4852-a145-1b9cc90ab3b6" }, "outputs": [ { "data": { "text/plain": [ "array([2, 4, 6])" ] }, "execution_count": 44, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "a = np.array([1,2,3])\n", "b = np.array([1,2,3])\n", "c = a+b\n", "c" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 1029, "status": "ok", "timestamp": 1590618932646, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "oK0kJIasOtyE", "outputId": "1bf29cfa-fa38-422e-d5aa-51cd95d96494" }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 4, 5, 6, 400, 800])" ] }, "execution_count": 45, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "a = np.array([[1,2,3],[4,5,6]])\n", "b = np.array([[400], [800]])\n", "\n", "c = np.append(a, b)\n", "c" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 528, "status": "ok", "timestamp": 1590618992226, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "GB6bBd5oRyny", "outputId": "e1c0b62e-6253-4ba9-fbed-ff43d8ed9fd4" }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 400],\n", " [ 4, 5, 6, 800]])" ] }, "execution_count": 48, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "a = np.array([[1,2,3],[4,5,6]])\n", "b = np.array([[400], [800]])\n", "\n", "c = np.append(a, b, axis = 1)\n", "c" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 547, "status": "ok", "timestamp": 1590619010541, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "klX8rt6oR3zE", "outputId": "7284ccd9-d0e1-472b-a7e4-7c00d38ea3a2" }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 90, 2, 3, 4, 5, 6])" ] }, "execution_count": 49, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "d = np.array([1, 2, 3])\n", "e = np.insert(a, 1, 90) ## specify whick index add the value we want\n", "e" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 481, "status": "ok", "timestamp": 1590619147184, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "vNMeDbeHR3wR", "outputId": "d7a7a15c-d797-49a7-9605-c94878a9a148" }, "outputs": [ { "data": { "text/plain": [ "array([60, 1, 2, 3])" ] }, "execution_count": 52, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "d = np.array([1, 2, 3])\n", "e = np.insert(d, 0, 60) ## specify whick index add the value we want\n", "e" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 644, "status": "ok", "timestamp": 1590619173947, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "K2Ra-zmqSh8H", "outputId": "703f91d2-d8e5-4735-ffa5-21c192c03382" }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 53, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "len(d)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 478, "status": "ok", "timestamp": 1590619181406, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "ATZLbdQcR3sp", "outputId": "a4858e7c-d82a-40ba-b234-60bcba56c1a8" }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 2, 3, 90])" ] }, "execution_count": 54, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "d = np.array([1, 2, 3])\n", "\n", "e = np.insert(d, 3, 90) ## specify whick index add the value we want\n", "e" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "DoV5CxARR3qh" }, "outputs": [], "source": [ "#APPEND A ROW" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "colab_type": "code", "executionInfo": { "elapsed": 488, "status": "ok", "timestamp": 1590619208418, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "BUu8sYV-R3nK", "outputId": "e5725b03-281c-4229-f20f-4cc364068277" }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3],\n", " [ 4, 5, 6],\n", " [50, 60, 70]])" ] }, "execution_count": 56, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "a = np.array([[1, 2, 3], [4, 5, 6]])\n", "\n", "b = np.append(a, [[50, 60, 70]], axis = 0)\n", "b" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 653, "status": "ok", "timestamp": 1590619221579, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "WavXw_CBR3kZ", "outputId": "290cee87-81e8-476c-90f9-a9bec2242824" }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2, 3, 50],\n", " [ 4, 5, 6, 60]])" ] }, "execution_count": 57, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "## se volessi per colonna?\n", "a = np.array([[1, 2, 3], [4, 5, 6]])\n", "\n", "c = np.array([[50],[60]])\n", "b = np.append(a, c, axis = 1)\n", "b" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 527, "status": "ok", "timestamp": 1590619231488, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "BbYDe3tlR3i8", "outputId": "e2225627-119d-47b4-cde6-0ee4e101a5ac" }, "outputs": [ { "data": { "text/plain": [ "array([1, 3])" ] }, "execution_count": 58, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "### Delete an element\n", "x = np.array([1, 2, 3])\n", "\n", "x= np.delete(x, 1, axis = 0)\n", "x" ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 51 }, "colab_type": "code", "executionInfo": { "elapsed": 492, "status": "ok", "timestamp": 1590619236842, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "YlXWa22aR3fk", "outputId": "35dbf0b9-d8b0-44ee-e229-bf06b29c09cd" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [4, 5, 6]])" ] }, "execution_count": 59, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "#delete an entire row (axis = 0)\n", "x = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])\n", "\n", "x = np.delete(x, 2, axis = 0)\n", "x" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 512, "status": "ok", "timestamp": 1590619243879, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "1q_OhwitR3ci", "outputId": "02e48398-9873-40f7-e97d-6b55e857c3f1" }, "outputs": [ { "data": { "text/plain": [ "(2, 3)" ] }, "execution_count": 60, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "x.shape" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 68 }, "colab_type": "code", "executionInfo": { "elapsed": 489, "status": "ok", "timestamp": 1590619251007, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "K76vvn1JR3ac", "outputId": "58dec169-f5cb-40bb-ffb5-94c9100ecf91" }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 2],\n", " [ 4, 5],\n", " [10, 20]])" ] }, "execution_count": 61, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "#delete an entire column (axis = 1)\n", "x = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])\n", "\n", "x = np.delete(x, 2, axis = 1)\n", "x" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 481, "status": "ok", "timestamp": 1590619260192, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "EHHLss5qR3Xd", "outputId": "752a12f2-cda3-4380-d1d9-2ea4e85176f2" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "il valore 5 si trova all'index: (array([4]),)\n" ] } ], "source": [ "#find the index value\n", "\n", "a = np.array([1, 2, 3, 4, 5])\n", "\n", "print(\"il valore 5 si trova all'index: \", np.where(a == 5))" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 499, "status": "ok", "timestamp": 1590619266511, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "5vvKoSZaR3Vs", "outputId": "199a9c06-9a40-4c76-e5f5-bcc5fe55a37a" }, "outputs": [ { "data": { "text/plain": [ "array([3, 4, 5, 6, 7, 8])" ] }, "execution_count": 63, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "######### automatic addition\n", "\n", "addizione = lambda x: x + 2\n", "\n", "a = np.array([1, 2, 3, 4, 5, 6])\n", "\n", "b = addizione(a)\n", "b" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 501, "status": "ok", "timestamp": 1590619278177, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "w--2BNq7R3Un", "outputId": "2fe19001-df7c-406f-cb61-7db3a94dd6cd" }, "outputs": [ { "data": { "text/plain": [ "array([ 1, 4, 9, 16, 25, 36])" ] }, "execution_count": 64, "metadata": { "tags": [] }, "output_type": "execute_result" } ], "source": [ "moltiplicazione = lambda x: x**2\n", "\n", "a = np.array([1, 2, 3, 4, 5, 6])\n", "\n", "b = moltiplicazione(a)\n", "b" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 534, "status": "ok", "timestamp": 1590619457415, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "cCIut-V2R3Qj", "outputId": "fa85392e-e12d-4c3e-aee2-6f6eb9acd20d" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sei maggiorenne\n" ] } ], "source": [ "età=18\n", "\n", "if età <18:\n", " print('sei minorenne')\n", "else:\n", " print('sei maggiorenne')" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 555, "status": "ok", "timestamp": 1590619473592, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "hMPXAr9xR3N2", "outputId": "fb667aea-fbd7-4787-e4bf-fc08cb95f0cd" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sei un regaz\n" ] } ], "source": [ "età=18\n", "\n", "if età <10:\n", " print('sei un bimbo')\n", "elif età<=18:\n", " print('sei un regaz')\n", "elif età<50:\n", " print('sei un boomer')\n", "else:\n", " print('bella vecchio! sei anziano')" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 34 }, "colab_type": "code", "executionInfo": { "elapsed": 543, "status": "ok", "timestamp": 1590619511477, "user": { "displayName": "T3Lab Vision", "photoUrl": "", "userId": "14779383426442114373" }, "user_tz": -120 }, "id": "c1eON1IMTwQw", "outputId": "9346f8c5-28d1-47e2-d411-8844cb53f466" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "no patente, no party\n" ] } ], "source": [ "età=50\n", "patente=False\n", "\n", "if età >=18 and patente==True:\n", " print('puoi guidare finalmente la tua auto da sogno')\n", "elif età >=18 and patente==False:\n", " print('no patente, no party')\n", "else:\n", " print('mi dispiace, ritenta quando sarai più grande!!')" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "aVxuPzCuUB0W" }, "source": [ "## Funzioni" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "w5nQNDzVUBFO" }, "outputs": [], "source": [ "def somma_due_numeri(a,b): ## a,b sono argomenti\n", " #print('questa funzione somma due numeri:')\n", " risultato = a + b\n", " return risultato\n", "# OUTPUT DI UNA FUNZIONE è IL VALORE DEL SUO RETURN\n", "# TUTTE LE FUNZIONI HANNO UN RETURN" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "1neHPvJfT5gw" }, "outputs": [], "source": [ "## input" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "J9df6FA4USn2" }, "outputs": [], "source": [ "x =input()\n", "#x = input('inserire qualcosa ')" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "pOxY8eY7UWfo" }, "outputs": [], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "i8ns-kXsUWcO" }, "outputs": [], "source": [ "### PROGRAMMA\n", "print('questo è il mio programma in python')\n", "\n", "nome=input('inserire il tuo nome ') \n", "print('buon pomeriggio '+nome+' spero tu stia imparando tantissimo!')" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "_dxO9VpLUWaF" }, "outputs": [], "source": [ "età = input('quanti anni hai?')\n", "print('Bene!!! dunque hai '+ età + ' anni. Il porssimo anno ne avrai '+ str(int(età)+1))" ] } ], "metadata": { "colab": { "authorship_tag": "ABX9TyP1iowxR4WHVGGUHR4NFCE9", "collapsed_sections": [], "name": "03_intro.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3.6.9 64-bit ('env': virtualenv)", "language": "python", "name": "python36964bitenvvirtualenv07dcaa16c3e14140805435db9416e568" }, "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.9" } }, "nbformat": 4, "nbformat_minor": 1 }