Skip to content

Commit 58dd50b

Browse files
committed
R + Python con reticulate
1 parent e7c723b commit 58dd50b

File tree

79 files changed

+1488
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1488
-1
lines changed

ejercicios/data_frame_plot.png

50.9 KB
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}

scripts/tema1/07-io.ipynb

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Entrada y salida de datos"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"Hola, me llamo Juan Gabriel\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"print(\"Hola, me llamo Juan Gabriel\")"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 6,
30+
"metadata": {},
31+
"outputs": [
32+
{
33+
"name": "stdout",
34+
"output_type": "stream",
35+
"text": [
36+
"5\n"
37+
]
38+
}
39+
],
40+
"source": [
41+
"x = 5\n",
42+
"print(x)"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 7,
48+
"metadata": {},
49+
"outputs": [
50+
{
51+
"name": "stdout",
52+
"output_type": "stream",
53+
"text": [
54+
"El valor de la variable x es: 5\n"
55+
]
56+
}
57+
],
58+
"source": [
59+
"print(\"El valor de la variable x es: \"+str(x))"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"texto = input(\"Introduce aquí tu nombre: \")"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": []
77+
}
78+
],
79+
"metadata": {
80+
"kernelspec": {
81+
"display_name": "Python 3",
82+
"language": "python",
83+
"name": "python3"
84+
},
85+
"language_info": {
86+
"codemirror_mode": {
87+
"name": "ipython",
88+
"version": 3
89+
},
90+
"file_extension": ".py",
91+
"mimetype": "text/x-python",
92+
"name": "python",
93+
"nbconvert_exporter": "python",
94+
"pygments_lexer": "ipython3",
95+
"version": "3.5.5"
96+
}
97+
},
98+
"nbformat": 4,
99+
"nbformat_minor": 2
100+
}

scripts/tema12/01-diamonds.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ output: html_document
66
---
77

88
```{r setup, include=FALSE}
9-
knitr::opts_chunk$set(echo = TRUE)
9+
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
1010
```
1111

1212
# Análisis de los diamantes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
base

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-10_4779ac4a62c84c0a676202a8ae084b0e.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-1_096a7a0b64254b3c70c246e7568ba470.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-2_93ece90322530318e6fe2ea6e420dc8b.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-3_19ca6f580f71f07011bd9c45bc6c70b8.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-4_477b9ef6be9ba8dab22a82595eed5e31.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-5_b721f9d48526f5fde3e9cddbf339d025.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-6_ad2fdaad3a00602260ce4b0dc5c7df5a.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-7_8ad9c425595259b3c344215090e0ca6d.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-8_41f1b40282d4e05a41fedf2e23619116.rdb

Whitespace-only changes.

scripts/tema12/01-diamonds_cache/html/unnamed-chunk-9_4c8906161007bde0450d5b26c7dbe3d0.rdb

Whitespace-only changes.

scripts/tema12/02-pokemon.Rmd

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: "Pokemon"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "9/1/2019"
5+
output: html_document
6+
---
7+
8+
```{r setup, include=FALSE}
9+
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
10+
library(reticulate)
11+
use_python("/anaconda3/bin/python")
12+
```
13+
14+
# Pokemon (Py -> R)
15+
16+
## Limpieza de datos en Python
17+
18+
```{python}
19+
import pandas as pd
20+
pokemon = pd.read_csv("../../data/Pokemon.csv")
21+
print(pokemon.head())
22+
print(pokemon.shape)
23+
pokemon = pokemon[pokemon["Generation"]==1]
24+
pokemon = pokemon[["Type 1", "Type 2", "Speed"]]
25+
print(pokemon.shape)
26+
pokemon = pokemon.dropna()
27+
print(pokemon.shape)
28+
```
29+
30+
## Transmisión de los datos de Python a R
31+
```{r, fig.width=7, fig.height=4}
32+
hist(py$pokemon[,"Speed"], breaks = 10, main = "Velocidad de los Pokemon")
33+
```
34+
35+
# Pokemon (R -> Py)
36+
## Carga de datos en R
37+
```{r}
38+
pokemon2 <- read.csv("../../data/Pokemon.csv", header = TRUE)
39+
head(pokemon2)
40+
library(tidyverse)
41+
pokemon2 <- pokemon2 %>%
42+
filter(Generation == 1) %>%
43+
select(Type.1, Type.2, Speed) %>%
44+
na.omit()
45+
summary(pokemon2)
46+
```
47+
48+
## Transmisión de datos de R a Python
49+
```{python}
50+
print(r.pokemon2.head())
51+
```
52+

scripts/tema12/02-pokemon.html

+309
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
base
2+
reticulate
3+
tidyverse
4+
ggplot2
5+
tibble
6+
tidyr
7+
readr
8+
purrr
9+
dplyr
10+
stringr
11+
forcats
12+
bindrcpp

