Numpy

parent 3f637aed
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 159,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
" [10, 11, 12, 13, 14]], dtype=int16)" " [10, 11, 12, 13, 14]], dtype=int16)"
] ]
}, },
"execution_count": 6, "execution_count": 159,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -868,47 +868,83 @@ ...@@ -868,47 +868,83 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 75, "execution_count": 20,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cuantos elementos tienen tus vectores: \n",
"2\n",
"Ingresa los elementos uno a uno del vector 1 \n",
"\n",
"Ingresa el elemento: 1\n",
"10\n",
"Ingresa el elemento: 2\n",
"20\n",
"\n",
"Ingresa los elementos uno a uno del vector 2 \n",
"\n",
"Ingresa el elemento: 1\n",
"2\n",
"Ingresa el elemento: 2\n",
"4\n",
"\n",
"Vector 1: [10, 20]\n",
"Vector 2: [2, 4]\n",
"Suma Producto: 100\n"
]
}
],
"source": [ "source": [
"from functools import reduce\n",
"#Tarea, generar el producto punto\n", "#Tarea, generar el producto punto\n",
"def prod_punto(v1,v2):\n", "def prod_punto(v1,v2):\n",
" '''\n", " '''\n",
" Soy un docstring\n", " Soy un docstring\n",
" '''\n", " '''\n",
" pass\n", " v1,v2,v3=[],[],[]\n",
" #return prod_punto" " elementos=int(input(\"Cuantos elementos tienen tus vectores: \\n\"))\n",
" \n",
" print(\"Ingresa los elementos uno a uno del vector 1 \\n\")\n",
" for elemento in range(0,elementos):\n",
" elem=int(input(\"Ingresa el elemento: \"+str(elemento+1)+\"\\n\"))\n",
" v1.append(elem)\n",
" \n",
" print(\"\\nIngresa los elementos uno a uno del vector 2 \\n\")\n",
" for elemento in range(0,elementos):\n",
" elem=int(input(\"Ingresa el elemento: \"+str(elemento+1)+\"\\n\"))\n",
" v2.append(elem)\n",
" \n",
" v3=list(map(lambda x,y: y*x, v1,v2))\n",
" sumap=reduce((lambda x,y: x+y),v3)\n",
" \n",
" print(\"\\nVector 1: \"+str(v1)+\"\\n\"+\"Vector 2: \"+str(v2)+\"\\n\"+\"Suma Producto: \"+str(sumap))\n",
" return sumap\n",
" #return prod_punto\n",
"x=prod_punto(3,4)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 170, "execution_count": 162,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 2, 3], [4, 5, 6]]\n"
]
},
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"2" "100"
] ]
}, },
"execution_count": 170, "execution_count": 162,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"#Matriz sin numpy para la tarea\n", "#Comprobacion con Numpy\n",
"aaa=[[1,2,3],[4,5,6]]\n", "np.matmul([10,20], [2,4])"
"print(aaa)\n",
"aaa[0][1]"
] ]
}, },
{ {
...@@ -920,32 +956,232 @@ ...@@ -920,32 +956,232 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 165, "execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'def matmult(a,b):\\n zip_b = zip(*b)\\n #uncomment next line if python 3 : \\n zip_b = list(zip_b)\\n return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \\n for col_b in zip_b] for row_a in a]\\n'"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tamMatriz():\n",
" n=int(input(\"Ingrese las filas de la matriz: \\n\"))\n",
" m=int(input(\"Ingrese las columnas de la matriz: \\n\"))\n",
" return [n,m]\n",
"\n",
"def creaMatriz(nFila, nCol):\n",
" matriz = []\n",
" for i in range(nFila):\n",
" listaFila = []\n",
" for j in range(nCol):\n",
" val=int(input(\"Ingresa el valor del elemento\"+str(i)+\",\"+str(j)+\": \\n\"))\n",
" listaFila.append(val)\n",
" matriz.append(listaFila)\n",
"\n",
" return matriz\n",
"\n",
"def mmult1(m1, m2):\n",
" fila_A = len(m1)\n",
" cols_A = len(m1[0])\n",
" fila_B = len(m2)\n",
" cols_B = len(m2[0])\n",
"\n",
" if cols_A != fila_B:\n",
" print (\"Imposible resolver, la dimensionalidad de las matrices no cuadra.\")\n",
" return\n",
"\n",
" C = [[0 for fila in range(cols_B)] for col in range(fila_A)]\n",
" print (C)\n",
"\n",
" for i in range(fila_A):\n",
" for j in range(cols_B):\n",
" for k in range(cols_A):\n",
" C[i][j] += m1[i][k] * m2[k][j]\n",
" return C\n",
"\n",
"#Me encantaria que me podieran explicar esta ya que es extremadamente eficiente :O\n",
"\n",
"'''def matmult(a,b):\n",
" zip_b = zip(*b)\n",
" #uncomment next line if python 3 : \n",
" zip_b = list(zip_b)\n",
" return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \n",
" for col_b in zip_b] for row_a in a]\n",
"'''\n",
"\n",
"'''b=mat2\n",
"print(b)\n",
"zip_b = zip(*b)\n",
"for col_b in zip_b:\n",
" print(col_b)\n",
"'''\n",
"\n",
"### Muy PRO\n",
"m = np.array([[sum(row_a*col_b) for col_b in b.T] for row_a in a])\n",
"print(m, type(m))\n",
"###"
]
},
{
"cell_type": "code",
"execution_count": 212,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"A = [[1,2,3],\n", "a=np.array(mat1)\n",
" [4,5,6]]\n", "b=np.array(mat2)\n"
"B = [[7,8],[9,10],[11,12]]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 162, "execution_count": 118,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingrese las filas de la matriz: \n",
"2\n",
"Ingrese las columnas de la matriz: \n",
"3\n"
]
}
],
"source": [
"aa=tamMatriz()"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingrese las filas de la matriz: \n",
"3\n",
"Ingrese las columnas de la matriz: \n",
"2\n"
]
}
],
"source": [
"bb=tamMatriz()"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa el valor del elemento0,0: \n",
"1\n",
"Ingresa el valor del elemento0,1: \n",
"2\n",
"Ingresa el valor del elemento0,2: \n",
"3\n",
"Ingresa el valor del elemento1,0: \n",
"4\n",
"Ingresa el valor del elemento1,1: \n",
"5\n",
"Ingresa el valor del elemento1,2: \n",
"6\n"
]
}
],
"source": [
"mat1 = creaMatriz(aa[0],aa[1])"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa el valor del elemento0,0: \n",
"10\n",
"Ingresa el valor del elemento0,1: \n",
"20\n",
"Ingresa el valor del elemento1,0: \n",
"30\n",
"Ingresa el valor del elemento1,1: \n",
"40\n",
"Ingresa el valor del elemento2,0: \n",
"50\n",
"Ingresa el valor del elemento2,1: \n",
"60\n"
]
}
],
"source": [
"mat2=creaMatriz(bb[0],bb[1])"
]
},
{
"cell_type": "code",
"execution_count": 210,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[[1, 2, 3], [4, 5, 6]]\n", "[[0, 0], [0, 0]]\n"
"[[7, 8], [9, 10], [11, 12]]\n" ]
},
{
"data": {
"text/plain": [
"[[220, 280], [490, 640]]"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mmult1(mat1,mat2)"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[220, 280],\n",
" [490, 640]])"
] ]
},
"execution_count": 206,
"metadata": {},
"output_type": "execute_result"
} }
], ],
"source": [ "source": [
"print(A)\n", "#Comprobacion con Numpy\n",
"print(B)" "np.matmul(mat1, mat2)"
] ]
}, },
{ {
...@@ -957,9 +1193,20 @@ ...@@ -957,9 +1193,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 23,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [] "source": []
}, },
{ {
......
...@@ -560,7 +560,7 @@ ...@@ -560,7 +560,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -591,7 +591,7 @@ ...@@ -591,7 +591,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -633,7 +633,7 @@ ...@@ -633,7 +633,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -652,7 +652,7 @@ ...@@ -652,7 +652,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 43, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -660,7 +660,7 @@ ...@@ -660,7 +660,7 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Soy la fuente: El Universal\n", "Soy la fuente: El Universal\n",
"Titulo de la nota\t02-14-19\n", "Titulo de la nota\t02-19-19\n",
"CGarcia\n", "CGarcia\n",
"Sección: Investigacion\n", "Sección: Investigacion\n",
"La Jornada\n", "La Jornada\n",
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 159,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
" [10, 11, 12, 13, 14]], dtype=int16)" " [10, 11, 12, 13, 14]], dtype=int16)"
] ]
}, },
"execution_count": 6, "execution_count": 159,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -868,47 +868,83 @@ ...@@ -868,47 +868,83 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 75, "execution_count": 20,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cuantos elementos tienen tus vectores: \n",
"2\n",
"Ingresa los elementos uno a uno del vector 1 \n",
"\n",
"Ingresa el elemento: 1\n",
"10\n",
"Ingresa el elemento: 2\n",
"20\n",
"\n",
"Ingresa los elementos uno a uno del vector 2 \n",
"\n",
"Ingresa el elemento: 1\n",
"2\n",
"Ingresa el elemento: 2\n",
"4\n",
"\n",
"Vector 1: [10, 20]\n",
"Vector 2: [2, 4]\n",
"Suma Producto: 100\n"
]
}
],
"source": [ "source": [
"from functools import reduce\n",
"#Tarea, generar el producto punto\n", "#Tarea, generar el producto punto\n",
"def prod_punto(v1,v2):\n", "def prod_punto(v1,v2):\n",
" '''\n", " '''\n",
" Soy un docstring\n", " Soy un docstring\n",
" '''\n", " '''\n",
" pass\n", " v1,v2,v3=[],[],[]\n",
" #return prod_punto" " elementos=int(input(\"Cuantos elementos tienen tus vectores: \\n\"))\n",
" \n",
" print(\"Ingresa los elementos uno a uno del vector 1 \\n\")\n",
" for elemento in range(0,elementos):\n",
" elem=int(input(\"Ingresa el elemento: \"+str(elemento+1)+\"\\n\"))\n",
" v1.append(elem)\n",
" \n",
" print(\"\\nIngresa los elementos uno a uno del vector 2 \\n\")\n",
" for elemento in range(0,elementos):\n",
" elem=int(input(\"Ingresa el elemento: \"+str(elemento+1)+\"\\n\"))\n",
" v2.append(elem)\n",
" \n",
" v3=list(map(lambda x,y: y*x, v1,v2))\n",
" sumap=reduce((lambda x,y: x+y),v3)\n",
" \n",
" print(\"\\nVector 1: \"+str(v1)+\"\\n\"+\"Vector 2: \"+str(v2)+\"\\n\"+\"Suma Producto: \"+str(sumap))\n",
" return sumap\n",
" #return prod_punto\n",
"x=prod_punto(3,4)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 170, "execution_count": 162,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 2, 3], [4, 5, 6]]\n"
]
},
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"2" "100"
] ]
}, },
"execution_count": 170, "execution_count": 162,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"#Matriz sin numpy para la tarea\n", "#Comprobacion con Numpy\n",
"aaa=[[1,2,3],[4,5,6]]\n", "np.matmul([10,20], [2,4])"
"print(aaa)\n",
"aaa[0][1]"
] ]
}, },
{ {
...@@ -920,32 +956,232 @@ ...@@ -920,32 +956,232 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 165, "execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'def matmult(a,b):\\n zip_b = zip(*b)\\n #uncomment next line if python 3 : \\n zip_b = list(zip_b)\\n return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \\n for col_b in zip_b] for row_a in a]\\n'"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tamMatriz():\n",
" n=int(input(\"Ingrese las filas de la matriz: \\n\"))\n",
" m=int(input(\"Ingrese las columnas de la matriz: \\n\"))\n",
" return [n,m]\n",
"\n",
"def creaMatriz(nFila, nCol):\n",
" matriz = []\n",
" for i in range(nFila):\n",
" listaFila = []\n",
" for j in range(nCol):\n",
" val=int(input(\"Ingresa el valor del elemento\"+str(i)+\",\"+str(j)+\": \\n\"))\n",
" listaFila.append(val)\n",
" matriz.append(listaFila)\n",
"\n",
" return matriz\n",
"\n",
"def mmult1(m1, m2):\n",
" fila_A = len(m1)\n",
" cols_A = len(m1[0])\n",
" fila_B = len(m2)\n",
" cols_B = len(m2[0])\n",
"\n",
" if cols_A != fila_B:\n",
" print (\"Imposible resolver, la dimensionalidad de las matrices no cuadra.\")\n",
" return\n",
"\n",
" C = [[0 for fila in range(cols_B)] for col in range(fila_A)]\n",
" print (C)\n",
"\n",
" for i in range(fila_A):\n",
" for j in range(cols_B):\n",
" for k in range(cols_A):\n",
" C[i][j] += m1[i][k] * m2[k][j]\n",
" return C\n",
"\n",
"#Me encantaria que me podieran explicar esta ya que es extremadamente eficiente :O\n",
"\n",
"'''def matmult(a,b):\n",
" zip_b = zip(*b)\n",
" #uncomment next line if python 3 : \n",
" zip_b = list(zip_b)\n",
" return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \n",
" for col_b in zip_b] for row_a in a]\n",
"'''\n",
"\n",
"'''b=mat2\n",
"print(b)\n",
"zip_b = zip(*b)\n",
"for col_b in zip_b:\n",
" print(col_b)\n",
"'''\n",
"\n",
"### Muy PRO\n",
"m = np.array([[sum(row_a*col_b) for col_b in b.T] for row_a in a])\n",
"print(m, type(m))\n",
"###"
]
},
{
"cell_type": "code",
"execution_count": 212,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"A = [[1,2,3],\n", "a=np.array(mat1)\n",
" [4,5,6]]\n", "b=np.array(mat2)\n"
"B = [[7,8],[9,10],[11,12]]"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 162, "execution_count": 118,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingrese las filas de la matriz: \n",
"2\n",
"Ingrese las columnas de la matriz: \n",
"3\n"
]
}
],
"source": [
"aa=tamMatriz()"
]
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingrese las filas de la matriz: \n",
"3\n",
"Ingrese las columnas de la matriz: \n",
"2\n"
]
}
],
"source": [
"bb=tamMatriz()"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa el valor del elemento0,0: \n",
"1\n",
"Ingresa el valor del elemento0,1: \n",
"2\n",
"Ingresa el valor del elemento0,2: \n",
"3\n",
"Ingresa el valor del elemento1,0: \n",
"4\n",
"Ingresa el valor del elemento1,1: \n",
"5\n",
"Ingresa el valor del elemento1,2: \n",
"6\n"
]
}
],
"source": [
"mat1 = creaMatriz(aa[0],aa[1])"
]
},
{
"cell_type": "code",
"execution_count": 134,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa el valor del elemento0,0: \n",
"10\n",
"Ingresa el valor del elemento0,1: \n",
"20\n",
"Ingresa el valor del elemento1,0: \n",
"30\n",
"Ingresa el valor del elemento1,1: \n",
"40\n",
"Ingresa el valor del elemento2,0: \n",
"50\n",
"Ingresa el valor del elemento2,1: \n",
"60\n"
]
}
],
"source": [
"mat2=creaMatriz(bb[0],bb[1])"
]
},
{
"cell_type": "code",
"execution_count": 210,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"[[1, 2, 3], [4, 5, 6]]\n", "[[0, 0], [0, 0]]\n"
"[[7, 8], [9, 10], [11, 12]]\n" ]
},
{
"data": {
"text/plain": [
"[[220, 280], [490, 640]]"
]
},
"execution_count": 210,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mmult1(mat1,mat2)"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[220, 280],\n",
" [490, 640]])"
] ]
},
"execution_count": 206,
"metadata": {},
"output_type": "execute_result"
} }
], ],
"source": [ "source": [
"print(A)\n", "#Comprobacion con Numpy\n",
"print(B)" "np.matmul(mat1, mat2)"
] ]
}, },
{ {
...@@ -957,9 +1193,20 @@ ...@@ -957,9 +1193,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 23,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [] "source": []
}, },
{ {
......
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