"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",
...
...
@@ -24,7 +24,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 1,
"metadata": {},
"outputs": [
{
...
...
@@ -53,7 +53,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 2,
"metadata": {},
"outputs": [
{
...
...
@@ -63,7 +63,7 @@
" [3.+0.j, 4.+0.j]])"
]
},
"execution_count": 6,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -75,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"metadata": {},
"outputs": [
{
...
...
@@ -86,7 +86,7 @@
" [0., 0., 0., 0.]])"
]
},
"execution_count": 7,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -98,7 +98,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"metadata": {},
"outputs": [
{
...
...
@@ -109,7 +109,7 @@
" [1., 1., 1., 1.]])"
]
},
"execution_count": 8,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -120,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 5,
"metadata": {},
"outputs": [
{
...
...
@@ -131,7 +131,7 @@
" [1., 1., 1., 1.]])"
]
},
"execution_count": 9,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -142,7 +142,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 6,
"metadata": {},
"outputs": [
{
...
...
@@ -155,7 +155,7 @@
" [0., 0., 0., 0., 1.]])"
]
},
"execution_count": 39,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -173,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -183,7 +183,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 8,
"metadata": {},
"outputs": [
{
...
...
@@ -192,7 +192,7 @@
"array([20, 29, 38, 47])"
]
},
"execution_count": 11,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -204,7 +204,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"outputs": [
{
...
...
@@ -213,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": 13,
"execution_count": 10,
"metadata": {},
"outputs": [
{
...
...
@@ -234,7 +234,7 @@
"array([0, 1, 4, 9])"
]
},
"execution_count": 13,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -246,7 +246,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 11,
"metadata": {},
"outputs": [
{
...
...
@@ -255,7 +255,7 @@
"array([ True, True, True, False])"
]
},
"execution_count": 14,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -267,7 +267,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 12,
"metadata": {},
"outputs": [
{
...
...
@@ -276,7 +276,7 @@
"array([ 0, 30, 80, 150])"
]
},
"execution_count": 15,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -286,135 +286,135 @@
"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": 16,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
"66"
]
},
"execution_count": 16,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a@b == a.dot(b)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([[1, 0], [0, 1]])\n",
"b = np.array([[4, 1], [2, 2]]) "
"c= np.arange(12).reshape(3,4)\n",
"c.sum()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True],\n",
" [ True, True]])"
"array([12, 15, 18, 21])"
]
},
"execution_count": 18,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.matmul(a, b) == a.dot(b)\n"
"c.sum(axis=0) # Suma por Columna"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"66"
"array([ 6, 22, 38])"
]
},
"execution_count": 19,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c= np.arange(12).reshape(3,4)\n",
"c.sum()"
"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": 20,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([12, 15, 18, 21])"
"True"
]
},
"execution_count": 20,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=0) # Suma por Columna"
"a@b == a.dot(b)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1, 0], [0, 1]])\n",
"B = np.array([[4, 1], [2, 2]]) "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6, 22, 38])"
"array([[ True, True],\n",
" [ True, True]])"
]
},
"execution_count": 21,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c.sum(axis=1) #Suma por Fila"
"np.matmul(A, B) == A.dot(B)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Elementos, filas, columnas y subarreglos."
"### Elementos, filas, columnas y submatrices."
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 19,
"metadata": {},
"outputs": [
{
...
...
@@ -427,7 +427,7 @@
" [40, 41, 42, 43]])"
]
},
"execution_count": 22,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -435,13 +435,13 @@
"source": [
"def f(x,y):\n",
" return 10*x+y\n",
"b = np.fromfunction(f,(5,4),dtype=int)\n",
"b"
"B = np.fromfunction(f,(5,4),dtype=int)\n",
"B"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 20,
"metadata": {},
"outputs": [
{
...
...
@@ -450,18 +450,18 @@
"3"
]
},
"execution_count": 23,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[0,3]"
"B[0,3]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 21,
"metadata": {},
"outputs": [
{
...
...
@@ -470,18 +470,18 @@
"array([10, 11, 12, 13])"
]
},
"execution_count": 24,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[1,:]"
"B[1,:]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 22,
"metadata": {},
"outputs": [
{
...
...
@@ -490,18 +490,18 @@
"array([ 1, 11, 21, 31, 41])"
]
},
"execution_count": 25,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[:,1]"
"B[:,1]"
]
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 23,
"metadata": {},
"outputs": [
{
...
...
@@ -511,13 +511,13 @@
" [11, 12]])"
]
},
"execution_count": 26,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b[:2,1:3]"
"B[:2,1:3]"
]
},
{
...
...
@@ -529,7 +529,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 24,
"metadata": {},
"outputs": [
{
...
...
@@ -545,13 +545,13 @@
}
],
"source": [
"for row in b:\n",
"for row in B:\n",
" print(row)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 25,
"metadata": {},
"outputs": [
{
...
...
@@ -582,7 +582,7 @@
}
],
"source": [
"for element in b.flat:\n",
"for element in B.flat:\n",
" print(element)"
]
},
...
...
@@ -595,18 +595,18 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[8., 0., 8., 9.],\n",
" [6., 1., 5., 9.],\n",
" [9., 3., 3., 2.]])"
"array([[2., 2., 9., 4.],\n",
" [6., 6., 3., 6.],\n",
" [7., 9., 9., 8.]])"
]
},
"execution_count": 29,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -618,7 +618,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 27,
"metadata": {},
"outputs": [
{
...
...
@@ -627,7 +627,7 @@
"(3, 4)"
]
},
"execution_count": 30,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -638,21 +638,21 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[8., 0.],\n",
" [8., 9.],\n",
" [6., 1.],\n",
" [5., 9.],\n",
" [9., 3.],\n",
" [3., 2.]])"
"array([[2., 2.],\n",
" [9., 4.],\n",
" [6., 6.],\n",
" [3., 6.],\n",
" [7., 9.],\n",
" [9., 8.]])"
]
},
"execution_count": 31,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -663,19 +663,19 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[8., 6., 9.],\n",
" [0., 1., 3.],\n",
" [8., 5., 3.],\n",
" [9., 9., 2.]])"
"array([[2., 6., 7.],\n",
" [2., 6., 9.],\n",
" [9., 3., 9.],\n",
" [4., 6., 8.]])"
]
},
"execution_count": 32,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -686,7 +686,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 30,
"metadata": {},
"outputs": [
{
...
...
@@ -698,7 +698,7 @@
" [ True, True, True]])"
]
},
"execution_count": 33,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -709,7 +709,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 31,
"metadata": {},
"outputs": [
{
...
...
@@ -718,7 +718,7 @@
"(4, 3)"
]
},
"execution_count": 34,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -729,24 +729,24 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[8., 0., 8., 9.],\n",
" [6., 1., 5., 9.],\n",
" [9., 3., 3., 2.]])"
"array([[2., 2., 9., 4.],\n",
" [6., 6., 3., 6.],\n",
" [7., 9., 9., 8.]])"
]
},
"execution_count": 35,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# La dimencion con -1 se calcula automaticamente\n",
"# La dimensión con -1 se calcula automaticamente\n",
"a.reshape(3,-1)"
]
},
...
...
@@ -761,7 +761,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -778,7 +778,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -840,7 +840,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -850,21 +850,21 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0.994272\n",
"b 0.530519\n",
"c 1.162452\n",
"d -0.981436\n",
"e -1.283798\n",
"a -0.192610\n",
"b 0.957230\n",
"c -0.917233\n",
"d 1.156523\n",
"e -0.539532\n",
"dtype: float64"
]
},
"execution_count": 51,
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -876,21 +876,21 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 2.042498\n",
"1 -0.964070\n",
"2 -0.687132\n",
"3 0.623300\n",
"4 1.366322\n",
"0 -0.856313\n",
"1 -0.342923\n",
"2 -1.398583\n",
"3 0.170869\n",
"4 1.049196\n",
"dtype: float64"
]
},
"execution_count": 46,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -901,7 +901,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 38,
"metadata": {},
"outputs": [
{
...
...
@@ -913,7 +913,7 @@
"dtype: int64"
]
},
"execution_count": 47,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -925,7 +925,7 @@
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 39,
"metadata": {},
"outputs": [
{
...
...
@@ -938,7 +938,7 @@
"dtype: float64"
]
},
"execution_count": 48,
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -957,16 +957,16 @@
},
{
"cell_type": "code",
"execution_count": 53,
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9942721192063438"
"-0.19261011238315226"
]
},
"execution_count": 53,
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -977,19 +977,19 @@
},
{
"cell_type": "code",
"execution_count": 54,
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0.994272\n",
"b 0.530519\n",
"c 1.162452\n",
"a -0.192610\n",
"b 0.957230\n",
"c -0.917233\n",
"dtype: float64"
]
},
"execution_count": 54,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1000,19 +1000,18 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a 0.994272\n",
"b 0.530519\n",
"c 1.162452\n",
"b 0.957230\n",
"d 1.156523\n",
"dtype: float64"
]
},
"execution_count": 55,
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1023,21 +1022,21 @@
},
{
"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 -0.385220\n",
"b 1.914460\n",
"c -1.834466\n",
"d 2.313046\n",
"e -1.079065\n",
"dtype: float64"
]
},
"execution_count": 56,
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1048,21 +1047,21 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a True\n",
"b False\n",
"c True\n",
"d False\n",
"a False\n",
"b True\n",
"c False\n",
"d True\n",
"e False\n",
"dtype: bool"
]
},
"execution_count": 57,
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1073,16 +1072,16 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.9942721192063438"
"-0.19261011238315226"
]
},
"execution_count": 58,
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1100,7 +1099,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -1110,7 +1109,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 47,
"metadata": {},
"outputs": [
{
...
...
@@ -1119,7 +1118,7 @@
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 60,
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1130,7 +1129,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 48,
"metadata": {},
"outputs": [
{
...
...
@@ -1149,7 +1148,7 @@
"dtype: int64"
]
},
"execution_count": 62,
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1160,7 +1159,7 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": 49,
"metadata": {},
"outputs": [
{
...
...
@@ -1169,7 +1168,7 @@
"array([ 4, 6, 8, 10, 12, 14])"
]
},
"execution_count": 75,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1180,7 +1179,7 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 50,
"metadata": {},
"outputs": [
{
...
...
@@ -1199,7 +1198,7 @@
"dtype: float64"
]
},
"execution_count": 76,
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -1208,6 +1207,718 @@
"(s[:6]+s[4:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.3.2 DataFrame\n",
"DataFrame es un estructura bidimensiona etiquetada con columnas que pueden ser de diferentes tipos, es el objeto mas usado en pandas, se puede pensar en ella como un diccionario de **Series**. **DataFrame** acepta diferentes tipos de entradas como:\n",
"* Diccionarios de arreglos unidimensionales, listas dicionarios o series. \n",