Matplotlib y palabras

parent 4dd91c4c
......@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 159,
"execution_count": 9,
"metadata": {},
"outputs": [
{
......@@ -47,7 +47,7 @@
" [10, 11, 12, 13, 14]], dtype=int16)"
]
},
"execution_count": 159,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
......@@ -868,7 +868,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -909,6 +909,7 @@
" v2 (vector): Lista vacia\n",
" \n",
" Yields:\n",
" v3 (float): Un escalar\n",
" \n",
" Examples:\n",
" >>> prod_punto([10,20],[2,4])\n",
......@@ -931,7 +932,7 @@
" sumap=reduce((lambda x,y: x+y),v3)\n",
" \n",
" print(\"\\nVector 1: \"+str(v1)+\"\\n\"+\"Vector 2: \"+str(v2)+\"\\n\"+\"Suma Producto: \"+str(sumap))\n",
" return sumap\n",
" return (sumap)\n",
"\n",
" #return prod_punto\n",
" \n",
......@@ -949,27 +950,57 @@
},
{
"cell_type": "code",
"execution_count": 173,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'def matmult(a,b):\\n zip_b = zip(*b)\\n #uncomment next line if python 3 : \\n zip_b = list(zip_b)\\n return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \\n for col_b in zip_b] for row_a in a]\\n'"
"'b=mat2\\nprint(b)\\nzip_b = zip(*b)\\nfor col_b in zip_b:\\n print(col_b)\\n'"
]
},
"execution_count": 173,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tamMatriz():\n",
" '''\n",
" Función que da tamaño a un arreglo matricial\n",
" \n",
" Args:\n",
" None\n",
" \n",
" Yields:\n",
" En forma de lista devuelve:\n",
" nFila(int): Un escalar\n",
" nCol(int): Un escalar\n",
" \n",
" '''\n",
" n=int(input(\"Ingrese las filas de la matriz: \\n\"))\n",
" m=int(input(\"Ingrese las columnas de la matriz: \\n\"))\n",
" return [n,m]\n",
"\n",
"def creaMatriz(nFila, nCol):\n",
" '''\n",
" Función que introduce valores a un arreglo matricial.\n",
" \n",
" Args:\n",
" nFila(int): Un escalar\n",
" nCol(int): Un escalar\n",
" \n",
" Yields:\n",
" C(list): Lista que representa a una matriz\n",
" \n",
" Example:\n",
" mat1=[[1,2],\n",
" [3,4]]\n",
" mat2=[[2,2],\n",
" [2,2]]\n",
" C= [[6, 6],\n",
" [14, 14]]\n",
" '''\n",
" matriz = []\n",
" for i in range(nFila):\n",
" listaFila = []\n",
......@@ -1024,7 +1055,7 @@
},
{
"cell_type": "code",
"execution_count": 118,
"execution_count": 5,
"metadata": {},
"outputs": [
{
......@@ -1034,17 +1065,26 @@
"Ingrese las filas de la matriz: \n",
"2\n",
"Ingrese las columnas de la matriz: \n",
"3\n"
"2\n",
"Ingresa el valor del elemento0,0: \n",
"1\n",
"Ingresa el valor del elemento0,1: \n",
"2\n",
"Ingresa el valor del elemento1,0: \n",
"3\n",
"Ingresa el valor del elemento1,1: \n",
"4\n"
]
}
],
"source": [
"aa=tamMatriz()"
"aa=tamMatriz()\n",
"mat1 = creaMatriz(aa[0],aa[1])"
]
},
{
"cell_type": "code",
"execution_count": 133,
"execution_count": 6,
"metadata": {},
"outputs": [
{
......@@ -1052,75 +1092,70 @@
"output_type": "stream",
"text": [
"Ingrese las filas de la matriz: \n",
"3\n",
"2\n",
"Ingrese las columnas de la matriz: \n",
"2\n",
"Ingresa el valor del elemento0,0: \n",
"2\n",
"Ingresa el valor del elemento0,1: \n",
"2\n",
"Ingresa el valor del elemento1,0: \n",
"2\n",
"Ingresa el valor del elemento1,1: \n",
"2\n"
]
}
],
"source": [
"bb=tamMatriz()"
"bb=tamMatriz()\n",
"mat2 = creaMatriz(bb[0],bb[1])"
]
},
{
"cell_type": "code",
"execution_count": 119,
"execution_count": 1095,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa el valor del elemento0,0: \n",
"1\n",
"Ingresa el valor del elemento0,1: \n",
"Ingrese las filas de la matriz: \n",
"2\n",
"Ingresa el valor del elemento0,2: \n",
"3\n",
"Ingresa el valor del elemento1,0: \n",
"4\n",
"Ingresa el valor del elemento1,1: \n",
"5\n",
"Ingresa el valor del elemento1,2: \n",
"6\n"
"Ingrese las columnas de la matriz: \n",
"3\n"
]
}
],
"source": [
"mat1 = creaMatriz(aa[0],aa[1])"
"cc=tamMatriz()\n",
"mat3 = creaMatriz(cc[0],cc[1])"
]
},
{
"cell_type": "code",
"execution_count": 134,
"execution_count": 1236,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa el valor del elemento0,0: \n",
"10\n",
"Ingresa el valor del elemento0,1: \n",
"20\n",
"Ingresa el valor del elemento1,0: \n",
"30\n",
"Ingresa el valor del elemento1,1: \n",
"40\n",
"Ingresa el valor del elemento2,0: \n",
"50\n",
"Ingresa el valor del elemento2,1: \n",
"60\n"
"Ingrese las filas de la matriz: \n",
"5\n",
"Ingrese las columnas de la matriz: \n",
"5\n"
]
}
],
"source": [
"mat2=creaMatriz(bb[0],bb[1])"
"dd=tamMatriz()\n",
"mat4 = creaMatriz(dd[0],dd[1])"
]
},
{
"cell_type": "code",
"execution_count": 210,
"execution_count": 7,
"metadata": {},
"outputs": [
{
......@@ -1133,10 +1168,10 @@
{
"data": {
"text/plain": [
"[[220, 280], [490, 640]]"
"[[6, 6], [14, 14]]"
]
},
"execution_count": 210,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1154,28 +1189,89 @@
},
{
"cell_type": "code",
"execution_count": 206,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n"
]
},
{
"data": {
"text/plain": [
"array([[220, 280],\n",
" [490, 640]])"
"array([[ 6, 6],\n",
" [14, 14]])"
]
},
"execution_count": 206,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Comprobacion con Numpy ejercicio 1\n",
"np.matmul([10,20], [2,4])\n",
"print(np.matmul([10,20], [2,4]))\n",
"#Comprobacion con Numpy ejercicio 2\n",
"np.matmul(mat1, mat2)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'mat3' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-12-6b19a936e2a1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmat1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmat2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mz\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmat3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mw\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmat4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m#A=np.array(mmult1(x,y))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNameError\u001b[0m: name 'mat3' is not defined"
]
}
],
"source": [
"x=np.array(mat1,dtype=np.float64)\n",
"y=np.array(mat2, dtype=np.float64)\n",
"z=np.array(mat3,dtype=np.float64)\n",
"w=np.array(mat4,dtype=np.float64)\n",
"#A=np.array(mmult1(x,y))\n",
"#np.linalg.det(A)\n",
"#b = np.array([[ 0, 1, 2, 3, 4],\n",
"# [ 5, 6, 7, 8, 9],\n",
"# [10, 11, 12, 13, 14]], dtype=np.int16)\n",
"#b\n",
"\n",
"#Toda la fila x[0,:]\n",
"print(x[0,:]*1/1400)\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2. , 1. ],\n",
" [ 1.5, -0.5]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.linalg.inv(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -1184,6 +1280,482 @@
"[Wikipedia](https://en.wikipedia.org/wiki/Gaussian_elimination): En matemáticas, la eliminación de Gauss Jordan, llamada así en honor de Carl Friedrich Gauss y Wilhelm Jordan es un algoritmo del álgebra lineal que se usa para determinar las soluciones de un sistema de ecuaciones lineales, para encontrar matrices e inversas. Un sistema de ecuaciones se resuelve por el método de Gauss cuando se obtienen sus soluciones mediante la reducción del sistema dado a otro equivalente en el que cada ecuación tiene una incógnita menos que la anterior. El método de Gauss transforma la matriz de coeficientes en una matriz triangular superior. El método de Gauss-Jordan continúa el proceso de transformación hasta obtener una matriz diagonal"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"class Matriz():\n",
" '''\n",
" Clase que representa una matriz\n",
" \n",
" Args:\n",
" matriz (list): lista que representa una matriz (puede ser de python base o de numpy array)\n",
" \n",
" Methods:\n",
" filas(): Devuelve el numero de filas de la matriz\n",
" columnas(): Devuelve el nomero de columnas de la matriz\n",
" determinante(): Devuelve el determinante de la matriz\n",
" gauss(): Por método de eliminación Gaussiana\n",
" Calcula la matriz inversa para matrices cuadradas y con determinante\n",
" Calcula la pseudo inversa por la derecha para matrices no cuadradas\n",
" Yields:\n",
" filas(): Un escalar\n",
" columnas(): Un escalar\n",
" determinante(): Un escalar\n",
" gauss()\n",
" \n",
" '''\n",
" def __init__ (self,matriz):\n",
" self.matriz=np.array(matriz)\n",
" self.no=0\n",
" \n",
" def filas(self):\n",
" return(len(self.matriz))\n",
" \n",
" def columnas(self):\n",
" return(len(self.matriz[0]))\n",
" \n",
" def determinante(self):\n",
" if self.filas()==self.columnas():\n",
" return(np.linalg.det(self.matriz))\n",
" else:\n",
" print(\"Matriz no cuadrada, no existe determinante\")\n",
" self.no=1\n",
" return(self.no)\n",
" \n",
" def gauss(self):\n",
" self.determinante()\n",
" if (self.no==0 and self.determinante()!=0):\n",
" print(\"Determinante distinto de cero:\\nMatriz invertible!\")\n",
" \n",
" #Operaciones\n",
" mAuxiliar=np.identity(self.filas())\n",
" print(mAuxiliar)\n",
" piv = []\n",
" linea=[]\n",
" lineaux=[]\n",
" pivaux=[]\n",
" \n",
" for fila in range(self.filas()):\n",
" for columna in range(self.columnas()):\n",
" if fila==columna:\n",
" print(\"Entra fila=columna\"+ str(fila))\n",
" aux=None\n",
" aux1=None\n",
" aux2=None\n",
" auxR=None\n",
" \n",
" #print(self.matriz[fila][columna])\n",
" aux=1/self.matriz[fila][columna]\n",
" #print(aux)\n",
" #print(\"aqui\")\n",
" #print(self.matriz[fila,:])\n",
" #print(self.matriz[fila,:]*aux)\n",
" self.matriz[fila,:]=self.matriz[fila,:]*aux\n",
" #print(\"aca\")\n",
" #print(self.matriz[fila,:])\n",
" mAuxiliar[fila,:]=mAuxiliar[fila,:]*aux\n",
" aux1=self.matriz[fila,:]\n",
" aux2=mAuxiliar[fila,:]\n",
" linea.append(aux1)\n",
" piv.append(self.matriz[fila][columna])\n",
" lineaux.append(aux2)\n",
" pivaux.append(mAuxiliar[fila][columna])\n",
" #print(\"Resultados\")\n",
" #print(piv)\n",
" #print(linea)\n",
" #print(\"Sobre la identidad\")\n",
" #print(pivaux)\n",
" #print(lineaux)\n",
" #print(\"Resultados matrices\")\n",
" print(self.matriz)\n",
" print(mAuxiliar)\n",
"\n",
" elif fila>columna:\n",
" print(\"Entra fila>columna\"+ str(fila))\n",
" auxR=self.matriz[fila][columna]\n",
" \n",
" #print(\"preuba\")\n",
" #print(linea[columna]*-auxR)\n",
" self.matriz[fila,:]=self.matriz[fila,:]+(linea[columna]*-auxR)\n",
" mAuxiliar[fila,:]=mAuxiliar[fila,:]+(lineaux[columna]*-auxR)\n",
" \n",
" print(\"Resultados matrices\")\n",
" print(self.matriz)\n",
" print(mAuxiliar)\n",
" \n",
" \n",
" else:\n",
" pass\n",
" print(\"intermedio\")\n",
" piv = []\n",
" linea=[]\n",
" lineaux=[]\n",
" pivaux=[] \n",
" \n",
" a=max(range(self.columnas()))\n",
" for columna in range(a,-1,-1):\n",
" for fila in range(a,-1,-1):\n",
" if fila==columna:\n",
" linea.append(self.matriz[fila,:])\n",
" lineaux.append(mAuxiliar[fila,:]) \n",
" \n",
" elif fila>columna:\n",
" pass\n",
" \n",
" elif fila<columna:\n",
" piv.append(self.matriz[fila][columna])\n",
" self.matriz[fila,:]=self.matriz[fila,:]+(linea[-1]*-piv[-1])\n",
" mAuxiliar[fila,:]=mAuxiliar[fila,:]+(lineaux[-1]*-piv[-1])\n",
" print(\"Resultados matrices\")\n",
" print(self.matriz)\n",
" print(mAuxiliar)\n",
" print(\"La inversa es: \\n\")\n",
" return(mAuxiliar)\n",
" \n",
" \n",
" elif self.determinante()==0:\n",
" print(\"Determinante igual a cero:\\nMatriz no invertible\")\n",
" return(0)\n",
" \n",
" elif self.no==1:\n",
" print(\"Matriz no cuadrada:\\nMatriz no invertible\")\n",
" print(\"Inicia proceso de pseudoinversa\")\n",
"\n",
" original=self.matriz\n",
" x=mmult1(self.matriz,original.T)\n",
" x=Matriz(x)\n",
" #print(x)\n",
" #print(self.matriz)\n",
" inve=x.gauss()\n",
" #print(inve)\n",
" soluD=mmult1(original.T,inve)\n",
" #print(soluD)\n",
" \n",
" \n",
" #Por la izquierda\n",
" #original=self.matriz\n",
" #y=mmult1(original.T,self.matriz)\n",
" #y=Matriz(y)\n",
" #print(y)\n",
" #print(self.matriz)\n",
" #inve=y.gauss()\n",
" #print(inve)\n",
" #soluI=mmult1(inve,original.T)\n",
" \n",
" return(soluD)\n",
" \n",
" \n",
" def __str__ (self):\n",
" return(\"Soy la matriz:\\n\"+ str(self.matriz))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"''' \n",
" else:\n",
" #original=self.matriz\n",
" #x=mmult1(original.T,self.matriz)\n",
" #x=Matriz(x)\n",
" #print(x)\n",
" #print(self.matriz)\n",
" #inve=x.gauss\n",
" #print(inve)\n",
" #mmult1(inve,original.T)\n",
" pass'''"
]
},
{
"cell_type": "code",
"execution_count": 1545,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Soy la matriz:\n",
"[[1400. 970.]\n",
" [ 970. 878.]]\n",
"Soy la matriz:\n",
"[[ 1 2 3]\n",
" [ 4 5 6]\n",
" [ 7 8 10]]\n",
"Soy la matriz:\n",
"[[10. 20. 30.]\n",
" [18. 5. 23.]]\n",
"Soy la matriz:\n",
"[[ 1. 1. 0. 2. -1.]\n",
" [ 0. 0. 2. -1. 2.]\n",
" [ 0. 2. -3. 4. 5.]\n",
" [ 1. 0. -1. 1. 1.]\n",
" [ 0. 0. 0. 0. 4.]]\n"
]
}
],
"source": [
"mmatriz=Matriz(A)\n",
"mmatriz1=Matriz(x)\n",
"mmatriz2=Matriz(y)\n",
"mmatriz3=Matriz(z)\n",
"mmatriz4=Matriz(w)\n",
"print(mmatriz1)\n",
"print(mmatriz2)\n",
"print(mmatriz3)\n",
"print(mmatriz4)"
]
},
{
"cell_type": "code",
"execution_count": 1483,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Soy la matriz:\n",
"[[1400. 970.]\n",
" [ 970. 878.]]\n",
"288300.0\n",
"2\n",
"2\n",
"Determinante distinto de cero:\n",
"Matriz invertible!\n",
"[[1. 0.]\n",
" [0. 1.]]\n",
"Entra fila=columna0\n",
"[[1.00000000e+00 6.92857143e-01]\n",
" [9.70000000e+02 8.78000000e+02]]\n",
"[[7.14285714e-04 0.00000000e+00]\n",
" [0.00000000e+00 1.00000000e+00]]\n",
"Entra fila>columna1\n",
"Resultados matrices\n",
"[[ 1. 0.69285714]\n",
" [ 0. 205.92857143]]\n",
"[[ 7.14285714e-04 0.00000000e+00]\n",
" [-6.92857143e-01 1.00000000e+00]]\n",
"Entra fila=columna1\n",
"[[1. 0.69285714]\n",
" [0. 1. ]]\n",
"[[ 0.00071429 0. ]\n",
" [-0.00336455 0.00485605]]\n",
"intermedio\n",
"Resultados matrices\n",
"[[1.00000000e+00 1.11022302e-16]\n",
" [0.00000000e+00 1.00000000e+00]]\n",
"[[ 0.00304544 -0.00336455]\n",
" [-0.00336455 0.00485605]]\n",
"La inversa es: \n",
"\n"
]
},
{
"data": {
"text/plain": [
"array([[ 0.00304544, -0.00336455],\n",
" [-0.00336455, 0.00485605]])"
]
},
"execution_count": 1483,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(mmatriz1)\n",
"print(mmatriz1.determinante())\n",
"print(mmatriz1.filas())\n",
"print(mmatriz1.columnas())\n",
"mmatriz1.gauss()"
]
},
{
"cell_type": "code",
"execution_count": 1484,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Soy la matriz:\n",
"[[ 1 2 3]\n",
" [ 4 5 6]\n",
" [ 7 8 10]]\n",
"-2.9999999999999996\n",
"3\n",
"3\n",
"Determinante distinto de cero:\n",
"Matriz invertible!\n",
"[[1. 0. 0.]\n",
" [0. 1. 0.]\n",
" [0. 0. 1.]]\n",
"Entra fila=columna0\n",
"[[ 1 2 3]\n",
" [ 4 5 6]\n",
" [ 7 8 10]]\n",
"[[1. 0. 0.]\n",
" [0. 1. 0.]\n",
" [0. 0. 1.]]\n",
"Entra fila>columna1\n",
"Resultados matrices\n",
"[[ 1 2 3]\n",
" [ 0 -3 -6]\n",
" [ 7 8 10]]\n",
"[[ 1. 0. 0.]\n",
" [-4. 1. 0.]\n",
" [ 0. 0. 1.]]\n",
"Entra fila=columna1\n",
"[[ 1 2 3]\n",
" [ 0 1 2]\n",
" [ 7 8 10]]\n",
"[[ 1. 0. 0. ]\n",
" [ 1.33333333 -0.33333333 -0. ]\n",
" [ 0. 0. 1. ]]\n",
"Entra fila>columna2\n",
"Resultados matrices\n",
"[[ 1 2 3]\n",
" [ 0 1 2]\n",
" [ 0 -6 -11]]\n",
"[[ 1. 0. 0. ]\n",
" [ 1.33333333 -0.33333333 -0. ]\n",
" [-7. 0. 1. ]]\n",
"Entra fila>columna2\n",
"Resultados matrices\n",
"[[1 2 3]\n",
" [0 1 2]\n",
" [0 0 1]]\n",
"[[ 1. 0. 0. ]\n",
" [ 1.33333333 -0.33333333 -0. ]\n",
" [ 1. -2. 1. ]]\n",
"Entra fila=columna2\n",
"[[1 2 3]\n",
" [0 1 2]\n",
" [0 0 1]]\n",
"[[ 1. 0. 0. ]\n",
" [ 1.33333333 -0.33333333 -0. ]\n",
" [ 1. -2. 1. ]]\n",
"intermedio\n",
"Resultados matrices\n",
"[[1 2 3]\n",
" [0 1 0]\n",
" [0 0 1]]\n",
"[[ 1. 0. 0. ]\n",
" [-0.66666667 3.66666667 -2. ]\n",
" [ 1. -2. 1. ]]\n",
"Resultados matrices\n",
"[[1 2 0]\n",
" [0 1 0]\n",
" [0 0 1]]\n",
"[[-2. 6. -3. ]\n",
" [-0.66666667 3.66666667 -2. ]\n",
" [ 1. -2. 1. ]]\n",
"Resultados matrices\n",
"[[1 0 0]\n",
" [0 1 0]\n",
" [0 0 1]]\n",
"[[-0.66666667 -1.33333333 1. ]\n",
" [-0.66666667 3.66666667 -2. ]\n",
" [ 1. -2. 1. ]]\n",
"La inversa es: \n",
"\n"
]
},
{
"data": {
"text/plain": [
"array([[-0.66666667, -1.33333333, 1. ],\n",
" [-0.66666667, 3.66666667, -2. ],\n",
" [ 1. , -2. , 1. ]])"
]
},
"execution_count": 1484,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(mmatriz2)\n",
"print(mmatriz2.determinante())\n",
"print(mmatriz2.filas())\n",
"print(mmatriz2.columnas())\n",
"mmatriz2.gauss()"
]
},
{
"cell_type": "code",
"execution_count": 1546,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Soy la matriz:\n",
"[[10. 20. 30.]\n",
" [18. 5. 23.]]\n",
"Matriz no cuadrada, no existe determinante\n",
"Matriz no cuadrada, no existe determinante\n",
"Matriz no cuadrada:\n",
"Matriz no invertible\n",
"Inicia proceso de pseudoinversa\n",
"[[0, 0], [0, 0]]\n",
"Determinante distinto de cero:\n",
"Matriz invertible!\n",
"[[1. 0.]\n",
" [0. 1.]]\n",
"Entra fila=columna0\n",
"[[1.00000000e+00 6.92857143e-01]\n",
" [9.70000000e+02 8.78000000e+02]]\n",
"[[7.14285714e-04 0.00000000e+00]\n",
" [0.00000000e+00 1.00000000e+00]]\n",
"Entra fila>columna1\n",
"Resultados matrices\n",
"[[ 1. 0.69285714]\n",
" [ 0. 205.92857143]]\n",
"[[ 7.14285714e-04 0.00000000e+00]\n",
" [-6.92857143e-01 1.00000000e+00]]\n",
"Entra fila=columna1\n",
"[[1. 0.69285714]\n",
" [0. 1. ]]\n",
"[[ 0.00071429 0. ]\n",
" [-0.00336455 0.00485605]]\n",
"intermedio\n",
"Resultados matrices\n",
"[[1.00000000e+00 1.11022302e-16]\n",
" [0.00000000e+00 1.00000000e+00]]\n",
"[[ 0.00304544 -0.00336455]\n",
" [-0.00336455 0.00485605]]\n",
"La inversa es: \n",
"\n",
"[[0, 0], [0, 0], [0, 0]]\n"
]
},
{
"data": {
"text/plain": [
"[[-0.030107526881720432, 0.05376344086021505],\n",
" [0.044086021505376334, -0.04301075268817203],\n",
" [0.013978494623655913, 0.010752688172043015]]"
]
},
"execution_count": 1546,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(mmatriz3)\n",
"mmatriz3.gauss()"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -1199,6 +1771,15 @@
"Nota: Utilizar arreglos de Numpy\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"***Esta en la clase de arriba"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -1594,6 +2175,1555 @@
"#En series la suma es elemento a elemento poer con respecto al índice de toda la variable\n",
"(s[:6]+s[4:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.4 Ejercicios\n",
"Los siguentes ejercicios de realizann con los dato de *iris.csv *"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" <th>species</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width species\n",
"0 5.1 3.5 1.4 0.2 setosa\n",
"1 4.9 3.0 1.4 0.2 setosa\n",
"2 4.7 3.2 1.3 0.2 setosa\n",
"3 4.6 3.1 1.5 0.2 setosa\n",
"4 5.0 3.6 1.4 0.2 setosa"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"iris = pd.read_csv('data/iris.csv')\n",
"iris.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.1 ¿Cual es el numero de observaciones en el conjunto de datos?"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"150"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(iris)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.2 ¿Cual es el numero de columnas en el conjunto de datos?"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n",
"['sepal_length' 'sepal_width' 'petal_length' 'petal_width' 'species']\n"
]
}
],
"source": [
"nombres=iris.columns.values\n",
"print(len(nombres))\n",
"print(nombres)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.4 ¿Cual es el nombre de la columna 4? (numero, no indice)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"petal_width\n"
]
}
],
"source": [
"print(nombres[3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.5 Selecciona las columnas, \"sepal_length\" y \"petal_length\""
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>petal_length</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>1.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5.4</td>\n",
" <td>1.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>4.6</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>5.0</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>4.4</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>4.9</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length petal_length\n",
"0 5.1 1.4\n",
"1 4.9 1.4\n",
"2 4.7 1.3\n",
"3 4.6 1.5\n",
"4 5.0 1.4\n",
"5 5.4 1.7\n",
"6 4.6 1.4\n",
"7 5.0 1.5\n",
"8 4.4 1.4\n",
"9 4.9 1.5"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[[\"sepal_length\",\"petal_length\"]].head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.6 Selecciona las filas en donde \"sepal_length\" sea mayor a 4.8"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>120</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>121</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>122</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>123</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>124</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>125</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>126</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>127</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>128</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>129</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>130</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>131</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>132</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>133</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>134</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>135</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>136</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>137</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>138</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>139</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>140</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>141</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>142</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>143</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>144</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal_length\n",
"0 True\n",
"1 True\n",
"2 False\n",
"3 False\n",
"4 True\n",
"5 True\n",
"6 False\n",
"7 True\n",
"8 False\n",
"9 True\n",
"10 True\n",
"11 False\n",
"12 False\n",
"13 False\n",
"14 True\n",
"15 True\n",
"16 True\n",
"17 True\n",
"18 True\n",
"19 True\n",
"20 True\n",
"21 True\n",
"22 False\n",
"23 True\n",
"24 False\n",
"25 True\n",
"26 True\n",
"27 True\n",
"28 True\n",
"29 False\n",
".. ...\n",
"120 True\n",
"121 True\n",
"122 True\n",
"123 True\n",
"124 True\n",
"125 True\n",
"126 True\n",
"127 True\n",
"128 True\n",
"129 True\n",
"130 True\n",
"131 True\n",
"132 True\n",
"133 True\n",
"134 True\n",
"135 True\n",
"136 True\n",
"137 True\n",
"138 True\n",
"139 True\n",
"140 True\n",
"141 True\n",
"142 True\n",
"143 True\n",
"144 True\n",
"145 True\n",
"146 True\n",
"147 True\n",
"148 True\n",
"149 True\n",
"\n",
"[150 rows x 1 columns]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[[\"sepal_length\"]]>4.8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.7 Agrega una nueva columna que sea la muliplicacion de \"petal_length\" por \"petal_width\""
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" <th>species</th>\n",
" <th>multipicacion</th>\n",
" <th>multiplicacion</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.28</td>\n",
" <td>0.28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.28</td>\n",
" <td>0.28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.26</td>\n",
" <td>0.26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.30</td>\n",
" <td>0.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.28</td>\n",
" <td>0.28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5.4</td>\n",
" <td>3.9</td>\n",
" <td>1.7</td>\n",
" <td>0.4</td>\n",
" <td>setosa</td>\n",
" <td>0.68</td>\n",
" <td>0.68</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>4.6</td>\n",
" <td>3.4</td>\n",
" <td>1.4</td>\n",
" <td>0.3</td>\n",
" <td>setosa</td>\n",
" <td>0.42</td>\n",
" <td>0.42</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>5.0</td>\n",
" <td>3.4</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.30</td>\n",
" <td>0.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>4.4</td>\n",
" <td>2.9</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.28</td>\n",
" <td>0.28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>4.9</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.1</td>\n",
" <td>setosa</td>\n",
" <td>0.15</td>\n",
" <td>0.15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>5.4</td>\n",
" <td>3.7</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.30</td>\n",
" <td>0.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>4.8</td>\n",
" <td>3.4</td>\n",
" <td>1.6</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.32</td>\n",
" <td>0.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>4.8</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.1</td>\n",
" <td>setosa</td>\n",
" <td>0.14</td>\n",
" <td>0.14</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>4.3</td>\n",
" <td>3.0</td>\n",
" <td>1.1</td>\n",
" <td>0.1</td>\n",
" <td>setosa</td>\n",
" <td>0.11</td>\n",
" <td>0.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>5.8</td>\n",
" <td>4.0</td>\n",
" <td>1.2</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.24</td>\n",
" <td>0.24</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>5.7</td>\n",
" <td>4.4</td>\n",
" <td>1.5</td>\n",
" <td>0.4</td>\n",
" <td>setosa</td>\n",
" <td>0.60</td>\n",
" <td>0.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>5.4</td>\n",
" <td>3.9</td>\n",
" <td>1.3</td>\n",
" <td>0.4</td>\n",
" <td>setosa</td>\n",
" <td>0.52</td>\n",
" <td>0.52</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.3</td>\n",
" <td>setosa</td>\n",
" <td>0.42</td>\n",
" <td>0.42</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>5.7</td>\n",
" <td>3.8</td>\n",
" <td>1.7</td>\n",
" <td>0.3</td>\n",
" <td>setosa</td>\n",
" <td>0.51</td>\n",
" <td>0.51</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>5.1</td>\n",
" <td>3.8</td>\n",
" <td>1.5</td>\n",
" <td>0.3</td>\n",
" <td>setosa</td>\n",
" <td>0.45</td>\n",
" <td>0.45</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>5.4</td>\n",
" <td>3.4</td>\n",
" <td>1.7</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.34</td>\n",
" <td>0.34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>5.1</td>\n",
" <td>3.7</td>\n",
" <td>1.5</td>\n",
" <td>0.4</td>\n",
" <td>setosa</td>\n",
" <td>0.60</td>\n",
" <td>0.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>4.6</td>\n",
" <td>3.6</td>\n",
" <td>1.0</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.20</td>\n",
" <td>0.20</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>5.1</td>\n",
" <td>3.3</td>\n",
" <td>1.7</td>\n",
" <td>0.5</td>\n",
" <td>setosa</td>\n",
" <td>0.85</td>\n",
" <td>0.85</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>4.8</td>\n",
" <td>3.4</td>\n",
" <td>1.9</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.38</td>\n",
" <td>0.38</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>5.0</td>\n",
" <td>3.0</td>\n",
" <td>1.6</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.32</td>\n",
" <td>0.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>5.0</td>\n",
" <td>3.4</td>\n",
" <td>1.6</td>\n",
" <td>0.4</td>\n",
" <td>setosa</td>\n",
" <td>0.64</td>\n",
" <td>0.64</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>5.2</td>\n",
" <td>3.5</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.30</td>\n",
" <td>0.30</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>5.2</td>\n",
" <td>3.4</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.28</td>\n",
" <td>0.28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.6</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" <td>0.32</td>\n",
" <td>0.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>120</th>\n",
" <td>6.9</td>\n",
" <td>3.2</td>\n",
" <td>5.7</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" <td>13.11</td>\n",
" <td>13.11</td>\n",
" </tr>\n",
" <tr>\n",
" <th>121</th>\n",
" <td>5.6</td>\n",
" <td>2.8</td>\n",
" <td>4.9</td>\n",
" <td>2.0</td>\n",
" <td>virginica</td>\n",
" <td>9.80</td>\n",
" <td>9.80</td>\n",
" </tr>\n",
" <tr>\n",
" <th>122</th>\n",
" <td>7.7</td>\n",
" <td>2.8</td>\n",
" <td>6.7</td>\n",
" <td>2.0</td>\n",
" <td>virginica</td>\n",
" <td>13.40</td>\n",
" <td>13.40</td>\n",
" </tr>\n",
" <tr>\n",
" <th>123</th>\n",
" <td>6.3</td>\n",
" <td>2.7</td>\n",
" <td>4.9</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" <td>8.82</td>\n",
" <td>8.82</td>\n",
" </tr>\n",
" <tr>\n",
" <th>124</th>\n",
" <td>6.7</td>\n",
" <td>3.3</td>\n",
" <td>5.7</td>\n",
" <td>2.1</td>\n",
" <td>virginica</td>\n",
" <td>11.97</td>\n",
" <td>11.97</td>\n",
" </tr>\n",
" <tr>\n",
" <th>125</th>\n",
" <td>7.2</td>\n",
" <td>3.2</td>\n",
" <td>6.0</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" <td>10.80</td>\n",
" <td>10.80</td>\n",
" </tr>\n",
" <tr>\n",
" <th>126</th>\n",
" <td>6.2</td>\n",
" <td>2.8</td>\n",
" <td>4.8</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" <td>8.64</td>\n",
" <td>8.64</td>\n",
" </tr>\n",
" <tr>\n",
" <th>127</th>\n",
" <td>6.1</td>\n",
" <td>3.0</td>\n",
" <td>4.9</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" <td>8.82</td>\n",
" <td>8.82</td>\n",
" </tr>\n",
" <tr>\n",
" <th>128</th>\n",
" <td>6.4</td>\n",
" <td>2.8</td>\n",
" <td>5.6</td>\n",
" <td>2.1</td>\n",
" <td>virginica</td>\n",
" <td>11.76</td>\n",
" <td>11.76</td>\n",
" </tr>\n",
" <tr>\n",
" <th>129</th>\n",
" <td>7.2</td>\n",
" <td>3.0</td>\n",
" <td>5.8</td>\n",
" <td>1.6</td>\n",
" <td>virginica</td>\n",
" <td>9.28</td>\n",
" <td>9.28</td>\n",
" </tr>\n",
" <tr>\n",
" <th>130</th>\n",
" <td>7.4</td>\n",
" <td>2.8</td>\n",
" <td>6.1</td>\n",
" <td>1.9</td>\n",
" <td>virginica</td>\n",
" <td>11.59</td>\n",
" <td>11.59</td>\n",
" </tr>\n",
" <tr>\n",
" <th>131</th>\n",
" <td>7.9</td>\n",
" <td>3.8</td>\n",
" <td>6.4</td>\n",
" <td>2.0</td>\n",
" <td>virginica</td>\n",
" <td>12.80</td>\n",
" <td>12.80</td>\n",
" </tr>\n",
" <tr>\n",
" <th>132</th>\n",
" <td>6.4</td>\n",
" <td>2.8</td>\n",
" <td>5.6</td>\n",
" <td>2.2</td>\n",
" <td>virginica</td>\n",
" <td>12.32</td>\n",
" <td>12.32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>133</th>\n",
" <td>6.3</td>\n",
" <td>2.8</td>\n",
" <td>5.1</td>\n",
" <td>1.5</td>\n",
" <td>virginica</td>\n",
" <td>7.65</td>\n",
" <td>7.65</td>\n",
" </tr>\n",
" <tr>\n",
" <th>134</th>\n",
" <td>6.1</td>\n",
" <td>2.6</td>\n",
" <td>5.6</td>\n",
" <td>1.4</td>\n",
" <td>virginica</td>\n",
" <td>7.84</td>\n",
" <td>7.84</td>\n",
" </tr>\n",
" <tr>\n",
" <th>135</th>\n",
" <td>7.7</td>\n",
" <td>3.0</td>\n",
" <td>6.1</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" <td>14.03</td>\n",
" <td>14.03</td>\n",
" </tr>\n",
" <tr>\n",
" <th>136</th>\n",
" <td>6.3</td>\n",
" <td>3.4</td>\n",
" <td>5.6</td>\n",
" <td>2.4</td>\n",
" <td>virginica</td>\n",
" <td>13.44</td>\n",
" <td>13.44</td>\n",
" </tr>\n",
" <tr>\n",
" <th>137</th>\n",
" <td>6.4</td>\n",
" <td>3.1</td>\n",
" <td>5.5</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" <td>9.90</td>\n",
" <td>9.90</td>\n",
" </tr>\n",
" <tr>\n",
" <th>138</th>\n",
" <td>6.0</td>\n",
" <td>3.0</td>\n",
" <td>4.8</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" <td>8.64</td>\n",
" <td>8.64</td>\n",
" </tr>\n",
" <tr>\n",
" <th>139</th>\n",
" <td>6.9</td>\n",
" <td>3.1</td>\n",
" <td>5.4</td>\n",
" <td>2.1</td>\n",
" <td>virginica</td>\n",
" <td>11.34</td>\n",
" <td>11.34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>140</th>\n",
" <td>6.7</td>\n",
" <td>3.1</td>\n",
" <td>5.6</td>\n",
" <td>2.4</td>\n",
" <td>virginica</td>\n",
" <td>13.44</td>\n",
" <td>13.44</td>\n",
" </tr>\n",
" <tr>\n",
" <th>141</th>\n",
" <td>6.9</td>\n",
" <td>3.1</td>\n",
" <td>5.1</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" <td>11.73</td>\n",
" <td>11.73</td>\n",
" </tr>\n",
" <tr>\n",
" <th>142</th>\n",
" <td>5.8</td>\n",
" <td>2.7</td>\n",
" <td>5.1</td>\n",
" <td>1.9</td>\n",
" <td>virginica</td>\n",
" <td>9.69</td>\n",
" <td>9.69</td>\n",
" </tr>\n",
" <tr>\n",
" <th>143</th>\n",
" <td>6.8</td>\n",
" <td>3.2</td>\n",
" <td>5.9</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" <td>13.57</td>\n",
" <td>13.57</td>\n",
" </tr>\n",
" <tr>\n",
" <th>144</th>\n",
" <td>6.7</td>\n",
" <td>3.3</td>\n",
" <td>5.7</td>\n",
" <td>2.5</td>\n",
" <td>virginica</td>\n",
" <td>14.25</td>\n",
" <td>14.25</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>6.7</td>\n",
" <td>3.0</td>\n",
" <td>5.2</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" <td>11.96</td>\n",
" <td>11.96</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>6.3</td>\n",
" <td>2.5</td>\n",
" <td>5.0</td>\n",
" <td>1.9</td>\n",
" <td>virginica</td>\n",
" <td>9.50</td>\n",
" <td>9.50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>6.5</td>\n",
" <td>3.0</td>\n",
" <td>5.2</td>\n",
" <td>2.0</td>\n",
" <td>virginica</td>\n",
" <td>10.40</td>\n",
" <td>10.40</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>6.2</td>\n",
" <td>3.4</td>\n",
" <td>5.4</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" <td>12.42</td>\n",
" <td>12.42</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>5.9</td>\n",
" <td>3.0</td>\n",
" <td>5.1</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" <td>9.18</td>\n",
" <td>9.18</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width species \\\n",
"0 5.1 3.5 1.4 0.2 setosa \n",
"1 4.9 3.0 1.4 0.2 setosa \n",
"2 4.7 3.2 1.3 0.2 setosa \n",
"3 4.6 3.1 1.5 0.2 setosa \n",
"4 5.0 3.6 1.4 0.2 setosa \n",
"5 5.4 3.9 1.7 0.4 setosa \n",
"6 4.6 3.4 1.4 0.3 setosa \n",
"7 5.0 3.4 1.5 0.2 setosa \n",
"8 4.4 2.9 1.4 0.2 setosa \n",
"9 4.9 3.1 1.5 0.1 setosa \n",
"10 5.4 3.7 1.5 0.2 setosa \n",
"11 4.8 3.4 1.6 0.2 setosa \n",
"12 4.8 3.0 1.4 0.1 setosa \n",
"13 4.3 3.0 1.1 0.1 setosa \n",
"14 5.8 4.0 1.2 0.2 setosa \n",
"15 5.7 4.4 1.5 0.4 setosa \n",
"16 5.4 3.9 1.3 0.4 setosa \n",
"17 5.1 3.5 1.4 0.3 setosa \n",
"18 5.7 3.8 1.7 0.3 setosa \n",
"19 5.1 3.8 1.5 0.3 setosa \n",
"20 5.4 3.4 1.7 0.2 setosa \n",
"21 5.1 3.7 1.5 0.4 setosa \n",
"22 4.6 3.6 1.0 0.2 setosa \n",
"23 5.1 3.3 1.7 0.5 setosa \n",
"24 4.8 3.4 1.9 0.2 setosa \n",
"25 5.0 3.0 1.6 0.2 setosa \n",
"26 5.0 3.4 1.6 0.4 setosa \n",
"27 5.2 3.5 1.5 0.2 setosa \n",
"28 5.2 3.4 1.4 0.2 setosa \n",
"29 4.7 3.2 1.6 0.2 setosa \n",
".. ... ... ... ... ... \n",
"120 6.9 3.2 5.7 2.3 virginica \n",
"121 5.6 2.8 4.9 2.0 virginica \n",
"122 7.7 2.8 6.7 2.0 virginica \n",
"123 6.3 2.7 4.9 1.8 virginica \n",
"124 6.7 3.3 5.7 2.1 virginica \n",
"125 7.2 3.2 6.0 1.8 virginica \n",
"126 6.2 2.8 4.8 1.8 virginica \n",
"127 6.1 3.0 4.9 1.8 virginica \n",
"128 6.4 2.8 5.6 2.1 virginica \n",
"129 7.2 3.0 5.8 1.6 virginica \n",
"130 7.4 2.8 6.1 1.9 virginica \n",
"131 7.9 3.8 6.4 2.0 virginica \n",
"132 6.4 2.8 5.6 2.2 virginica \n",
"133 6.3 2.8 5.1 1.5 virginica \n",
"134 6.1 2.6 5.6 1.4 virginica \n",
"135 7.7 3.0 6.1 2.3 virginica \n",
"136 6.3 3.4 5.6 2.4 virginica \n",
"137 6.4 3.1 5.5 1.8 virginica \n",
"138 6.0 3.0 4.8 1.8 virginica \n",
"139 6.9 3.1 5.4 2.1 virginica \n",
"140 6.7 3.1 5.6 2.4 virginica \n",
"141 6.9 3.1 5.1 2.3 virginica \n",
"142 5.8 2.7 5.1 1.9 virginica \n",
"143 6.8 3.2 5.9 2.3 virginica \n",
"144 6.7 3.3 5.7 2.5 virginica \n",
"145 6.7 3.0 5.2 2.3 virginica \n",
"146 6.3 2.5 5.0 1.9 virginica \n",
"147 6.5 3.0 5.2 2.0 virginica \n",
"148 6.2 3.4 5.4 2.3 virginica \n",
"149 5.9 3.0 5.1 1.8 virginica \n",
"\n",
" multipicacion multiplicacion \n",
"0 0.28 0.28 \n",
"1 0.28 0.28 \n",
"2 0.26 0.26 \n",
"3 0.30 0.30 \n",
"4 0.28 0.28 \n",
"5 0.68 0.68 \n",
"6 0.42 0.42 \n",
"7 0.30 0.30 \n",
"8 0.28 0.28 \n",
"9 0.15 0.15 \n",
"10 0.30 0.30 \n",
"11 0.32 0.32 \n",
"12 0.14 0.14 \n",
"13 0.11 0.11 \n",
"14 0.24 0.24 \n",
"15 0.60 0.60 \n",
"16 0.52 0.52 \n",
"17 0.42 0.42 \n",
"18 0.51 0.51 \n",
"19 0.45 0.45 \n",
"20 0.34 0.34 \n",
"21 0.60 0.60 \n",
"22 0.20 0.20 \n",
"23 0.85 0.85 \n",
"24 0.38 0.38 \n",
"25 0.32 0.32 \n",
"26 0.64 0.64 \n",
"27 0.30 0.30 \n",
"28 0.28 0.28 \n",
"29 0.32 0.32 \n",
".. ... ... \n",
"120 13.11 13.11 \n",
"121 9.80 9.80 \n",
"122 13.40 13.40 \n",
"123 8.82 8.82 \n",
"124 11.97 11.97 \n",
"125 10.80 10.80 \n",
"126 8.64 8.64 \n",
"127 8.82 8.82 \n",
"128 11.76 11.76 \n",
"129 9.28 9.28 \n",
"130 11.59 11.59 \n",
"131 12.80 12.80 \n",
"132 12.32 12.32 \n",
"133 7.65 7.65 \n",
"134 7.84 7.84 \n",
"135 14.03 14.03 \n",
"136 13.44 13.44 \n",
"137 9.90 9.90 \n",
"138 8.64 8.64 \n",
"139 11.34 11.34 \n",
"140 13.44 13.44 \n",
"141 11.73 11.73 \n",
"142 9.69 9.69 \n",
"143 13.57 13.57 \n",
"144 14.25 14.25 \n",
"145 11.96 11.96 \n",
"146 9.50 9.50 \n",
"147 10.40 10.40 \n",
"148 12.42 12.42 \n",
"149 9.18 9.18 \n",
"\n",
"[150 rows x 7 columns]"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[\"multiplicacion\"]=iris[\"petal_length\"]*iris[\"petal_width\"]\n",
"iris"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.8 Encuentra el promedio por especie de cada columna"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" </tr>\n",
" <tr>\n",
" <th>species</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>setosa</th>\n",
" <td>5.006</td>\n",
" <td>3.418</td>\n",
" <td>1.464</td>\n",
" <td>0.244</td>\n",
" </tr>\n",
" <tr>\n",
" <th>versicolor</th>\n",
" <td>5.936</td>\n",
" <td>2.770</td>\n",
" <td>4.260</td>\n",
" <td>1.326</td>\n",
" </tr>\n",
" <tr>\n",
" <th>virginica</th>\n",
" <td>6.588</td>\n",
" <td>2.974</td>\n",
" <td>5.552</td>\n",
" <td>2.026</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width\n",
"species \n",
"setosa 5.006 3.418 1.464 0.244\n",
"versicolor 5.936 2.770 4.260 1.326\n",
"virginica 6.588 2.974 5.552 2.026"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.groupby('species').mean()"
]
}
],
"metadata": {
......
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![scipy](https://cdn-images-1.medium.com/max/1600/1*Y2v3PrF1rUQRUHwOcXJznA.png)\n",
"\n",
"# 5.SciPy\n",
"SciPy es un ecosistema para computo cientifico en python, esta constriuido sobre los arreglos de NumPy. Scipy incluye herramientas como Matplotlib, pandas , SymPy y scikit-learn. \n",
"\n",
"## 5.1 NumPy\n",
"NumPy es la base para todos los paquetes de computo científico en python, provee soporte para arreglos multidimensionales y matrices, junto con una amplia coleccion de funciones matematicas de alto nivel para operar con estos arreglos.\n",
"\n",
"### 5.1.1 numpy.array \n",
"El tipo de dato mas importante de numpy es **numpy.array** sus atibutos mas importantes son:\n",
"* numpy.array.**ndim**: -numero de dimensiones del arreglo.\n",
"* numpy.array.**shape**: Un tupla indicando el tamaño del arreglo en cada dimension.\n",
"* numpy.array.**size**: El numero total elementos en el arreglo.\n",
"* numpy.array.**dtype**: El tipo de elemenos en el arreglo e.g. numpy.int32, numpy.int16, and numpy.float64.\n",
"* numpy.array.**itemsize**: el tamaño en bytes de cada elemento del arrglo.\n",
"* numpy.array.**data**: El bloque de memoria que contiene los datos del arreglo.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]]\n",
"(3, 5)\n",
"2\n",
"int64\n"
]
}
],
"source": [
"import numpy as np\n",
"a = np.array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14]])\n",
"print(a)\n",
"print(a.shape)\n",
"print(a.ndim)\n",
"print(a.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.+0.j, 2.+0.j],\n",
" [3.+0.j, 4.+0.j]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = np.array( [ [1,2], [3,4] ], dtype=complex )\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0.],\n",
" [0., 0., 0., 0.],\n",
" [0., 0., 0., 0.]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" np.zeros( (3,4) )\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones( (3,4)) "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.empty( (3,4) ) "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 1.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.eye(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operaciones Basicas"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([20,30,40,50] )\n",
"b = np.arange( 4 )"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([20, 29, 38, 47])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Suma\n",
"a-b"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 40, 60, 80, 100])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Multiplicacion por Escalar\n",
"a*2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 4, 9])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Potencia\n",
"b**2"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, False])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Operadores Boleanos\n",
"a<50"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 30, 80, 150])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Multiplicacion por elemento\n",
"a*b"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"66"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c= np.arange(12).reshape(3,4)\n",
"c.sum()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([12, 15, 18, 21])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=0) # Suma por Columna"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6, 22, 38])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=1) #Suma por Fila"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[**Producto punto**](https://en.wikipedia.org/wiki/Dot_product) y [**Multiplicacion Matricial**](https://en.wikipedia.org/wiki/Matrix_multiplication)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a@b == a.dot(b)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 0], [0, 1]])\n",
"B = np.array([[4, 1], [2, 2]]) "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True],\n",
" [ True, True]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.matmul(A, B) == A.dot(B)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Elementos, filas, columnas y submatrices."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [10, 11, 12, 13],\n",
" [20, 21, 22, 23],\n",
" [30, 31, 32, 33],\n",
" [40, 41, 42, 43]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f(x,y):\n",
" return 10*x+y\n",
"B = np.fromfunction(f,(5,4),dtype=int)\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[0,3]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 11, 12, 13])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[1,:]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 11, 21, 31, 41])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[:,1]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2],\n",
" [11, 12]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[:2,1:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Iterando elementos"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3]\n",
"[10 11 12 13]\n",
"[20 21 22 23]\n",
"[30 31 32 33]\n",
"[40 41 42 43]\n"
]
}
],
"source": [
"for row in B:\n",
" print(row)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"10\n",
"11\n",
"12\n",
"13\n",
"20\n",
"21\n",
"22\n",
"23\n",
"30\n",
"31\n",
"32\n",
"33\n",
"40\n",
"41\n",
"42\n",
"43\n"
]
}
],
"source": [
"for element in B.flat:\n",
" print(element)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cambio de forma"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 2., 9., 4.],\n",
" [6., 6., 3., 6.],\n",
" [7., 9., 9., 8.]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.floor(10*np.random.random((3,4)))\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 2.],\n",
" [9., 4.],\n",
" [6., 6.],\n",
" [3., 6.],\n",
" [7., 9.],\n",
" [9., 8.]])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape(6,2)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 6., 7.],\n",
" [2., 6., 9.],\n",
" [9., 3., 9.],\n",
" [4., 6., 8.]])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.T"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True]])"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.transpose()==a.T"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4, 3)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.T.shape"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 2., 9., 4.],\n",
" [6., 6., 3., 6.],\n",
" [7., 9., 9., 8.]])"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# La dimensión con -1 se calcula automaticamente\n",
"a.reshape(3,-1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.2 Ejercicos\n",
"\n",
"### 5.2.1 Sin utilizar numpy escribe una funcion para obten el producto punto de dos vectores."
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"a = [2, 5.6, 9, 8, 10]\n",
"b = [1, 3, 2.4, 2, 11]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.2 Sin utilizar numpy escribe una funcion que obtenga la multiplicacion de dos matrices.\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"A = [[1,2,3],[4,5,6]]\n",
"B = [[7,8],[9,10],[11,12]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.3 Utiliza numpy para probar que las dos funciones anteriores dan el resultado correcto."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.4 Utilizando solo lo visto hasta el momento de numpy escribe una funcion que encuentre la inversa de una matriz por el metodo de Gauss-Jordan.\n",
"[Wikipedia](https://en.wikipedia.org/wiki/Gaussian_elimination): En matemáticas, la eliminación de Gauss Jordan, llamada así en honor de Carl Friedrich Gauss y Wilhelm Jordan es un algoritmo del álgebra lineal que se usa para determinar las soluciones de un sistema de ecuaciones lineales, para encontrar matrices e inversas. Un sistema de ecuaciones se resuelve por el método de Gauss cuando se obtienen sus soluciones mediante la reducción del sistema dado a otro equivalente en el que cada ecuación tiene una incógnita menos que la anterior. El método de Gauss transforma la matriz de coeficientes en una matriz triangular superior. El método de Gauss-Jordan continúa el proceso de transformación hasta obtener una matriz diagonal"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.5 Utilizando la funcion anterior escribe otra que obtenga la pseduo-inversa de una matriz."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.3 Pandas\n",
"En python, pandas es una biblioteca de software escrita como extensión de NumPy para manipulación y análisis de datos. En particular, ofrece estructuras de datos y operaciones para manipular tablas numéricas y series temporales.\n",
"and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. Su objetivo es ser un bloque de construccion fundamental para realizar analisis de datos en el mundo real.\n",
"El nombre de la biblioteca deriva del término \"datos de panel\" (PANel DAta), término de econometría que designa datos que combinan una dimensión temporal con otra dimensión transversal.\n",
"\n",
"Pandas tiene dos typos de datos principales, **Series** (1D) y **DataFrame** (2D), *Dataframe* es un contenedr para *Series* y *Series* es un contenedor de escalares. \n",
"\n",
"### 5.3.1 Series\n",
"Series es un arreglo unidimensional etiquetado capaz de contener cualquier tipo de dato (Enteros, cadenas, punto flotante, objetos, etc), El eje de etiquetas es llamado indice (**index**).\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a -0.192610\n",
"b 0.957230\n",
"c -0.917233\n",
"d 1.156523\n",
"e -0.539532\n",
"dtype: float64"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 -0.856313\n",
"1 -0.342923\n",
"2 -1.398583\n",
"3 0.170869\n",
"4 1.049196\n",
"dtype: float64"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.Series(np.random.randn(5))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0\n",
"b 1\n",
"c 2\n",
"dtype: int64"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'b': 1, 'a': 0, 'c': 2}\n",
"pd.Series(d)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b 1.0\n",
"c 2.0\n",
"d NaN\n",
"a 0.0\n",
"dtype: float64"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'a': 0., 'b': 1., 'c': 2.}\n",
"pd.Series(d, index=['b', 'c', 'd', 'a'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Las Series son compatibles con *numpy.array* y *dict*"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.19261011238315226"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[0]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a -0.192610\n",
"b 0.957230\n",
"c -0.917233\n",
"dtype: float64"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[:3]"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b 0.957230\n",
"d 1.156523\n",
"dtype: float64"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[s>s.mean()]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a -0.385220\n",
"b 1.914460\n",
"c -1.834466\n",
"d 2.313046\n",
"e -1.079065\n",
"dtype: float64"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s*2"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a False\n",
"b True\n",
"c False\n",
"d True\n",
"e False\n",
"dtype: bool"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s>s.median()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.19261011238315226"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[\"a\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Alieneacion Automatica"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"a = np.array(range(10))\n",
"s = pd.Series(a)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0\n",
"1 1\n",
"2 2\n",
"3 3\n",
"4 4\n",
"5 5\n",
"6 6\n",
"7 7\n",
"8 8\n",
"9 9\n",
"dtype: int64"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 4, 6, 8, 10, 12, 14])"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a[:6]+a[4:])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 NaN\n",
"1 NaN\n",
"2 NaN\n",
"3 NaN\n",
"4 8.0\n",
"5 10.0\n",
"6 NaN\n",
"7 NaN\n",
"8 NaN\n",
"9 NaN\n",
"dtype: float64"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(s[:6]+s[4:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.3.2 DataFrame\n",
"DataFrame es un estructura bidimensiona etiquetada con columnas que pueden ser de diferentes tipos, es el objeto mas usado en pandas, se puede pensar en ella como un diccionario de **Series**. **DataFrame** acepta diferentes tipos de entradas como:\n",
"* Diccionarios de arreglos unidimensionales, listas dicionarios o series. \n",
"* 2-D numpy.ndarray\n",
"* Series\n",
"* Otro **DataFrame**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),\n",
" 'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(d)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>two</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>4.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one two\n",
"a 1.0 1.0\n",
"b 2.0 2.0\n",
"c 3.0 3.0\n",
"d NaN 4.0"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Seleccionar, Añadir y Borrar Columnas"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 1.0\n",
"b 2.0\n",
"c 3.0\n",
"d NaN\n",
"Name: one, dtype: float64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"one\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>two</th>\n",
" <th>three</th>\n",
" <th>flag</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>2.0</td>\n",
" <td>4.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>3.0</td>\n",
" <td>9.0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>4.0</td>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one two three flag\n",
"a 1.0 1.0 1.0 False\n",
"b 2.0 2.0 4.0 False\n",
"c 3.0 3.0 9.0 True\n",
"d NaN 4.0 NaN False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['three'] = df['one'] * df['two']\n",
"df['flag'] = df['one'] > 2\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"del df['two']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"three = df.pop('three')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>flag</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one flag\n",
"a 1.0 False\n",
"b 2.0 False\n",
"c 3.0 True\n",
"d NaN False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>three</th>\n",
" <th>flag</th>\n",
" <th>thrwe</th>\n",
" <th>thre</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>4.0</td>\n",
" <td>False</td>\n",
" <td>4.0</td>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>9.0</td>\n",
" <td>True</td>\n",
" <td>9.0</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one three flag thrwe thre\n",
"a 1.0 1.0 False 1.0 1.0\n",
"b 2.0 4.0 False 4.0 4.0\n",
"c 3.0 9.0 True 9.0 9.0\n",
"d NaN NaN False NaN NaN"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.insert(3, \"thrwe\", three)\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Indexado y Selección\n",
"\n",
"| Operación | Sintaxis | Resultado |\n",
"|:----------|:--------:|:----------|\n",
"| Selección de Columna | df[col] | Series |\n",
"| Selección de fila por etiqueta | df.loc[label] | Series |\n",
"| Selección de fila por posición | df.iloc[loc] | Series |\n",
"| Rango de filas | df[5:10] | DataFrame |\n",
"| Selección de filas por vector booleano | df[bool_vec] | DataFrame |\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"one 1\n",
"three 1\n",
"flag False\n",
"thrwe 1\n",
"thre 1\n",
"Name: a, dtype: object"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['a']"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"one 3\n",
"three 9\n",
"flag True\n",
"thrwe 9\n",
"thre 9\n",
"Name: c, dtype: object"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iloc[2]"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>three</th>\n",
" <th>flag</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>4.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one three flag\n",
"a 1.0 1.0 False\n",
"b 2.0 4.0 False"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.4 Ejercicios\n",
"Los siguentes ejercicios de realizann con los dato de *iris.csv *"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" <th>species</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width species\n",
"0 5.1 3.5 1.4 0.2 setosa\n",
"1 4.9 3.0 1.4 0.2 setosa\n",
"2 4.7 3.2 1.3 0.2 setosa\n",
"3 4.6 3.1 1.5 0.2 setosa\n",
"4 5.0 3.6 1.4 0.2 setosa"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"iris = pd.read_csv('data/iris.csv')\n",
"iris.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.1 ¿Cual es el numero de observaciones en el conjunto de datos?"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"150"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(iris)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.2 ¿Cual es el numero de columnas en el conjunto de datos?"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"nombres=iris.columns.values\n",
"print(len(nombres))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.3 Imprime el nombre de todas las columnas"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['sepal_length' 'sepal_width' 'petal_length' 'petal_width' 'species']\n"
]
}
],
"source": [
"nombres=iris.columns.values\n",
"print(nombres)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.4 ¿Cual es el nombre de la columna 4?"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"species\n"
]
}
],
"source": [
"print(nombres[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.5 Selecciona las columnas, \"sepal_length\" y \"petal_length\""
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>petal_length</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>1.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5.4</td>\n",
" <td>1.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>4.6</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>5.0</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>4.4</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>4.9</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length petal_length\n",
"0 5.1 1.4\n",
"1 4.9 1.4\n",
"2 4.7 1.3\n",
"3 4.6 1.5\n",
"4 5.0 1.4\n",
"5 5.4 1.7\n",
"6 4.6 1.4\n",
"7 5.0 1.5\n",
"8 4.4 1.4\n",
"9 4.9 1.5"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[[\"sepal_length\",\"petal_length\"]].head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.6 Selecciona las filas en donde \"sepal_length\" sea mayor a 4.8"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>120</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>121</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>122</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>123</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>124</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>125</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>126</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>127</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>128</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>129</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>130</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>131</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>132</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>133</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>134</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>135</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>136</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>137</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>138</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>139</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>140</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>141</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>142</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>143</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>144</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal_length\n",
"0 True\n",
"1 True\n",
"2 False\n",
"3 False\n",
"4 True\n",
"5 True\n",
"6 False\n",
"7 True\n",
"8 False\n",
"9 True\n",
"10 True\n",
"11 False\n",
"12 False\n",
"13 False\n",
"14 True\n",
"15 True\n",
"16 True\n",
"17 True\n",
"18 True\n",
"19 True\n",
"20 True\n",
"21 True\n",
"22 False\n",
"23 True\n",
"24 False\n",
"25 True\n",
"26 True\n",
"27 True\n",
"28 True\n",
"29 False\n",
".. ...\n",
"120 True\n",
"121 True\n",
"122 True\n",
"123 True\n",
"124 True\n",
"125 True\n",
"126 True\n",
"127 True\n",
"128 True\n",
"129 True\n",
"130 True\n",
"131 True\n",
"132 True\n",
"133 True\n",
"134 True\n",
"135 True\n",
"136 True\n",
"137 True\n",
"138 True\n",
"139 True\n",
"140 True\n",
"141 True\n",
"142 True\n",
"143 True\n",
"144 True\n",
"145 True\n",
"146 True\n",
"147 True\n",
"148 True\n",
"149 True\n",
"\n",
"[150 rows x 1 columns]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[[\"sepal_length\"]]>4.8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.7 Agrega una nueva columna que sea la muliplicacion de \"petal_length\" por \"petal_width\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.8 Encuentra el promedio por especie de cada columna."
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" </tr>\n",
" <tr>\n",
" <th>species</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>setosa</th>\n",
" <td>5.006</td>\n",
" <td>3.418</td>\n",
" <td>1.464</td>\n",
" <td>0.244</td>\n",
" </tr>\n",
" <tr>\n",
" <th>versicolor</th>\n",
" <td>5.936</td>\n",
" <td>2.770</td>\n",
" <td>4.260</td>\n",
" <td>1.326</td>\n",
" </tr>\n",
" <tr>\n",
" <th>virginica</th>\n",
" <td>6.588</td>\n",
" <td>2.974</td>\n",
" <td>5.552</td>\n",
" <td>2.026</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width\n",
"species \n",
"setosa 5.006 3.418 1.464 0.244\n",
"versicolor 5.936 2.770 4.260 1.326\n",
"virginica 6.588 2.974 5.552 2.026"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.groupby('species').mean()"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
......@@ -462,7 +462,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [
{
......@@ -470,24 +470,55 @@
"output_type": "stream",
"text": [
"Escriba su texto: \n",
"hola mundo hola\n",
"['hola', 'mundo', 'hola']\n",
"{'hola': 2, 'mundo': 1}\n"
"hola holito holon hola\n",
"['hola', 'holito', 'holon', 'hola']\n"
]
},
{
"data": {
"text/plain": [
"OrderedDict([('hola', 2), ('holito', 1), ('holon', 1)])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dicti={}\n",
"\n",
"from collections import OrderedDict\n",
"def ejercicio6():\n",
" dicti={}\n",
" texto=str(input(\"Escriba su texto: \\n\"))\n",
" texto=texto.split()\n",
" print(texto)\n",
" for palabra in texto:\n",
" dicti.update({palabra:texto.count(palabra)})\n",
" print(dicti)\n",
" sorted_dicti=OrderedDict(sorted(dicti.items(), key=lambda t: t[1],reverse=True))\n",
" return(sorted_dicti)\n",
" \n",
"ejercicio6()"
"x=ejercicio6()\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[\"hola\"]"
]
},
{
......
This source diff could not be displayed because it is too large. You can view the blob instead.
El aprendizaje automático o aprendizaje automatizado o aprendizaje de máquinas (del inglés, machine learning) es el subcampo de las ciencias de la computación y una rama de la inteligencia artificial, cuyo objetivo es desarrollar técnicas que permitan que las computadoras aprendan. De forma más concreta, se trata de crear programas capaces de generalizar comportamientos a partir de una información suministrada en forma de ejemplos.
Es, por lo tanto, un proceso de inducción del conocimiento. En muchas ocasiones el campo de actuación del aprendizaje automático se solapa con el de la estadística computacional, ya que las dos disciplinas se basan en el análisis de datos. Sin embargo, el aprendizaje automático también se centra en el estudio de la complejidad computacional de los problemas. Muchos problemas son de clase NP-hard, por lo que gran parte de la investigación realizada en aprendizaje automático está enfocada al diseño de soluciones factibles a esos problemas. El aprendizaje automático puede ser visto como un intento de automatizar algunas partes del método científico mediante métodos matemáticos.
El aprendizaje automático tiene una amplia gama de aplicaciones, incluyendo motores de búsqueda, diagnósticos médicos, detección de fraude en el uso de tarjetas de crédito, análisis del mercado de valores, clasificación de secuencias de ADN, reconocimiento del habla y del lenguaje escrito, juegos y robótica.
L'apprentissage automatique (en anglais machine learning, littéralement « l'apprentissage machine ») ou apprentissage statistique est un champ d'étude de l'intelligence artificielle qui se fonde sur des approches statistiques pour donner aux ordinateurs la capacité d' « apprendre » à partir de données, c'est-à-dire d'améliorer leurs performances à résoudre des tâches sans être explicitement programmés pour chacune. Plus largement, cela concerne la conception, l'analyse, le développement et l'implémentation de telles méthodes.
L'apprentissage automatique comporte généralement deux phases. La première consiste à estimer un modèle à partir de données, appelées observations, qui sont disponibles et en nombre fini, lors de la phase de conception du système. L'estimation du modèle consiste à résoudre une tâche pratique, telle que traduire un discours, estimer une densité de probabilité, reconnaître la présence d'un chat dans une photographie ou participer à la conduite d'un véhicule autonome. Cette phase dite « d'apprentissage » ou « d'entraînement » est généralement réalisée préalablement à l'utilisation pratique du modèle. La seconde phase correspond à la mise en production : le modèle étant déterminé, de nouvelles données peuvent alors être soumises afin d'obtenir le résultat correspondant à la tâche souhaitée. En pratique, certains systèmes peuvent poursuivre leur apprentissage une fois en production, pour peu qu'ils aient un moyen d'obtenir un retour sur la qualité des résultats produits.
Selon les informations disponibles durant la phase d'apprentissage, l'apprentissage est qualifié de différentes manières. Si les données sont étiquetées (c'est-à-dire que la réponse à la tâche est connue pour ces données), il s'agit d'un apprentissage supervisé. On parle de classification ou de classement1 si les étiquettes sont discrètes, ou de régression si elles sont continues. Si le modèle est appris de manière incrémentale en fonction d'une récompense reçue par le programme pour chacune des actions entreprises, on parle d'apprentissage par renforcement. Dans le cas le plus général, sans étiquette, on cherche à déterminer la structure sous-jacente des données (qui peuvent être une densité de probabilité) et il s'agit alors d'apprentissage non supervisé. L'apprentissage automatique peut être appliqué à différents types de données, tels des graphes, des arbres, des courbes, ou plus simplement des vecteurs de caractéristiques, qui peuvent être continues ou discrètes.
Random forest (o random forests) también conocidos en castellano como '"Bosques Aleatorios"' es una combinación de árboles predictores tal que cada árbol depende de los valores de un vector aleatorio probado independientemente y con la misma distribución para cada uno de estos. Es una modificación sustancial de bagging que construye una larga colección de árboles no correlacionados y luego los promedia.
El algoritmo para inducir un random forest fue desarrollado por Leo Breiman1​ y Adele Cutler y Random forests es su marca de fábrica. El término aparece de la primera propuesta de Random decision forests, hecha por Tin Kam Ho de Bell Labs en 1995. El método combina la idea de bagging de Breiman y la selección aleatoria de atributos, introducida independientemente por Ho,2​3​ Amit y Geman,4​ para construir una colección de árboles de decisión con variación controlada.
La selección de un subconjunto aleatorio de atributos es un ejemplo del método random subspace, el que, según la formulación de Ho, es una manera de llevar a cabo la discriminación estocástica5​ propuesta por Eugenio Kleinberg.
En muchos problemas el rendimiento del algoritmo random forest es muy similar a la del boosting, y es más simple de entrenar y ajustar. Como consecuencia, el Random forest es popular y ampliamente utilizado.
Les forêts d'arbres décisionnels1 (ou forêts aléatoires de l'anglais random forest classifier) ont été formellement proposées en 2001 par Leo Breiman et Adèle Cutler. Elles font partie des techniques d'apprentissage automatique. Cet algorithme combine les concepts de sous-espaces aléatoires et de bagging. L'algorithme des forêts d'arbres décisionnels effectue un apprentissage sur de multiples arbres de décision entraînés sur des sous-ensembles de données légèrement différents.
sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa
5.0,3.6,1.4,0.2,setosa
5.4,3.9,1.7,0.4,setosa
4.6,3.4,1.4,0.3,setosa
5.0,3.4,1.5,0.2,setosa
4.4,2.9,1.4,0.2,setosa
4.9,3.1,1.5,0.1,setosa
5.4,3.7,1.5,0.2,setosa
4.8,3.4,1.6,0.2,setosa
4.8,3.0,1.4,0.1,setosa
4.3,3.0,1.1,0.1,setosa
5.8,4.0,1.2,0.2,setosa
5.7,4.4,1.5,0.4,setosa
5.4,3.9,1.3,0.4,setosa
5.1,3.5,1.4,0.3,setosa
5.7,3.8,1.7,0.3,setosa
5.1,3.8,1.5,0.3,setosa
5.4,3.4,1.7,0.2,setosa
5.1,3.7,1.5,0.4,setosa
4.6,3.6,1.0,0.2,setosa
5.1,3.3,1.7,0.5,setosa
4.8,3.4,1.9,0.2,setosa
5.0,3.0,1.6,0.2,setosa
5.0,3.4,1.6,0.4,setosa
5.2,3.5,1.5,0.2,setosa
5.2,3.4,1.4,0.2,setosa
4.7,3.2,1.6,0.2,setosa
4.8,3.1,1.6,0.2,setosa
5.4,3.4,1.5,0.4,setosa
5.2,4.1,1.5,0.1,setosa
5.5,4.2,1.4,0.2,setosa
4.9,3.1,1.5,0.1,setosa
5.0,3.2,1.2,0.2,setosa
5.5,3.5,1.3,0.2,setosa
4.9,3.1,1.5,0.1,setosa
4.4,3.0,1.3,0.2,setosa
5.1,3.4,1.5,0.2,setosa
5.0,3.5,1.3,0.3,setosa
4.5,2.3,1.3,0.3,setosa
4.4,3.2,1.3,0.2,setosa
5.0,3.5,1.6,0.6,setosa
5.1,3.8,1.9,0.4,setosa
4.8,3.0,1.4,0.3,setosa
5.1,3.8,1.6,0.2,setosa
4.6,3.2,1.4,0.2,setosa
5.3,3.7,1.5,0.2,setosa
5.0,3.3,1.4,0.2,setosa
7.0,3.2,4.7,1.4,versicolor
6.4,3.2,4.5,1.5,versicolor
6.9,3.1,4.9,1.5,versicolor
5.5,2.3,4.0,1.3,versicolor
6.5,2.8,4.6,1.5,versicolor
5.7,2.8,4.5,1.3,versicolor
6.3,3.3,4.7,1.6,versicolor
4.9,2.4,3.3,1.0,versicolor
6.6,2.9,4.6,1.3,versicolor
5.2,2.7,3.9,1.4,versicolor
5.0,2.0,3.5,1.0,versicolor
5.9,3.0,4.2,1.5,versicolor
6.0,2.2,4.0,1.0,versicolor
6.1,2.9,4.7,1.4,versicolor
5.6,2.9,3.6,1.3,versicolor
6.7,3.1,4.4,1.4,versicolor
5.6,3.0,4.5,1.5,versicolor
5.8,2.7,4.1,1.0,versicolor
6.2,2.2,4.5,1.5,versicolor
5.6,2.5,3.9,1.1,versicolor
5.9,3.2,4.8,1.8,versicolor
6.1,2.8,4.0,1.3,versicolor
6.3,2.5,4.9,1.5,versicolor
6.1,2.8,4.7,1.2,versicolor
6.4,2.9,4.3,1.3,versicolor
6.6,3.0,4.4,1.4,versicolor
6.8,2.8,4.8,1.4,versicolor
6.7,3.0,5.0,1.7,versicolor
6.0,2.9,4.5,1.5,versicolor
5.7,2.6,3.5,1.0,versicolor
5.5,2.4,3.8,1.1,versicolor
5.5,2.4,3.7,1.0,versicolor
5.8,2.7,3.9,1.2,versicolor
6.0,2.7,5.1,1.6,versicolor
5.4,3.0,4.5,1.5,versicolor
6.0,3.4,4.5,1.6,versicolor
6.7,3.1,4.7,1.5,versicolor
6.3,2.3,4.4,1.3,versicolor
5.6,3.0,4.1,1.3,versicolor
5.5,2.5,4.0,1.3,versicolor
5.5,2.6,4.4,1.2,versicolor
6.1,3.0,4.6,1.4,versicolor
5.8,2.6,4.0,1.2,versicolor
5.0,2.3,3.3,1.0,versicolor
5.6,2.7,4.2,1.3,versicolor
5.7,3.0,4.2,1.2,versicolor
5.7,2.9,4.2,1.3,versicolor
6.2,2.9,4.3,1.3,versicolor
5.1,2.5,3.0,1.1,versicolor
5.7,2.8,4.1,1.3,versicolor
6.3,3.3,6.0,2.5,virginica
5.8,2.7,5.1,1.9,virginica
7.1,3.0,5.9,2.1,virginica
6.3,2.9,5.6,1.8,virginica
6.5,3.0,5.8,2.2,virginica
7.6,3.0,6.6,2.1,virginica
4.9,2.5,4.5,1.7,virginica
7.3,2.9,6.3,1.8,virginica
6.7,2.5,5.8,1.8,virginica
7.2,3.6,6.1,2.5,virginica
6.5,3.2,5.1,2.0,virginica
6.4,2.7,5.3,1.9,virginica
6.8,3.0,5.5,2.1,virginica
5.7,2.5,5.0,2.0,virginica
5.8,2.8,5.1,2.4,virginica
6.4,3.2,5.3,2.3,virginica
6.5,3.0,5.5,1.8,virginica
7.7,3.8,6.7,2.2,virginica
7.7,2.6,6.9,2.3,virginica
6.0,2.2,5.0,1.5,virginica
6.9,3.2,5.7,2.3,virginica
5.6,2.8,4.9,2.0,virginica
7.7,2.8,6.7,2.0,virginica
6.3,2.7,4.9,1.8,virginica
6.7,3.3,5.7,2.1,virginica
7.2,3.2,6.0,1.8,virginica
6.2,2.8,4.8,1.8,virginica
6.1,3.0,4.9,1.8,virginica
6.4,2.8,5.6,2.1,virginica
7.2,3.0,5.8,1.6,virginica
7.4,2.8,6.1,1.9,virginica
7.9,3.8,6.4,2.0,virginica
6.4,2.8,5.6,2.2,virginica
6.3,2.8,5.1,1.5,virginica
6.1,2.6,5.6,1.4,virginica
7.7,3.0,6.1,2.3,virginica
6.3,3.4,5.6,2.4,virginica
6.4,3.1,5.5,1.8,virginica
6.0,3.0,4.8,1.8,virginica
6.9,3.1,5.4,2.1,virginica
6.7,3.1,5.6,2.4,virginica
6.9,3.1,5.1,2.3,virginica
5.8,2.7,5.1,1.9,virginica
6.8,3.2,5.9,2.3,virginica
6.7,3.3,5.7,2.5,virginica
6.7,3.0,5.2,2.3,virginica
6.3,2.5,5.0,1.9,virginica
6.5,3.0,5.2,2.0,virginica
6.2,3.4,5.4,2.3,virginica
5.9,3.0,5.1,1.8,virginica
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![scipy](https://cdn-images-1.medium.com/max/1600/1*Y2v3PrF1rUQRUHwOcXJznA.png)\n",
"\n",
"# 5.SciPy\n",
"SciPy es un ecosistema para computo cientifico en python, esta constriuido sobre los arreglos de NumPy. Scipy incluye herramientas como Matplotlib, pandas , SymPy y scikit-learn. \n",
"\n",
"## 5.1 NumPy\n",
"NumPy es la base para todos los paquetes de computo científico en python, provee soporte para arreglos multidimensionales y matrices, junto con una amplia coleccion de funciones matematicas de alto nivel para operar con estos arreglos.\n",
"\n",
"### 5.1.1 numpy.array \n",
"El tipo de dato mas importante de numpy es **numpy.array** sus atibutos mas importantes son:\n",
"* numpy.array.**ndim**: -numero de dimensiones del arreglo.\n",
"* numpy.array.**shape**: Un tupla indicando el tamaño del arreglo en cada dimension.\n",
"* numpy.array.**size**: El numero total elementos en el arreglo.\n",
"* numpy.array.**dtype**: El tipo de elemenos en el arreglo e.g. numpy.int32, numpy.int16, and numpy.float64.\n",
"* numpy.array.**itemsize**: el tamaño en bytes de cada elemento del arrglo.\n",
"* numpy.array.**data**: El bloque de memoria que contiene los datos del arreglo.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0 1 2 3 4]\n",
" [ 5 6 7 8 9]\n",
" [10 11 12 13 14]]\n",
"(3, 5)\n",
"2\n",
"int64\n"
]
}
],
"source": [
"import numpy as np\n",
"a = np.array([[ 0, 1, 2, 3, 4],\n",
" [ 5, 6, 7, 8, 9],\n",
" [10, 11, 12, 13, 14]])\n",
"print(a)\n",
"print(a.shape)\n",
"print(a.ndim)\n",
"print(a.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.+0.j, 2.+0.j],\n",
" [3.+0.j, 4.+0.j]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = np.array( [ [1,2], [3,4] ], dtype=complex )\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0.],\n",
" [0., 0., 0., 0.],\n",
" [0., 0., 0., 0.]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" np.zeros( (3,4) )\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones( (3,4)) "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.empty( (3,4) ) "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 1.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.eye(5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operaciones Basicas"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([20,30,40,50] )\n",
"b = np.arange( 4 )"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([20, 29, 38, 47])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Suma\n",
"a-b"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 40, 60, 80, 100])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Multiplicacion por Escalar\n",
"a*2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 4, 9])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Potencia\n",
"b**2"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, False])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Operadores Boleanos\n",
"a<50"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 30, 80, 150])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Multiplicacion por elemento\n",
"a*b"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"66"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c= np.arange(12).reshape(3,4)\n",
"c.sum()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([12, 15, 18, 21])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=0) # Suma por Columna"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6, 22, 38])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=1) #Suma por Fila"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[**Producto punto**](https://en.wikipedia.org/wiki/Dot_product) y [**Multiplicacion Matricial**](https://en.wikipedia.org/wiki/Matrix_multiplication)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a@b == a.dot(b)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 0], [0, 1]])\n",
"B = np.array([[4, 1], [2, 2]]) "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True],\n",
" [ True, True]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.matmul(A, B) == A.dot(B)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Elementos, filas, columnas y submatrices."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3],\n",
" [10, 11, 12, 13],\n",
" [20, 21, 22, 23],\n",
" [30, 31, 32, 33],\n",
" [40, 41, 42, 43]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f(x,y):\n",
" return 10*x+y\n",
"B = np.fromfunction(f,(5,4),dtype=int)\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[0,3]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 11, 12, 13])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[1,:]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 11, 21, 31, 41])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[:,1]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2],\n",
" [11, 12]])"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B[:2,1:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Iterando elementos"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3]\n",
"[10 11 12 13]\n",
"[20 21 22 23]\n",
"[30 31 32 33]\n",
"[40 41 42 43]\n"
]
}
],
"source": [
"for row in B:\n",
" print(row)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"10\n",
"11\n",
"12\n",
"13\n",
"20\n",
"21\n",
"22\n",
"23\n",
"30\n",
"31\n",
"32\n",
"33\n",
"40\n",
"41\n",
"42\n",
"43\n"
]
}
],
"source": [
"for element in B.flat:\n",
" print(element)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cambio de forma"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 2., 9., 4.],\n",
" [6., 6., 3., 6.],\n",
" [7., 9., 9., 8.]])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.floor(10*np.random.random((3,4)))\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 2.],\n",
" [9., 4.],\n",
" [6., 6.],\n",
" [3., 6.],\n",
" [7., 9.],\n",
" [9., 8.]])"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape(6,2)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 6., 7.],\n",
" [2., 6., 9.],\n",
" [9., 3., 9.],\n",
" [4., 6., 8.]])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.T"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True]])"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.transpose()==a.T"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4, 3)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.T.shape"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[2., 2., 9., 4.],\n",
" [6., 6., 3., 6.],\n",
" [7., 9., 9., 8.]])"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# La dimensión con -1 se calcula automaticamente\n",
"a.reshape(3,-1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.2 Ejercicos\n",
"\n",
"### 5.2.1 Sin utilizar numpy escribe una funcion para obten el producto punto de dos vectores."
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"a = [2, 5.6, 9, 8, 10]\n",
"b = [1, 3, 2.4, 2, 11]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.2 Sin utilizar numpy escribe una funcion que obtenga la multiplicacion de dos matrices.\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"A = [[1,2,3],[4,5,6]]\n",
"B = [[7,8],[9,10],[11,12]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.3 Utiliza numpy para probar que las dos funciones anteriores dan el resultado correcto."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.4 Utilizando solo lo visto hasta el momento de numpy escribe una funcion que encuentre la inversa de una matriz por el metodo de Gauss-Jordan.\n",
"[Wikipedia](https://en.wikipedia.org/wiki/Gaussian_elimination): En matemáticas, la eliminación de Gauss Jordan, llamada así en honor de Carl Friedrich Gauss y Wilhelm Jordan es un algoritmo del álgebra lineal que se usa para determinar las soluciones de un sistema de ecuaciones lineales, para encontrar matrices e inversas. Un sistema de ecuaciones se resuelve por el método de Gauss cuando se obtienen sus soluciones mediante la reducción del sistema dado a otro equivalente en el que cada ecuación tiene una incógnita menos que la anterior. El método de Gauss transforma la matriz de coeficientes en una matriz triangular superior. El método de Gauss-Jordan continúa el proceso de transformación hasta obtener una matriz diagonal"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2.5 Utilizando la funcion anterior escribe otra que obtenga la pseduo-inversa de una matriz."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.3 Pandas\n",
"En python, pandas es una biblioteca de software escrita como extensión de NumPy para manipulación y análisis de datos. En particular, ofrece estructuras de datos y operaciones para manipular tablas numéricas y series temporales.\n",
"and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive. Su objetivo es ser un bloque de construccion fundamental para realizar analisis de datos en el mundo real.\n",
"El nombre de la biblioteca deriva del término \"datos de panel\" (PANel DAta), término de econometría que designa datos que combinan una dimensión temporal con otra dimensión transversal.\n",
"\n",
"Pandas tiene dos typos de datos principales, **Series** (1D) y **DataFrame** (2D), *Dataframe* es un contenedr para *Series* y *Series* es un contenedor de escalares. \n",
"\n",
"### 5.3.1 Series\n",
"Series es un arreglo unidimensional etiquetado capaz de contener cualquier tipo de dato (Enteros, cadenas, punto flotante, objetos, etc), El eje de etiquetas es llamado indice (**index**).\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a -0.192610\n",
"b 0.957230\n",
"c -0.917233\n",
"d 1.156523\n",
"e -0.539532\n",
"dtype: float64"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = pd.Series(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 -0.856313\n",
"1 -0.342923\n",
"2 -1.398583\n",
"3 0.170869\n",
"4 1.049196\n",
"dtype: float64"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.Series(np.random.randn(5))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0\n",
"b 1\n",
"c 2\n",
"dtype: int64"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'b': 1, 'a': 0, 'c': 2}\n",
"pd.Series(d)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b 1.0\n",
"c 2.0\n",
"d NaN\n",
"a 0.0\n",
"dtype: float64"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'a': 0., 'b': 1., 'c': 2.}\n",
"pd.Series(d, index=['b', 'c', 'd', 'a'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Las Series son compatibles con *numpy.array* y *dict*"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.19261011238315226"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[0]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a -0.192610\n",
"b 0.957230\n",
"c -0.917233\n",
"dtype: float64"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[:3]"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b 0.957230\n",
"d 1.156523\n",
"dtype: float64"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[s>s.mean()]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a -0.385220\n",
"b 1.914460\n",
"c -1.834466\n",
"d 2.313046\n",
"e -1.079065\n",
"dtype: float64"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s*2"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a False\n",
"b True\n",
"c False\n",
"d True\n",
"e False\n",
"dtype: bool"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s>s.median()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.19261011238315226"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[\"a\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Alieneacion Automatica"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"a = np.array(range(10))\n",
"s = pd.Series(a)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0\n",
"1 1\n",
"2 2\n",
"3 3\n",
"4 4\n",
"5 5\n",
"6 6\n",
"7 7\n",
"8 8\n",
"9 9\n",
"dtype: int64"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 4, 6, 8, 10, 12, 14])"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a[:6]+a[4:])"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 NaN\n",
"1 NaN\n",
"2 NaN\n",
"3 NaN\n",
"4 8.0\n",
"5 10.0\n",
"6 NaN\n",
"7 NaN\n",
"8 NaN\n",
"9 NaN\n",
"dtype: float64"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(s[:6]+s[4:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.3.2 DataFrame\n",
"DataFrame es un estructura bidimensiona etiquetada con columnas que pueden ser de diferentes tipos, es el objeto mas usado en pandas, se puede pensar en ella como un diccionario de **Series**. **DataFrame** acepta diferentes tipos de entradas como:\n",
"* Diccionarios de arreglos unidimensionales, listas dicionarios o series. \n",
"* 2-D numpy.ndarray\n",
"* Series\n",
"* Otro **DataFrame**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),\n",
" 'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(d)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>two</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>4.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one two\n",
"a 1.0 1.0\n",
"b 2.0 2.0\n",
"c 3.0 3.0\n",
"d NaN 4.0"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Seleccionar, Añadir y Borrar Columnas"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 1.0\n",
"b 2.0\n",
"c 3.0\n",
"d NaN\n",
"Name: one, dtype: float64"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"one\"]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>two</th>\n",
" <th>three</th>\n",
" <th>flag</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>2.0</td>\n",
" <td>4.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>3.0</td>\n",
" <td>9.0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>4.0</td>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one two three flag\n",
"a 1.0 1.0 1.0 False\n",
"b 2.0 2.0 4.0 False\n",
"c 3.0 3.0 9.0 True\n",
"d NaN 4.0 NaN False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['three'] = df['one'] * df['two']\n",
"df['flag'] = df['one'] > 2\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"del df['two']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"three = df.pop('three')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>flag</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one flag\n",
"a 1.0 False\n",
"b 2.0 False\n",
"c 3.0 True\n",
"d NaN False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>three</th>\n",
" <th>flag</th>\n",
" <th>thrwe</th>\n",
" <th>thre</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>4.0</td>\n",
" <td>False</td>\n",
" <td>4.0</td>\n",
" <td>4.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\n",
" <td>3.0</td>\n",
" <td>9.0</td>\n",
" <td>True</td>\n",
" <td>9.0</td>\n",
" <td>9.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>d</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one three flag thrwe thre\n",
"a 1.0 1.0 False 1.0 1.0\n",
"b 2.0 4.0 False 4.0 4.0\n",
"c 3.0 9.0 True 9.0 9.0\n",
"d NaN NaN False NaN NaN"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.insert(3, \"thrwe\", three)\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Indexado y Selección\n",
"\n",
"| Operación | Sintaxis | Resultado |\n",
"|:----------|:--------:|:----------|\n",
"| Selección de Columna | df[col] | Series |\n",
"| Selección de fila por etiqueta | df.loc[label] | Series |\n",
"| Selección de fila por posición | df.iloc[loc] | Series |\n",
"| Rango de filas | df[5:10] | DataFrame |\n",
"| Selección de filas por vector booleano | df[bool_vec] | DataFrame |\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"one 1\n",
"three 1\n",
"flag False\n",
"thrwe 1\n",
"thre 1\n",
"Name: a, dtype: object"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['a']"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"one 3\n",
"three 9\n",
"flag True\n",
"thrwe 9\n",
"thre 9\n",
"Name: c, dtype: object"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iloc[2]"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>one</th>\n",
" <th>three</th>\n",
" <th>flag</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>a</th>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>b</th>\n",
" <td>2.0</td>\n",
" <td>4.0</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one three flag\n",
"a 1.0 1.0 False\n",
"b 2.0 4.0 False"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[:2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5.4 Ejercicios\n",
"Los siguentes ejercicios de realizann con los dato de *iris.csv *"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" <th>species</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>setosa</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width species\n",
"0 5.1 3.5 1.4 0.2 setosa\n",
"1 4.9 3.0 1.4 0.2 setosa\n",
"2 4.7 3.2 1.3 0.2 setosa\n",
"3 4.6 3.1 1.5 0.2 setosa\n",
"4 5.0 3.6 1.4 0.2 setosa"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"iris = pd.read_csv('data/iris.csv')\n",
"iris.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.1 ¿Cual es el numero de observaciones en el conjunto de datos?"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"150"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(iris)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.2 ¿Cual es el numero de columnas en el conjunto de datos?"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"nombres=iris.columns.values\n",
"print(len(nombres))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.3 Imprime el nombre de todas las columnas"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['sepal_length' 'sepal_width' 'petal_length' 'petal_width' 'species']\n"
]
}
],
"source": [
"nombres=iris.columns.values\n",
"print(nombres)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.4 ¿Cual es el nombre de la columna 4?"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"species\n"
]
}
],
"source": [
"print(nombres[4])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.5 Selecciona las columnas, \"sepal_length\" y \"petal_length\""
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>petal_length</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>1.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>5.4</td>\n",
" <td>1.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>4.6</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>5.0</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>4.4</td>\n",
" <td>1.4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>4.9</td>\n",
" <td>1.5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length petal_length\n",
"0 5.1 1.4\n",
"1 4.9 1.4\n",
"2 4.7 1.3\n",
"3 4.6 1.5\n",
"4 5.0 1.4\n",
"5 5.4 1.7\n",
"6 4.6 1.4\n",
"7 5.0 1.5\n",
"8 4.4 1.4\n",
"9 4.9 1.5"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[[\"sepal_length\",\"petal_length\"]].head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.6 Selecciona las filas en donde \"sepal_length\" sea mayor a 4.8"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>25</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>26</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>28</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29</th>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>120</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>121</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>122</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>123</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>124</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>125</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>126</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>127</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>128</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>129</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>130</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>131</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>132</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>133</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>134</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>135</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>136</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>137</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>138</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>139</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>140</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>141</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>142</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>143</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>144</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal_length\n",
"0 True\n",
"1 True\n",
"2 False\n",
"3 False\n",
"4 True\n",
"5 True\n",
"6 False\n",
"7 True\n",
"8 False\n",
"9 True\n",
"10 True\n",
"11 False\n",
"12 False\n",
"13 False\n",
"14 True\n",
"15 True\n",
"16 True\n",
"17 True\n",
"18 True\n",
"19 True\n",
"20 True\n",
"21 True\n",
"22 False\n",
"23 True\n",
"24 False\n",
"25 True\n",
"26 True\n",
"27 True\n",
"28 True\n",
"29 False\n",
".. ...\n",
"120 True\n",
"121 True\n",
"122 True\n",
"123 True\n",
"124 True\n",
"125 True\n",
"126 True\n",
"127 True\n",
"128 True\n",
"129 True\n",
"130 True\n",
"131 True\n",
"132 True\n",
"133 True\n",
"134 True\n",
"135 True\n",
"136 True\n",
"137 True\n",
"138 True\n",
"139 True\n",
"140 True\n",
"141 True\n",
"142 True\n",
"143 True\n",
"144 True\n",
"145 True\n",
"146 True\n",
"147 True\n",
"148 True\n",
"149 True\n",
"\n",
"[150 rows x 1 columns]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[[\"sepal_length\"]]>4.8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.7 Agrega una nueva columna que sea la muliplicacion de \"petal_length\" por \"petal_width\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.8 Encuentra el promedio por especie de cada columna."
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" </tr>\n",
" <tr>\n",
" <th>species</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>setosa</th>\n",
" <td>5.006</td>\n",
" <td>3.418</td>\n",
" <td>1.464</td>\n",
" <td>0.244</td>\n",
" </tr>\n",
" <tr>\n",
" <th>versicolor</th>\n",
" <td>5.936</td>\n",
" <td>2.770</td>\n",
" <td>4.260</td>\n",
" <td>1.326</td>\n",
" </tr>\n",
" <tr>\n",
" <th>virginica</th>\n",
" <td>6.588</td>\n",
" <td>2.974</td>\n",
" <td>5.552</td>\n",
" <td>2.026</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width\n",
"species \n",
"setosa 5.006 3.418 1.464 0.244\n",
"versicolor 5.936 2.770 4.260 1.326\n",
"virginica 6.588 2.974 5.552 2.026"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.groupby('species').mean()"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
testEF.png

26.4 KB

Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment