Skip to content

Commit

Permalink
R + Python con reticulate
Browse files Browse the repository at this point in the history
  • Loading branch information
joanby committed Jan 12, 2019
1 parent e7c723b commit 58dd50b
Show file tree
Hide file tree
Showing 79 changed files with 1,488 additions and 1 deletion.
Binary file added ejercicios/data_frame_plot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions scripts/tema1/.ipynb_checkpoints/07-io-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
100 changes: 100 additions & 0 deletions scripts/tema1/07-io.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Entrada y salida de datos"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hola, me llamo Juan Gabriel\n"
]
}
],
"source": [
"print(\"Hola, me llamo Juan Gabriel\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"x = 5\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"El valor de la variable x es: 5\n"
]
}
],
"source": [
"print(\"El valor de la variable x es: \"+str(x))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"texto = input(\"Introduce aquí tu nombre: \")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion scripts/tema12/01-diamonds.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
```

# Análisis de los diamantes
Expand Down
1 change: 1 addition & 0 deletions scripts/tema12/01-diamonds_cache/html/__packages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
base
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions scripts/tema12/02-pokemon.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: "Pokemon"
author: "Curso de Estadística Descriptiva"
date: "9/1/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
library(reticulate)
use_python("/anaconda3/bin/python")
```

# Pokemon (Py -> R)

## Limpieza de datos en Python

```{python}
import pandas as pd
pokemon = pd.read_csv("../../data/Pokemon.csv")
print(pokemon.head())
print(pokemon.shape)
pokemon = pokemon[pokemon["Generation"]==1]
pokemon = pokemon[["Type 1", "Type 2", "Speed"]]
print(pokemon.shape)
pokemon = pokemon.dropna()
print(pokemon.shape)
```

## Transmisión de los datos de Python a R
```{r, fig.width=7, fig.height=4}
hist(py$pokemon[,"Speed"], breaks = 10, main = "Velocidad de los Pokemon")
```

# Pokemon (R -> Py)
## Carga de datos en R
```{r}
pokemon2 <- read.csv("../../data/Pokemon.csv", header = TRUE)
head(pokemon2)
library(tidyverse)
pokemon2 <- pokemon2 %>%
filter(Generation == 1) %>%
select(Type.1, Type.2, Speed) %>%
na.omit()
summary(pokemon2)
```

## Transmisión de datos de R a Python
```{python}
print(r.pokemon2.head())
```

309 changes: 309 additions & 0 deletions scripts/tema12/02-pokemon.html

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions scripts/tema12/02-pokemon_cache/html/__packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
base
reticulate
tidyverse
ggplot2
tibble
tidyr
readr
purrr
dplyr
stringr
forcats
bindrcpp
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions scripts/tema12/03-flights.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
title: "Vuelos de NYC"
author: "Curso de Estadística Descriptiva"
date: "9/1/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
```

## Vuelos de NYC

```{r}
data <- nycflights13::flights
head(data)
nrow(data)
summary(data)
```

```{python}
pydata = r.data
pydata = pydata[pydata["dest"]=="ORD"]
pydata = pydata[['carrier', 'dep_delay', 'arr_delay', 'origin']]
pydata = pydata[pydata['arr_delay']<6*60]
pydata = pydata.dropna()
print(pydata.head())
print(pydata.shape)
```

```{r}
summary(py$pydata)
boxplot(arr_delay~origin, data = py$pydata, main = "Retraso de los vuelo hacia Orlando desde NYC")
```

309 changes: 309 additions & 0 deletions scripts/tema12/03-flights.html

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions scripts/tema12/04-mtcars.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: "Coches"
author: "Curso de Estadística Descriptiva"
date: "9/1/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE)
```

## Análisis de los coches (mtcars)

### Carga de datos
```{python}
from ggplot import mtcars
data = mtcars
data.index = data["name"]
print(data.head())
```

### Medidas de centralización
```{python}
print(data.mean()) # Media por columnas
print(data.mean(axis = 1)) # Media por filas
print(data.median())
print(mtcars.mode())
```


### Medidas vs distribuciones
```{python}
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.clf()
norm_data = pd.DataFrame(np.random.normal(size=100000))
norm_data.plot(kind="density", figsize=(10,10))
plt.vlines(norm_data.mean(), ymin = 0, ymax = 0.4, linewidth=5.0, color = "green")
plt.vlines(norm_data.median(), ymin = 0, ymax = 0.4,
linewidth = 2.0, color = "red")
plt.show()
plt.clf()
skewed_data = pd.DataFrame(np.random.exponential(size=100000))
skewed_data.plot(kind="density", figsize=(10,10), xlim = (-1,5))
plt.vlines(skewed_data.mean(), ymin = 0, ymax = 1.0, linewidth=5.0, color = "green")
plt.vlines(skewed_data.median(), ymin = 0, ymax = 1.0,
linewidth = 2.0, color = "red")
plt.show()
norm_data = np.random.normal(size = 50)
outliers = np.random.normal(15, size = 3)
combined_data = pd.DataFrame(np.concatenate((norm_data, outliers), axis = 0))
combined_data.plot(kind="density", figsize=(10,10), xlim = (-5,20))
plt.vlines(combined_data.mean(), ymin = 0, ymax = 0.3, linewidth=5.0, color = "green")
plt.vlines(combined_data.median(), ymin = 0, ymax = 0.3,
linewidth = 2.0, color = "red")
plt.show()
```

417 changes: 417 additions & 0 deletions scripts/tema12/04-mtcars.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions scripts/tema12/04-mtcars_cache/html/__packages
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
base
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 58dd50b

Please sign in to comment.