Ejercicios usando Pandas

parent 210cff18
...@@ -4,22 +4,1771 @@ ...@@ -4,22 +4,1771 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 2.SciPy\n", "![scipy](https://cdn-images-1.medium.com/max/1600/1*Y2v3PrF1rUQRUHwOcXJznA.png)\n",
"## 2.1 NumPy" "\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 tumpla 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", "cell_type": "code",
"execution_count": null, "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",
"8\n",
"<memory at 0x7f7b018c0480>\n",
"[ 5 0 -5]\n",
"[ 2 -1 -2]\n",
"[ 0 -2 1]\n",
"1\n"
]
},
{
"data": {
"text/plain": [
"-5"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"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)\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"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1.-1.j])"
]
},
"execution_count": 10,
"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"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0, 0, 0],\n",
" [0, 0, 0, 0],\n",
" [0, 0, 0, 0]], dtype=int16)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a= np.zeros( (3,4), dtype=np.int16)\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones( (3,4)) "
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1.],\n",
" [1., 1., 1., 1.],\n",
" [1., 1., 1., 1.]])"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.empty( (3,4) ) \n"
]
},
{
"cell_type": "code",
"execution_count": 55,
"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)"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.eye(5, dtype=np.int8)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operaciones Basicas"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3]\n"
]
}
],
"source": [
"a = np.array([20,30,40,50] )\n",
"b = np.arange( 4 )\n",
"print(b)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([20, 29, 38, 47])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Suma\n",
"a-b"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 40, 60, 80, 100])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Multiplicacion Por escalar\n",
"a*2"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 4, 9])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Potencia\n",
"b**2"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ True, True, True, False])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Operadores Boleanos\n",
"a<50"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 30, 80, 150])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Multiplicacion por elemento\n",
"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,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"260"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a@b == a.dot(b)\n",
"a@b"
]
},
{
"cell_type": "code",
"execution_count": 277,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "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)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"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"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c= np.arange(9).reshape(3,3)\n",
"print(np.arange(9))\n",
"print(c)\n",
"c.sum()"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 9, 12, 15])"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=0) # Suma por Columna"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 3, 12, 21])"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=1) #Suma por Fila"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Elementos, filas, columnas y subarreglos."
]
},
{
"cell_type": "code",
"execution_count": 32,
"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)"
]
},
"execution_count": 32,
"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)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[1][2] == b[1,2]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 11, 12, 13, 14], dtype=int8)"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[1,:]"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 12, 22, 32, 42, 52], dtype=int8)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[:,2]"
]
},
{
"cell_type": "code",
"execution_count": 36,
"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)"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[:,3:5]"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[20, 21, 22],\n",
" [30, 31, 32],\n",
" [40, 41, 42],\n",
" [50, 51, 52]], dtype=int8)"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[2:,:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Iterando elementos"
]
},
{
"cell_type": "code",
"execution_count": 177,
"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"
]
}
],
"source": [
"for row in b:\n",
" print(row)\n",
" print('a')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"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"
]
}
],
"source": [
"for element in b.flat:\n",
" print(element)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cambio de forma"
]
},
{
"cell_type": "code",
"execution_count": 239,
"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.]])"
]
},
"execution_count": 239,
"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)"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"execution_count": 220,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.shape"
]
},
{
"cell_type": "code",
"execution_count": 245,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4., 4., 0., 8.],\n",
" [0., 1., 9., 7.],\n",
" [3., 6., 4., 3.]])"
]
},
"execution_count": 245,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.reshape(3,4)"
]
},
{
"cell_type": "code",
"execution_count": 244,
"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.]])"
]
},
"execution_count": 244,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(a)\n",
"a.T"
]
},
{
"cell_type": "code",
"execution_count": 243,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True, True],\n",
" [ True, True, True, True],\n",
" [ True, True, True, True]])"
]
},
"execution_count": 243,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.transpose()==a.T"
]
},
{
"cell_type": "code",
"execution_count": 242,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4, 3)\n"
]
},
{
"data": {
"text/plain": [
"(3, 4)"
]
},
"execution_count": 242,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(a.shape)\n",
"a.T.shape"
]
},
{
"cell_type": "code",
"execution_count": 266,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[4., 4.],\n",
" [0., 8.],\n",
" [0., 1.]],\n",
"\n",
" [[9., 7.],\n",
" [3., 6.],\n",
" [4., 3.]]])"
]
},
"execution_count": 266,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# La dimencion con -1 se calcula automaticamente\n",
"a.reshape(-1,3,2) "
]
},
{
"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": 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,
"metadata": {},
"output_type": "execute_result"
}
],
"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",
" \n",
" Args: \n",
" A, B (list): Listas que representan los vectores A, B con valores tipo float o int.\n",
" Deben tener la misma longitud, de lo contrario genera un mensaje de error.\n",
" \n",
" Returns:\n",
" float, int: El resultado del producto punto entre A y B\n",
" \n",
" Ejemplos:\n",
" >>> A = [5, -10, 15] \n",
" >>> B = [30, 35, -40]\n",
" >>> ProdPunto(A, B)\n",
" -800\n",
" \n",
" >>> D = [2, 5.6, 9, 8, 10] \n",
" >>> E = [1, 3, 2.4, 2, 11]\n",
" >>> ProdPunto(D, E)\n",
" 166.39999999999998\n",
" '''\n",
" if len(A) == len(B):\n",
" return sum( map((lambda x, y: x*y), A, B) )\n",
" else:\n",
" print(\"Error, los vectores son de diferente longitud\", len(A), '!=', len(B))\n",
"\n",
"import doctest\n",
"doctest.testmod(verbose=True)"
]
},
{
"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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[28, 4, 5]\n",
"[8, 64, 50]\n",
"[-28, -4, -5]\n"
]
}
],
"source": [
"def MulMat(A, B):\n",
" '''Función que devuelve la multiplicación de dos matrices A y B sin usar arreglos numPy\n",
" \n",
" Args: \n",
" A, B (list): Lista de Arreglos que representan las matrices A, B con valores tipo float o int.\n",
" Columnas de A debe ser igual a Filas de B, de lo contrario genera un mensaje de error. \n",
" Tomar en cuenta que no siempre se cumple la propiedad conmutativa: MulMat(A, B) =! MulMat(B, A)\n",
" \n",
" Returns:\n",
" list: El resultado de la multiplicación entre la matriz A y la matriz B\n",
" \n",
" Ejemplos:\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",
" '''\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",
" Bt = list(zip(*B)); R = []\n",
" for i in range(n):\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",
"\n",
"#import doctest\n",
"#doctest.testmod(verbose=True)"
]
},
{
"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": 36,
"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"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"a = [2, 5.6, 9, 8, 10]\n",
"b = [1, 3, 2.4, 2, 11]\n",
"c = [5, -10, 15]\n",
"d = [30, 35, -40]\n",
"\n",
"if ProdPunto(a,b) == np.dot(a,b):\n",
" print('ProdPunto(a,b)=', ProdPunto(a,b))\n",
" print('np.dot(a,b)=', np.dot(a,b))\n",
"if ProdPunto(c,d) == np.dot(c,d):\n",
" print('\\nProdPunto(a,b)=', ProdPunto(c,d))\n",
" print('np.dot(a,b)=', np.dot(c,d))\n",
"\n",
"A = [[1,2,3],\n",
" [4,5,6]]\n",
"\n",
"B = [[7,8],\n",
" [9,10],\n",
" [11,12]]\n",
"\n",
"E=[[125, 216, 419],\n",
" [38.3, -516, 237],\n",
" [-209, 855, 601],\n",
" [403, 237, -50.6]]\n",
"\n",
"D=[[-73, 36],\n",
" [21, -28],\n",
" [49,67]]\n",
"\n",
"if (MulMat(A,B) == np.matmul(A,B)).all():\n",
" print('\\nMulMat(A,B)=')\n",
" for col in MulMat(A, B): print(col)\n",
" print('np.matmul(A,B)=\\n', np.matmul(A,B))\n",
"if (MulMat(E,D) == np.matmul(E,D)).all():\n",
" print('\\nMulMat(E,D)=')\n",
" for col in MulMat(E, D): print(col)\n",
" print('np.matmul(E,D)=\\n', np.matmul(E,D))\n"
]
},
{
"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": "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,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import sys\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",
" de Gauss-Jordan.\n",
" \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",
" \n",
" Returns:\n",
" np.array: La matriz inversa de A\n",
" \n",
" Ejemplos:\n",
" >>> A = np.array([[2, 4, 3], [0, 1, -1], [3, 5, 7]])\n",
" >>> Inversa(A)\n",
" array([[ 4. , -4.33333333, -2.33333333],\n",
" [-1. , 1.66666667, 0.66666667],\n",
" [-1. , 0.66666667, 0.66666667]])\n",
" \n",
" >>> C = np.array([[1, 3, 5], [-2, -4, -5], [3, 6, 1]])\n",
" >>> Inversa(C)\n",
" 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",
" 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(3, dtype=np.float64)\n",
" #Factorizacion L por eliminación de Gauss\n",
" for p in range(n-1):\n",
" if A[p,p] == 0: #Verifica que el pivote no sea cero\n",
" print ()\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",
" #factorizacion 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",
" print ()\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",
"\n",
"C = np.array([[1, 3, 5],\n",
" [-2, -4, -5],\n",
" [3, 6, 1]], dtype=float)\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)"
]
},
{
"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": 117,
"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"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=8)"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"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",
" \n",
" A+ = AT· (A · AT) -1\n",
" \n",
" Args: \n",
" A (np.array): Lista de Arreglos que representa la matriz \"A\" con valores tipo float.\n",
" Returns:\n",
" 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",
" >>> 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",
" '''\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",
"\n",
"D = np.array([[2, 4, 6],\n",
" [4, 5, 6],\n",
" [3, 1, -2]]) \n",
"import doctest\n",
"doctest.testmod(verbose=True)"
]
},
{
"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": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6, -3, 21])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"a = np.array([2, -1, 7])\n",
"3*a\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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",
"dtype: float64"
]
},
"execution_count": 4,
"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": 5,
"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",
"dtype: float64"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.Series(np.random.randn(5))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0\n",
"b 1\n",
"c 2\n",
"dtype: int64"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'b': 1, 'a': 0, 'c': 2}\n",
"pd.Series(d)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"b 1.0\n",
"c 2.0\n",
"d NaN\n",
"a 0.0\n",
"dtype: float64"
]
},
"execution_count": 7,
"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", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## 2.2 Pandas" "### Las Series son compatibles con *numpy.array* y *dict*"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.720723972548356"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[0]\n",
"s['c']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0.317227\n",
"b -0.901452\n",
"c 1.720724\n",
"dtype: float64"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[:3]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a False\n",
"b False\n",
"c True\n",
"d True\n",
"e True\n",
"dtype: bool"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[s>s.mean()"
]
},
{
"cell_type": "code",
"execution_count": 56,
"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",
"dtype: float64"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s*2"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a False\n",
"b False\n",
"c True\n",
"d False\n",
"e True\n",
"dtype: bool"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s>s.median()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3172265975694433"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[\"a\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Alieneacion Automatica"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"a = np.array(range(10,20))\n",
"s = pd.Series(a)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 62,
"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": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 4, 6, 8, 10, 12, 14])"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(a[:6]+a[4:])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 NaN\n",
"1 NaN\n",
"2 NaN\n",
"3 NaN\n",
"4 28.0\n",
"5 30.0\n",
"6 32.0\n",
"7 NaN\n",
"8 NaN\n",
"9 NaN\n",
"dtype: float64"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(s[:7]+s[4:])"
] ]
}, },
{ {
...@@ -46,7 +1795,7 @@ ...@@ -46,7 +1795,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.8rc1" "version": "3.5.3"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
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