Ejercicios realizados

parent bf65b02c
......@@ -15,7 +15,7 @@
"### 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 tumpla indicando el tamaño del arreglo en cada dimension.\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",
......@@ -36,24 +36,8 @@
" [10 11 12 13 14]]\n",
"(3, 5)\n",
"2\n",
"int64\n",
"8\n",
"<memory at 0x7f7b018c0480>\n",
"[ 5 0 -5]\n",
"[ 2 -1 -2]\n",
"[ 0 -2 1]\n",
"1\n"
"int64\n"
]
},
{
"data": {
"text/plain": [
"-5"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
......@@ -64,75 +48,57 @@
"print(a)\n",
"print(a.shape)\n",
"print(a.ndim)\n",
"print(a.dtype)\n",
"print(a.itemsize)\n",
"print(a.data)\n",
"p = np.array([2, 1, -1])\n",
"q = np.array([-3, 1, 4])\n",
"r = np.array([-1, 0, 2])\n",
"o = np.array([-3, -1, 5])\n",
"\n",
"M = p-q\n",
"print(M)\n",
"V = r-q\n",
"print(V)\n",
"W = o-q\n",
"print(W)\n",
"\n",
"MxV = np.cross(M,V)\n",
"print(MxV.ndim)\n",
"MxV@W\n"
"print(a.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1.-1.j])"
"array([[1.+0.j, 2.+0.j],\n",
" [3.+0.j, 4.+0.j]])"
]
},
"execution_count": 10,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = np.array( [ [1+1j,2], [3,4+5j] ], dtype=complex )\n",
"c\n",
"d = np.array([2+1j], dtype=np.complex)\n",
"3-d"
"c = np.array( [ [1,2], [3,4] ], dtype=complex )\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 0],\n",
" [0, 0, 0, 0],\n",
" [0, 0, 0, 0]], dtype=int16)"
"array([[0., 0., 0., 0.],\n",
" [0., 0., 0., 0.],\n",
" [0., 0., 0., 0.]])"
]
},
"execution_count": 12,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a= np.zeros( (3,4), dtype=np.int16)\n",
"a"
" np.zeros( (3,4) )\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 4,
"metadata": {},
"outputs": [
{
......@@ -143,7 +109,7 @@
" [1., 1., 1., 1.]])"
]
},
"execution_count": 53,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
......@@ -154,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 5,
"metadata": {},
"outputs": [
{
......@@ -165,37 +131,37 @@
" [1., 1., 1., 1.]])"
]
},
"execution_count": 57,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.empty( (3,4) ) \n"
"np.empty( (3,4) ) "
]
},
{
"cell_type": "code",
"execution_count": 55,
"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]], dtype=int8)"
"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": 55,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.eye(5, dtype=np.int8)"
"np.eye(5)"
]
},
{
......@@ -207,26 +173,17 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3]\n"
]
}
],
"outputs": [],
"source": [
"a = np.array([20,30,40,50] )\n",
"b = np.arange( 4 )\n",
"print(b)"
"b = np.arange( 4 )"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 8,
"metadata": {},
"outputs": [
{
......@@ -235,7 +192,7 @@
"array([20, 29, 38, 47])"
]
},
"execution_count": 11,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
......@@ -247,7 +204,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"outputs": [
{
......@@ -256,19 +213,19 @@
"array([ 40, 60, 80, 100])"
]
},
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Multiplicacion Por escalar\n",
"# Multiplicacion por Escalar\n",
"a*2"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 10,
"metadata": {},
"outputs": [
{
......@@ -277,7 +234,7 @@
"array([0, 1, 4, 9])"
]
},
"execution_count": 18,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
......@@ -289,7 +246,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 11,
"metadata": {},
"outputs": [
{
......@@ -298,7 +255,7 @@
"array([ True, True, True, False])"
]
},
"execution_count": 14,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
......@@ -310,7 +267,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 12,
"metadata": {},
"outputs": [
{
......@@ -319,7 +276,7 @@
"array([ 0, 30, 80, 150])"
]
},
"execution_count": 42,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
......@@ -329,281 +286,238 @@
"a*b"
]
},
{
"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": 19,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"260"
"66"
]
},
"execution_count": 19,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a@b == a.dot(b)\n",
"a@b"
]
},
{
"cell_type": "code",
"execution_count": 277,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([[1, 1],\n",
" [1, 1]])\n",
"\n",
"b = np.array([[4, 1], \n",
" [2, 2]]) "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#np.matmul(a, b) == a.dot(b)\n",
"#np.matmul(a, b)\n",
"#help(np.dot)"
"c= np.arange(12).reshape(3,4)\n",
"c.sum()"
]
},
{
"cell_type": "code",
"execution_count": 99,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3 4 5 6 7 8]\n",
"[[0 1 2]\n",
" [3 4 5]\n",
" [6 7 8]]\n"
]
},
{
"data": {
"text/plain": [
"36"
"array([12, 15, 18, 21])"
]
},
"execution_count": 99,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c= np.arange(9).reshape(3,3)\n",
"print(np.arange(9))\n",
"print(c)\n",
"c.sum()"
"c.sum(axis=0) # Suma por Columna"
]
},
{
"cell_type": "code",
"execution_count": 100,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 9, 12, 15])"
"array([ 6, 22, 38])"
]
},
"execution_count": 100,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=0) # Suma por Columna"
"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": 101,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 3, 12, 21])"
"True"
]
},
"execution_count": 101,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=1) #Suma por Fila"
"a@b == a.dot(b)"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"### Elementos, filas, columnas y subarreglos."
"A = np.array([[1, 0], [0, 1]])\n",
"B = np.array([[4, 1], [2, 2]]) "
]
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"54\n"
]
},
{
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4],\n",
" [10, 11, 12, 13, 14],\n",
" [20, 21, 22, 23, 24],\n",
" [30, 31, 32, 33, 34],\n",
" [40, 41, 42, 43, 44],\n",
" [50, 51, 52, 53, 54]], dtype=int8)"
"array([[ True, True],\n",
" [ True, True]])"
]
},
"execution_count": 32,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def f(x,y):\n",
" return 10*x+y\n",
"print(f(5,4))\n",
"b = np.fromfunction(f,(6,5),dtype=np.int8)\n",
"b\n",
"#help(np.fromfunction)"
"np.matmul(A, B) == A.dot(B)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Elementos, filas, columnas y submatrices."
]
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
"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": 33,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[1][2] == b[1,2]"
"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": 34,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 11, 12, 13, 14], dtype=int8)"
"3"
]
},
"execution_count": 34,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[1,:]"
"B[0,3]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 12, 22, 32, 42, 52], dtype=int8)"
"array([10, 11, 12, 13])"
]
},
"execution_count": 35,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[:,2]"
"B[1,:]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 3, 4],\n",
" [13, 14],\n",
" [23, 24],\n",
" [33, 34],\n",
" [43, 44],\n",
" [53, 54]], dtype=int8)"
"array([ 1, 11, 21, 31, 41])"
]
},
"execution_count": 36,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[:,3:5]"
"B[:,1]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[20, 21, 22],\n",
" [30, 31, 32],\n",
" [40, 41, 42],\n",
" [50, 51, 52]], dtype=int8)"
"array([[ 1, 2],\n",
" [11, 12]])"
]
},
"execution_count": 37,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[2:,:3]"
"B[:2,1:3]"
]
},
{
......@@ -615,37 +529,29 @@
},
{
"cell_type": "code",
"execution_count": 177,
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3 4]\n",
"a\n",
"[10 11 12 13 14]\n",
"a\n",
"[20 21 22 23 24]\n",
"a\n",
"[30 31 32 33 34]\n",
"a\n",
"[40 41 42 43 44]\n",
"a\n",
"[50 51 52 53 54]\n",
"a\n"
"[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)\n",
" print('a')"
"for row in B:\n",
" print(row)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 25,
"metadata": {},
"outputs": [
{
......@@ -663,12 +569,20 @@
"20\n",
"21\n",
"22\n",
"23\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",
"for element in B.flat:\n",
" print(element)"
]
},
......@@ -681,40 +595,30 @@
},
{
"cell_type": "code",
"execution_count": 239,
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"float64\n"
]
},
{
"data": {
"text/plain": [
"array([[4., 4., 0.],\n",
" [8., 0., 1.],\n",
" [9., 7., 3.],\n",
" [6., 4., 3.]])"
"array([[2., 9., 8., 6.],\n",
" [8., 4., 2., 9.],\n",
" [1., 1., 4., 5.]])"
]
},
"execution_count": 239,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.floor(10*np.random.random((4,3)))\n",
"print(a.dtype)\n",
"a\n",
"#help(np.floor)"
"a = np.floor(10*np.random.random((3,4)))\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 220,
"execution_count": 27,
"metadata": {},
"outputs": [
{
......@@ -723,7 +627,7 @@
"(3, 4)"
]
},
"execution_count": 220,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
......@@ -734,73 +638,67 @@
},
{
"cell_type": "code",
"execution_count": 245,
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4., 4., 0., 8.],\n",
" [0., 1., 9., 7.],\n",
" [3., 6., 4., 3.]])"
"array([[2., 9.],\n",
" [8., 6.],\n",
" [8., 4.],\n",
" [2., 9.],\n",
" [1., 1.],\n",
" [4., 5.]])"
]
},
"execution_count": 245,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape(3,4)"
"a.reshape(6,2)"
]
},
{
"cell_type": "code",
"execution_count": 244,
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[4. 4. 0.]\n",
" [8. 0. 1.]\n",
" [9. 7. 3.]\n",
" [6. 4. 3.]]\n"
]
},
{
"data": {
"text/plain": [
"array([[4., 8., 9., 6.],\n",
" [4., 0., 7., 4.],\n",
" [0., 1., 3., 3.]])"
"array([[2., 8., 1.],\n",
" [9., 4., 1.],\n",
" [8., 2., 4.],\n",
" [6., 9., 5.]])"
]
},
"execution_count": 244,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(a)\n",
"a.T"
]
},
{
"cell_type": "code",
"execution_count": 243,
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True, True],\n",
" [ True, True, True, True],\n",
" [ True, True, True, True]])"
"array([[ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True]])"
]
},
"execution_count": 243,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
......@@ -811,57 +709,45 @@
},
{
"cell_type": "code",
"execution_count": 242,
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4, 3)\n"
]
},
{
"data": {
"text/plain": [
"(3, 4)"
"(4, 3)"
]
},
"execution_count": 242,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(a.shape)\n",
"a.T.shape"
]
},
{
"cell_type": "code",
"execution_count": 266,
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[4., 4.],\n",
" [0., 8.],\n",
" [0., 1.]],\n",
"\n",
" [[9., 7.],\n",
" [3., 6.],\n",
" [4., 3.]]])"
"array([[2., 9., 8., 6.],\n",
" [8., 4., 2., 9.],\n",
" [1., 1., 4., 5.]])"
]
},
"execution_count": 266,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# La dimencion con -1 se calcula automaticamente\n",
"a.reshape(-1,3,2) "
"# La dimensión con -1 se calcula automaticamente\n",
"a.reshape(3,-1)"
]
},
{
......@@ -875,59 +761,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trying:\n",
" A = [5, -10, 15] \n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" B = [30, 35, -40]\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" ProdPunto(A, B)\n",
"Expecting:\n",
" -800\n",
"ok\n",
"Trying:\n",
" D = [2, 5.6, 9, 8, 10] \n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" E = [1, 3, 2.4, 2, 11]\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" ProdPunto(D, E)\n",
"Expecting:\n",
" 166.39999999999998\n",
"ok\n",
"1 items had no tests:\n",
" __main__\n",
"1 items passed all tests:\n",
" 6 tests in __main__.ProdPunto\n",
"6 tests in 2 items.\n",
"6 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=6)"
]
},
"execution_count": 1,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"def ProdPunto(A,B):\n",
" '''Función que obtiene el producto punto de dos vectores A y B sin usar arreglos de numPy\n",
......@@ -955,8 +791,8 @@
" else:\n",
" print(\"Error, los vectores son de diferente longitud\", len(A), '!=', len(B))\n",
"\n",
"import doctest\n",
"doctest.testmod(verbose=True)"
"#import doctest\n",
"#doctest.testmod(verbose=True)"
]
},
{
......@@ -968,19 +804,9 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[28, 4, 5]\n",
"[8, 64, 50]\n",
"[-28, -4, -5]\n"
]
}
],
"outputs": [],
"source": [
"def MulMat(A, B):\n",
" '''Función que devuelve la multiplicación de dos matrices A y B sin usar arreglos numPy\n",
......@@ -993,34 +819,29 @@
" Returns:\n",
" list: El resultado de la multiplicación entre la matriz A y la matriz B\n",
" \n",
" Ejemplos:\n",
" Ejemplo:\n",
" >>> A = [[125, 216, 419], [383, -516, 237], [-209, 855, 601], [403, 237, -506]]\n",
" >>> B = [[-73, 36], [21, -28], [49,67]]\n",
" >>> MulMat(A,B)\n",
" [[15942, 26525], [-27182, 44115], [62661, 8803], [-49236, -26030]]\n",
" \n",
" >>> MulMat(B,A)\n",
" La multiplicacion no es posible, Col(A)= 2 es diferente a Row(B)= 4\n",
" La multiplicacion no es posible, Col(A) 2 != Row(B) 4\n",
" '''\n",
" n = len(A) #Col(A)\n",
" m = len(A[0]) #Row(A)\n",
" q = len(B) #Col(B)\n",
" p = len(B[0]) #Row(B)\n",
" if m == q: #Valida si la multiplicacion es posible\n",
" m = len(A) #Row(A)\n",
" n = len(A[0]) #Col(A)\n",
" q = len(B) #Row(B)\n",
" p = len(B[0]) #Col(B)\n",
" if n == q: #Valida si la multiplicacion es posible\n",
" Bt = list(zip(*B)); R = []\n",
" for i in range(n):\n",
" for i in range(m):\n",
" C=[]\n",
" for j in range(p):\n",
" C.append(ProdPunto(A[i], Bt[j])) #Calcula las filas de la matriz Resultante\n",
" else:\n",
" R.append(C) #Agrega cada fila a la matriz resultante\n",
" return R\n",
" else:\n",
" print('La multiplicacion no es posible, Col(A)=', m, 'es diferente a Row(B)=', q)\n",
"\n",
"A=[[1,2,3,4,5],[6,7,8,9,0],[-1,-2,-3,-4,-5]]\n",
"B=[[5,4,3],[0,9,8],[-5,-4,-3],[2,1,0],[6,-2,-1]]\n",
"for col in MulMat(A, B): print(col)\n",
" print('La multiplicacion no es posible, Col(A)', n, '!= Row(B)', q)\n",
"\n",
"#import doctest\n",
"#doctest.testmod(verbose=True)"
......@@ -1035,39 +856,9 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ProdPunto(a,b)= 166.39999999999998\n",
"np.dot(a,b)= 166.39999999999998\n",
"\n",
"ProdPunto(a,b)= -800\n",
"np.dot(a,b)= -800\n",
"\n",
"MulMat(A,B)=\n",
"[58, 64]\n",
"[139, 154]\n",
"np.matmul(A,B)=\n",
" [[ 58 64]\n",
" [139 154]]\n",
"\n",
"MulMat(E,D)=\n",
"[15942, 26525]\n",
"[-2018.8999999999996, 31705.8]\n",
"[62661, 8803]\n",
"[-26921.4, 4481.799999999999]\n",
"np.matmul(E,D)=\n",
" [[ 15942. 26525. ]\n",
" [ -2018.9 31705.8]\n",
" [ 62661. 8803. ]\n",
" [-26921.4 4481.8]]\n"
]
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
......@@ -1119,67 +910,13 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inversa(C)=\n",
" [[-2. -2.07692308 -0.38461538]\n",
" [ 1. 1.07692308 0.38461538]\n",
" [-0. -0.23076923 -0.15384615]]\n",
"\n",
"np.linalg.inv(C)=\n",
" [[-2. -2.07692308 -0.38461538]\n",
" [ 1. 1.07692308 0.38461538]\n",
" [-0. -0.23076923 -0.15384615]]\n",
"Trying:\n",
" A = np.array([[2, 4, 3], [0, 1, -1], [3, 5, 7]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" Inversa(A)\n",
"Expecting:\n",
" array([[ 4. , -4.33333333, -2.33333333],\n",
" [-1. , 1.66666667, 0.66666667],\n",
" [-1. , 0.66666667, 0.66666667]])\n",
"ok\n",
"Trying:\n",
" C = np.array([[1, 3, 5], [-2, -4, -5], [3, 6, 1]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" Inversa(C)\n",
"Expecting:\n",
" array([[-2. , -2.07692308, -0.38461538],\n",
" [ 1. , 1.07692308, 0.38461538],\n",
" [-0. , -0.23076923, -0.15384615]])\n",
"ok\n",
"1 items had no tests:\n",
" __main__\n",
"1 items passed all tests:\n",
" 4 tests in __main__.Inversa\n",
"4 tests in 2 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 4,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"import sys\n",
"verbosity = True #Activar para mostrar en pantalla la solucion detallada\n",
"\n",
"def Inversa(A):\n",
" '''Función que calcula la inversa (si existe) de una matriz de n x n mediante el metodo de eliminación\n",
......@@ -1187,10 +924,10 @@
" \n",
" Args: \n",
" A (np.array): Lista de Arreglos que representa la matriz con valores tipo float.\n",
" Se dice que la matriz inversa A-1 tiene inversa, si A-1*A = I, donde I es la matriz identidad \n",
" Se dice que la matriz A tiene inversa(A-1), si A*A-1 = I = A-1*A, donde I es la matriz identidad \n",
" \n",
" Returns:\n",
" np.array: La matriz inversa de A\n",
" np.array, dtype=float64: La matriz inversa de A\n",
" \n",
" Ejemplos:\n",
" >>> A = np.array([[2, 4, 3], [0, 1, -1], [3, 5, 7]])\n",
......@@ -1204,128 +941,104 @@
" array([[-2. , -2.07692308, -0.38461538],\n",
" [ 1. , 1.07692308, 0.38461538],\n",
" [-0. , -0.23076923, -0.15384615]])\n",
" '''\n",
" if len(A) != len(A[0]):\n",
" ''' \n",
" A = np.array(A, dtype=np.float64)\n",
" if A.ndim != 2 and A.shape[0] != A.shape[1]:\n",
" print('La matriz no es cuadrada, por lo tanto NO INVERTIBLE estrictamente')\n",
" else:\n",
" A = np.array(A, dtype=np.float64)\n",
" n = len(A)\n",
" Inver = np.eye(n, dtype=np.float64)\n",
" #Método L por eliminación de Gauss, p es el pivote\n",
" for p in range(n-1):\n",
" if A[p,p] == 0: #Verifica que el pivote no sea cero\n",
" sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n",
" A[p,:], Inver[p,:] = A[p,:] / A[p,p], Inver[p,:] / A[p,p]\n",
" for k in range(p+1,n):\n",
" A[k,:], Inver[k,:] = A[p,:] * A[k,p] - A[k,:], Inver[p,:] * A[k,p] - Inver[k,:]\n",
" #Método U por eliminación de Gauss\n",
" for p in reversed(range(1,n)):\n",
" if A[p,p] == 0: #Verifica que el pivote no sea cero\n",
" n = len(A) #Tamaño de la matriz\n",
" Inver = np.eye(n, dtype=np.float64) #Inicializo la matriz Inversa\n",
" if verbosity: print('Matriz = \\n', A, '\\n')\n",
" \n",
" #Eliminación de Gauss-Jordan hacia abajo\n",
" piv = 0\n",
" while piv < n-1:\n",
" intercambiaRenglon(A, Inver, piv) #Intercambia renglon usando pivoteo parcial\n",
" eliminaColumna(A, Inver, piv)\n",
" piv+=1\n",
" if A[piv,piv] == 0: #Verifica que el ultimo pivote no sea cero\n",
" sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n",
" A[p,:], Inver[p,:] = A[p,:] / A[p,p], Inver[p,:] / A[p,p]\n",
" for k in reversed(range(p)):\n",
" A[k,:], Inver[k,:] = A[p,:] * A[k,p] - A[k,:], Inver[p,:] * A[k,p] - Inver[k,:]\n",
" return Inver\n",
" A[piv], Inver[piv] = A[piv] / A[piv,piv], Inver[piv] / A[piv,piv]\n",
" \n",
" #Eliminación de Gauss-Jordan hacia arriba\n",
" if verbosity: print('Ahora hacia arriba: ')\n",
" for piv in reversed(range(1,n)):\n",
" eliminaColumna(A, Inver, piv, reverse=True)\n",
" A[piv-1], Inver[piv-1] = A[piv-1] / A[piv-1,piv-1], Inver[piv-1] / A[piv-1,piv-1]\n",
" if verbosity: print('Normalizo ultimo Pivote=',piv, '\\n', A, '\\n', '\\nInversa=\\n', Inver)\n",
" \n",
" return Inver\n",
"\n",
"C = np.array([[1, 3, 5],\n",
" [-2, -4, -5],\n",
" [3, 6, 1]], dtype=float)\n",
"def eliminaColumna(A, Inver, p, reverse=False):\n",
" direction = range(p+1,len(A))\n",
" if reverse == True:\n",
" direction = reversed(range(p))\n",
" A[p], Inver[p] = A[p] / A[p,p], Inver[p] / A[p,p]\n",
" for k in direction:\n",
" A[k], Inver[k] = A[p] * A[k,p] - A[k], Inver[p] * A[k,p] - Inver[k]\n",
" if verbosity: print('Elimino Columna=',p+1, '\\n', A, '\\n')\n",
" return A, Inver, p\n",
" \n",
" \n",
"def intercambiaRenglon(A, Inver, p):\n",
" filas = dict( enumerate(A[p+1:,p]) )\n",
" row = max(filas.items(), key=lambda x: abs(x[1])) # Encuentra la fila con valor abs maximo\n",
" if A[p,p] == 0 and all(value == 0 for value in filas.values()):\n",
" sys.exit('La matriz es singular, por lo tanto NO INVERTIBLE')\n",
" if abs(A[p,p]) < abs(row[1]):\n",
" A[[p, row[0]+p+1]], Inver[[p, row[0]+p+1]] = A[[row[0]+p+1, p]], Inver[[row[0]+p+1, p]]\n",
" if verbosity: print('Intercambio renglon',p+1 ,'por renglon', row[0]+p+2,'\\n', A, '\\n')\n",
" return A, Inver, p\n",
"\n",
"print('Inversa(C)=\\n',Inversa(C))\n",
"print('\\nnp.linalg.inv(C)=\\n', np.linalg.inv(C))\n",
"import doctest\n",
"doctest.testmod(verbose=True)"
"#import doctest\n",
"#doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### 5.2.5 Utilizando la funcion anterior escribe otra que obtenga la pseduo-inversa de una matriz."
"import numpy as np\n",
"H = np.array([[0, 2, -3, 4, 5],\n",
" [0, 0, 2, -1, 2],\n",
" [1, 1, 0, 2, -1],\n",
" [2, 0, -1, 1, 1],\n",
" [0, 0, 0, 1, 8]])\n",
"\n",
"Leontief = np.array([[1.234, 0.014, 0.007, 0.064, 0.006, 0.017],\n",
" [0.017, 1.436, 0.056, 0.014, 0.019, 0.032],\n",
" [0.078, 0.467, 1.878, 0.036, 0.044, 0.031],\n",
" [0.752, 0.133, 0.101, 1.741, 0.065, 0.123],\n",
" [0.061, 0.045, 0.130, 0.083, 1.578, 0.059],\n",
" [0.340, 0.236, 0.307, 0.315, 0.376, 1.349]])\n",
"\n",
"\n",
"e = np.transpose([[99640, 75548, 14444, 33501, 23527, 263985]])\n",
"\n",
"print('Mi Inversa=\\n', Inversa(H))\n",
"print('\\nnp.linalg.inv=\\n', np.linalg.inv(H))"
]
},
{
"cell_type": "code",
"execution_count": 117,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trying:\n",
" A = np.array([[2, 4, 3], [0, 1, -1], [3, 5, 7]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" Inversa(A)\n",
"Expecting:\n",
" array([[ 4. , -4.33333333, -2.33333333],\n",
" [-1. , 1.66666667, 0.66666667],\n",
" [-1. , 0.66666667, 0.66666667]])\n",
"ok\n",
"Trying:\n",
" C = np.array([[1, 3, 5], [-2, -4, -5], [3, 6, 1]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" Inversa(C)\n",
"Expecting:\n",
" array([[-2. , -2.07692308, -0.38461538],\n",
" [ 1. , 1.07692308, 0.38461538],\n",
" [-0. , -0.23076923, -0.15384615]])\n",
"ok\n",
"Trying:\n",
" B = np.array([[5, -1, 5], [11, -7, 5], [10, 2, -18]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" PseudoInv(B)\n",
"Expecting:\n",
" array([[ 0.14646465, -0.01010101, 0.03787879],\n",
" [ 0.31313131, -0.17676768, 0.03787879],\n",
" [ 0.11616162, -0.02525253, -0.03030303]])\n",
"ok\n",
"Trying:\n",
" D = np.array([[2, 4, 6], [4, 5, 6], [3, 1, -2]])\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" Inversa(D)\n",
"Expecting:\n",
" array([[-2.66666667, 2.33333333, -1. ],\n",
" [ 4.33333333, -3.66666667, 2. ],\n",
" [-1.83333333, 1.66666667, -1. ]])\n",
"ok\n",
"3 items had no tests:\n",
" __main__\n",
" __main__.adjoint\n",
" __main__.f\n",
"2 items passed all tests:\n",
" 4 tests in __main__.Inversa\n",
" 4 tests in __main__.PseudoInv\n",
"8 tests in 5 items.\n",
"8 passed and 0 failed.\n",
"Test passed.\n"
"source": [
"### 5.2.5 Utilizando la funcion anterior escribe otra que obtenga la pseduo-inversa de una matriz."
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=8)"
]
},
"execution_count": 117,
"cell_type": "code",
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"def PseudoInv(A):\n",
" '''Función que calcula la pseudo inversa (Moore–Penrose inverse) de una matriz \"A\". Se define como la inversa \n",
" del producto matricial de \"A\" con su matriz transpuesta \"AT, multiplicada por \"AT\". La multiplicación de la\n",
" pseudo inversa por \"A\" resulta en la matriz Identidad\n",
" '''Función que calcula la pseudo inversa por la derecha de una matriz \"A\" que no es cuadrada. \n",
" La pseudo inversa se define como la inversa del producto matricial de \"A\" con su matriz transpuesta \"AT\", \n",
" multiplicada por \"AT\". El producto de la pseudo inversa \"A+\" con \"A\", resulta en la matriz Identidad.\n",
" \n",
" A+ = AT· (A · AT) -1\n",
" \n",
......@@ -1335,35 +1048,26 @@
" np.array: La pseudo inversa de la matriz \"A\"\n",
" \n",
" Ejemplos:\n",
" >>> B = np.array([[5, -1, 5], [11, -7, 5], [10, 2, -18]])\n",
" >>> B = np.array([[1, 1, 1, 1], [5, 7, 7, 9], [0, -9, 3, -0.5]])\n",
" >>> PseudoInv(B)\n",
" array([[ 0.14646465, -0.01010101, 0.03787879],\n",
" [ 0.31313131, -0.17676768, 0.03787879],\n",
" [ 0.11616162, -0.02525253, -0.03030303]])\n",
" \n",
" >>> D = np.array([[2, 4, 6], [4, 5, 6], [3, 1, -2]])\n",
" >>> Inversa(D)\n",
" array([[-2.66666667, 2.33333333, -1. ],\n",
" [ 4.33333333, -3.66666667, 2. ],\n",
" [-1.83333333, 1.66666667, -1. ]])\n",
" array([[ 2.01296151, -0.24783975, 0.01728201],\n",
" [ 0.18047918, -0.0115868 , -0.09269442],\n",
" [ 0.2935978 , 0.0072663 , 0.0581304 ],\n",
" [-1.48703849, 0.25216025, 0.01728201]])\n",
" >>> np.linalg.pinv(B)\n",
" array([[ 2.01296151, -0.24783975, 0.01728201],\n",
" [ 0.18047918, -0.0115868 , -0.09269442],\n",
" [ 0.2935978 , 0.0072663 , 0.0581304 ],\n",
" [-1.48703849, 0.25216025, 0.01728201]])\n",
" '''\n",
" AT = list(zip(*A)) #Transpuesta de A\n",
" Tinv = Inversa(A.dot(AT))\n",
" PsInv = AT@Tinv\n",
" return PsInv\n",
"\n",
"A = np.array([[1, 1, 1, 1],\n",
" [5, 7, 7, 9]])\n",
"\n",
"B = np.array([[5, -1, 5],\n",
" [11, -7, 5],\n",
" [10, 2, -18]])\n",
" Transp = list(zip(*A)) #Transpuesta de A\n",
" TranspInver = Inversa( MulMat(A,Transp) )\n",
" PseudoInv = MulMat(Transp, TranspInver)\n",
" \n",
" return np.array(PseudoInv, dtype=np.float64)\n",
"\n",
"D = np.array([[2, 4, 6],\n",
" [4, 5, 6],\n",
" [3, 1, -2]]) \n",
"import doctest\n",
"doctest.testmod(verbose=True)"
"#import doctest\n",
"#doctest.testmod(verbose=True)"
]
},
{
......@@ -1384,45 +1088,31 @@
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6, -3, 21])"
]
},
"execution_count": 1,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"a = np.array([2, -1, 7])\n",
"3*a\n"
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0.317227\n",
"b -0.901452\n",
"c 1.720724\n",
"d 1.518539\n",
"e 1.590072\n",
"a 1.085164\n",
"b 0.684054\n",
"c -3.137046\n",
"d -0.780916\n",
"e 0.497413\n",
"dtype: float64"
]
},
"execution_count": 4,
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1434,21 +1124,21 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 1.131946\n",
"1 -1.331670\n",
"2 0.055184\n",
"3 -0.389788\n",
"4 0.143579\n",
"0 -0.165303\n",
"1 0.089697\n",
"2 -1.680879\n",
"3 -0.160729\n",
"4 0.235082\n",
"dtype: float64"
]
},
"execution_count": 5,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1459,7 +1149,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 38,
"metadata": {},
"outputs": [
{
......@@ -1471,7 +1161,7 @@
"dtype: int64"
]
},
"execution_count": 6,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1483,7 +1173,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 39,
"metadata": {},
"outputs": [
{
......@@ -1496,7 +1186,7 @@
"dtype: float64"
]
},
"execution_count": 7,
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1515,40 +1205,39 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.720723972548356"
"1.0851635954341003"
]
},
"execution_count": 10,
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[0]\n",
"s['c']"
"s[0]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0.317227\n",
"b -0.901452\n",
"c 1.720724\n",
"a 1.085164\n",
"b 0.684054\n",
"c -3.137046\n",
"dtype: float64"
]
},
"execution_count": 11,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1559,46 +1248,44 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a False\n",
"b False\n",
"c True\n",
"d True\n",
"e True\n",
"dtype: bool"
"a 1.085164\n",
"b 0.684054\n",
"e 0.497413\n",
"dtype: float64"
]
},
"execution_count": 16,
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[s>s.mean()"
"s[s>s.mean()]"
]
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 1.988544\n",
"b 1.061037\n",
"c 2.324904\n",
"d -1.962872\n",
"e -2.567597\n",
"a 2.170327\n",
"b 1.368108\n",
"c -6.274092\n",
"d -1.561832\n",
"e 0.994826\n",
"dtype: float64"
]
},
"execution_count": 56,
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1609,21 +1296,21 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a False\n",
"b False\n",
"c True\n",
"a True\n",
"b True\n",
"c False\n",
"d False\n",
"e True\n",
"e False\n",
"dtype: bool"
]
},
"execution_count": 15,
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1634,16 +1321,16 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3172265975694433"
"1.0851635954341003"
]
},
"execution_count": 17,
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1661,17 +1348,17 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"a = np.array(range(10,20))\n",
"a = np.array(range(10))\n",
"s = pd.Series(a)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 47,
"metadata": {},
"outputs": [
{
......@@ -1680,7 +1367,7 @@
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 60,
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1691,7 +1378,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 48,
"metadata": {},
"outputs": [
{
......@@ -1710,7 +1397,7 @@
"dtype: int64"
]
},
"execution_count": 62,
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1721,7 +1408,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 49,
"metadata": {},
"outputs": [
{
......@@ -1730,7 +1417,7 @@
"array([ 4, 6, 8, 10, 12, 14])"
]
},
"execution_count": 75,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
......@@ -1741,7 +1428,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 50,
"metadata": {},
"outputs": [
{
......@@ -1751,30 +1438,1167 @@
"1 NaN\n",
"2 NaN\n",
"3 NaN\n",
"4 28.0\n",
"5 30.0\n",
"6 32.0\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": 27,
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(s[:7]+s[4:])"
"(s[:6]+s[4:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.3.2 DataFrame\n",
"DataFrame es un estructura bidimensional 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": null,
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),\n",
" 'two': pd.Series([1., 2., 3., 4.], index=['a', 'x', 'c', 'd'])}"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(d)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"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>NaN</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",
" <tr>\n",
" <th>x</th>\n",
" <td>NaN</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one two\n",
"a 1.0 1.0\n",
"b 2.0 NaN\n",
"c 3.0 3.0\n",
"d NaN 4.0\n",
"x NaN 2.0"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Seleccionar, Añadir y Borrar Columnas"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 1.0\n",
"b 2.0\n",
"c 3.0\n",
"d NaN\n",
"x NaN\n",
"Name: one, dtype: float64"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"one\"]"
]
},
{
"cell_type": "code",
"execution_count": 56,
"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>NaN</td>\n",
" <td>NaN</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",
" <tr>\n",
" <th>x</th>\n",
" <td>NaN</td>\n",
" <td>2.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 NaN NaN False\n",
"c 3.0 3.0 9.0 True\n",
"d NaN 4.0 NaN False\n",
"x NaN 2.0 NaN False"
]
},
"execution_count": 56,
"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": 57,
"metadata": {},
"outputs": [],
"source": [
"del df['two']"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [],
"source": []
"source": [
"three = df.pop('three')"
]
},
{
"cell_type": "code",
"execution_count": 59,
"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",
" <tr>\n",
" <th>x</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\n",
"x NaN False"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 60,
"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>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>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>c</th>\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>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>x</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one two flag\n",
"a 1.0 1.0 False\n",
"b 2.0 NaN False\n",
"c 3.0 9.0 True\n",
"d NaN NaN False\n",
"x NaN NaN False"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# inserta la columna three en la posición 1 bajo el nombre two\n",
"df.insert(1, \"two\", 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": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"one 1\n",
"two 1\n",
"flag False\n",
"Name: a, dtype: object"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['a']"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"one 3\n",
"two 9\n",
"flag True\n",
"Name: c, dtype: object"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.iloc[2]"
]
},
{
"cell_type": "code",
"execution_count": 63,
"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>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>NaN</td>\n",
" <td>False</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" one two flag\n",
"a 1.0 1.0 False\n",
"b 2.0 NaN False"
]
},
"execution_count": 63,
"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": 176,
"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": 176,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"iris = pd.read_csv('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": 177,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"150"
]
},
"execution_count": 177,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.shape[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.2 ¿Cual es el numero de columnas en el conjunto de datos?"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.shape[1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.3 Imprime el nombre de todas las columnas"
]
},
{
"cell_type": "code",
"execution_count": 179,
"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",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"Empty DataFrame\n",
"Columns: [sepal_length, sepal_width, petal_length, petal_width, species]\n",
"Index: []"
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[:0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.4 ¿Cual es el nombre de la columna 4?"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'species'"
]
},
"execution_count": 180,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.columns[4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.5 Selecciona las columnas, \"sepal_length\" y \"petal_length\""
]
},
{
"cell_type": "code",
"execution_count": 181,
"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",
" </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"
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris[['sepal_length', 'petal_length']].head()"
]
},
{
"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": 182,
"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>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",
" <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",
" </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",
" </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",
"4 5.0 3.6 1.4 0.2 setosa\n",
"5 5.4 3.9 1.7 0.4 setosa\n",
"7 5.0 3.4 1.5 0.2 setosa"
]
},
"execution_count": 182,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris.loc[iris.sepal_length > 4.8].head()"
]
},
{
"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": 183,
"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>p_l*p_w</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",
" </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",
" </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",
" </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",
" </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",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width species p_l*p_w\n",
"0 5.1 3.5 1.4 0.2 setosa 0.28\n",
"1 4.9 3.0 1.4 0.2 setosa 0.28\n",
"2 4.7 3.2 1.3 0.2 setosa 0.26\n",
"3 4.6 3.1 1.5 0.2 setosa 0.30\n",
"4 5.0 3.6 1.4 0.2 setosa 0.28"
]
},
"execution_count": 183,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"iris['p_l*p_w'] = iris.petal_length * iris.petal_width\n",
"iris.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4.8 Encuentra el promedio por especie de cada columna."
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\species: setosa\n",
" sepal_length 5.0060\n",
"sepal_width 3.4180\n",
"petal_length 1.4640\n",
"petal_width 0.2440\n",
"p_l*p_w 0.3628\n",
"dtype: float64\n",
"\n",
"species: virginica\n",
" sepal_length 6.5880\n",
"sepal_width 2.9740\n",
"petal_length 5.5520\n",
"petal_width 2.0260\n",
"p_l*p_w 11.2962\n",
"dtype: float64\n",
"\n",
"species: versicolor\n",
" sepal_length 5.9360\n",
"sepal_width 2.7700\n",
"petal_length 4.2600\n",
"petal_width 1.3260\n",
"p_l*p_w 5.7204\n",
"dtype: float64\n"
]
}
],
"source": [
"print('\\species: setosa\\n', iris.loc[iris.species == 'setosa'].mean())\n",
"print('\\nspecies: virginica\\n', iris.loc[iris.species == 'virginica'].mean())\n",
"print('\\nspecies: versicolor\\n',iris.loc[iris.species == 'versicolor'].mean())"
]
}
],
"metadata": {
......
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