"[**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",
"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",