scripts/tema12/02-pokemon_cache/html/unnamed-chunk-1_1c495cfb97f70130860b49921495a756.rdb

Whitespace-only changes.

scripts/tema12/02-pokemon_cache/html/unnamed-chunk-2_f690a6a77dff8b57800e18e0f8d4a9ea.rdb

Whitespace-only changes.

scripts/tema12/02-pokemon_cache/html/unnamed-chunk-4_c752c7d5520433c63985be7e17c14657.rdb

Whitespace-only changes.

scripts/tema12/03-flights.Rmd

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: "Vuelos de NYC"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "9/1/2019"
5+
output: html_document
6+
---
7+
8+
```{r setup, include=FALSE}
9+
knitr::opts_chunk$set(echo = TRUE)
10+
library(reticulate)
11+
```
12+
13+
## Vuelos de NYC
14+
15+
```{r}
16+
data <- nycflights13::flights
17+
head(data)
18+
nrow(data)
19+
summary(data)
20+
```
21+
22+
```{python}
23+
pydata = r.data
24+
pydata = pydata[pydata["dest"]=="ORD"]
25+
pydata = pydata[['carrier', 'dep_delay', 'arr_delay', 'origin']]
26+
pydata = pydata[pydata['arr_delay']<6*60]
27+
pydata = pydata.dropna()
28+
print(pydata.head())
29+
print(pydata.shape)
30+
```
31+
32+
```{r}
33+
summary(py$pydata)
34+
boxplot(arr_delay~origin, data = py$pydata, main = "Retraso de los vuelo hacia Orlando desde NYC")
35+
```
36+

scripts/tema12/03-flights.html

+309
Large diffs are not rendered by default.

scripts/tema12/04-mtcars.Rmd

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: "Coches"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "9/1/2019"
5+
output: html_document
6+
---
7+
8+
```{r setup, include=FALSE}
9+
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
10+
```
11+
12+
## Análisis de los coches (mtcars)
13+
14+
### Carga de datos
15+
```{python}
16+
from ggplot import mtcars
17+
data = mtcars
18+
data.index = data["name"]
19+
print(data.head())
20+
```
21+
22+
### Medidas de centralización
23+
```{python}
24+
print(data.mean()) # Media por columnas
25+
print(data.mean(axis = 1)) # Media por filas
26+
print(data.median())
27+
print(mtcars.mode())
28+
```
29+
30+
31+
### Medidas vs distribuciones
32+
```{python}
33+
import numpy as np
34+
import pandas as pd
35+
import matplotlib.pyplot as plt
36+
plt.clf()
37+
norm_data = pd.DataFrame(np.random.normal(size=100000))
38+
norm_data.plot(kind="density", figsize=(10,10))
39+
plt.vlines(norm_data.mean(), ymin = 0, ymax = 0.4, linewidth=5.0, color = "green")
40+
plt.vlines(norm_data.median(), ymin = 0, ymax = 0.4,
41+
linewidth = 2.0, color = "red")
42+
plt.show()
43+
44+
45+
plt.clf()
46+
skewed_data = pd.DataFrame(np.random.exponential(size=100000))
47+
skewed_data.plot(kind="density", figsize=(10,10), xlim = (-1,5))
48+
plt.vlines(skewed_data.mean(), ymin = 0, ymax = 1.0, linewidth=5.0, color = "green")
49+
plt.vlines(skewed_data.median(), ymin = 0, ymax = 1.0,
50+
linewidth = 2.0, color = "red")
51+
plt.show()
52+
53+
54+
norm_data = np.random.normal(size = 50)
55+
outliers = np.random.normal(15, size = 3)
56+
combined_data = pd.DataFrame(np.concatenate((norm_data, outliers), axis = 0))
57+
58+
combined_data.plot(kind="density", figsize=(10,10), xlim = (-5,20))
59+
plt.vlines(combined_data.mean(), ymin = 0, ymax = 0.3, linewidth=5.0, color = "green")
60+
plt.vlines(combined_data.median(), ymin = 0, ymax = 0.3,
61+
linewidth = 2.0, color = "red")
62+
plt.show()
63+
```
64+

scripts/tema12/04-mtcars.html

+417
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
base

scripts/tema12/04-mtcars_cache/html/unnamed-chunk-1_5aa8e92586c06f87372ee9224a714830.rdb

Whitespace-only changes.

scripts/tema12/04-mtcars_cache/html/unnamed-chunk-2_79c3d81e9889d036bd7a43c4b2629709.rdb

Whitespace-only changes.

scripts/tema12/04-mtcars_cache/html/unnamed-chunk-3_ca3b58f96a4c64b743f5686c7fcc0874.rdb

Whitespace-only changes.

0 commit comments

Comments
 (0)