* "Python es ya el lenguaje de introducción más popular en las universidades norteamericanas" http://www.genbetadev.com/formacion/python-es-ya-el-lenguaje-de-introduccion-mas-popular-en-las-universidades-norteamericanas
print("Hello, world!")
Hello, world!
def fib(N):
a, b = 0, 1
for ii in range(N):
a, b = b, a + b
return b
for ii in range(8):
print(fib(ii))
1 1 2 3 5 8 13 21
Motivación:
"Make [Python] equivalent to a basic scientific calculator."
import numpy as np
np.array([
[1, 2, 3],
[4, 5, 6]
])
array([[1, 2, 3], [4, 5, 6]])
np.linspace(0, 10, 5)
array([ 0. , 2.5, 5. , 7.5, 10. ])
_.mean()
5.0
Solución: pandas
import pandas as pd
pd.Series([1, 3, 5, np.nan, 6, 8])
0 1 1 3 2 5 3 NaN 4 6 5 8 dtype: float64
dates = pd.date_range('20140701', periods=6)
dates
<class 'pandas.tseries.index.DatetimeIndex'> [2014-07-01, ..., 2014-07-06] Length: 6, Freq: D, Timezone: None
datos = pd.DataFrame(np.random.randn(6,4), index=dates,
columns=list('ABCD'))
datos
A | B | C | D | |
---|---|---|---|---|
2014-07-01 | -0.278300 | 0.424450 | -1.508621 | -1.746290 |
2014-07-02 | 0.941110 | 0.050437 | 0.371443 | -0.508548 |
2014-07-03 | 0.761886 | 1.308292 | 0.871853 | 0.548142 |
2014-07-04 | 1.972977 | 0.213835 | -1.778572 | -0.165594 |
2014-07-05 | -0.783794 | -1.057578 | 0.067174 | -0.141001 |
2014-07-06 | 0.326622 | -1.085902 | -0.236260 | -0.403086 |
6 rows × 4 columns
datos.describe()
A | B | C | D | |
---|---|---|---|---|
count | 6.000000 | 6.000000 | 6.000000 | 6.000000 |
mean | 0.490083 | -0.024411 | -0.368831 | -0.402730 |
std | 0.971202 | 0.920232 | 1.056548 | 0.754353 |
min | -0.783794 | -1.085902 | -1.778572 | -1.746290 |
25% | -0.127069 | -0.780575 | -1.190531 | -0.482183 |
50% | 0.544254 | 0.132136 | -0.084543 | -0.284340 |
75% | 0.896304 | 0.371796 | 0.295376 | -0.147149 |
max | 1.972977 | 1.308292 | 0.871853 | 0.548142 |
8 rows × 4 columns
import matplotlib.pyplot as plt
%matplotlib inline
datos["A"].plot()
<matplotlib.axes.AxesSubplot at 0x7fd6a471ed10>
datos["A"].plot()
plt.xlabel("Fecha")
plt.ylabel("Columna A")
plt.legend(["Datos"])
plt.title("Gráfica 1")
<matplotlib.text.Text at 0x7fd6a4189090>
¡Y mucho más!
La joya de la corona: el notebook
¿Por qué usar solo Python?
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
%load_ext rpy2.ipython
%Rpush X Y
%R lm(Y~X)$coef
<FloatVector - Python:0x7fd6a2fdf5f0 / R:0x2d86ec8> [3.200000, 0.900000]
b = %R a=resid(lm(Y~X))
%Rpull a
print(a)
%R -o a
1 2 3 4 5 -0.2 0.9 -1.0 0.1 0.2
%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)
Call: lm(formula = Y ~ X) Residuals: 1 2 3 4 5 -0.2 0.9 -1.0 0.1 0.2 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 3.2000 0.6164 5.191 0.0139 * X 0.9000 0.2517 3.576 0.0374 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.7958 on 3 degrees of freedom Multiple R-squared: 0.81, Adjusted R-squared: 0.7467 F-statistic: 12.79 on 1 and 3 DF, p-value: 0.03739
Puedo usar esta técnica con multitud de lenguajes:
¡y más! https://github.com/ipython/ipython/wiki/Extensions-Index
P: ¿Por qué lo llaman IPython si puede iteractuar con multitud de lenguajes diferentes?
**Muchas gracias :)**