# **Una breve introducción al ecosisteam de Python**

<hr/>

*Por: Martin Vuelta ([zodiacfireworks](https://github.com/zodiacfireworks))*

*Email:* `martin.vuelta@softbutterfly.io`

<hr/>

## 1. ¿Qué es Python?

    Python is a programming language that lets you work more quickly and integrate your systems more effectively.

### *Ejemplos*

a. Hola Mundo


```python
print("Hola Mundo!")
```

b. Saludos


```python
name = input("What's your name? ")
greetings = f"Hello {name}!"
print(greetings)
```

## 2. Módulos

    Consider a module to be the same as a code library. A file or set of files containing a set of functions you want to include in your application.

### *Ejemplos*

a. Gravity


```python
from math import pi, sin, sqrt

def g(phi):
    sin2phi = sin(phi) ** 2
    return 9.7803253359 * ( 1 + 0.001931850400 * sin2phi ) / sqrt(1 - 0.006694384442 * sin2phi)

latitude_deg = input("What's your latitude? [deg]")
latitude_rad = float(latitude_deg) * pi / 180
gravity = f"La gravedad a {latitude_deg}deg es de {g(latitude_rad)} m/s^2"
print(gravity)
```

## 3. Paquetes

    A package, in essence, is a module or a set of modules prepared to be distributed. The most common way of distribution is through the Python Package Index (PyPI).



### 3.1. Paquetes comunes en el Python club

#### Numpy

    Base N-dimensional array package

##### *Ejemplos*


```python
import numpy as np

sample_array = np.array([(1.5,2,3), (4,5,6)])
sample_array
```


```python
import numpy as np

sample_array = np.array([(1.5,2,3), (4,5,6)])
print(sample_array)
```


```python
zeros_array = np.zeros((3,4))
print("Array of zeros")
print(zeros_array)
print()

ones_array = np.ones((2,3,4), dtype=np.int16)
print("Array of ones")
print(ones_array)
print()

empty_array = np.empty((2,3,4))
print("Empty array")
print(empty_array)
```

### Scipy 

    Fundamental library for scientific computing

##### *Ejemplos*


```python
from numpy import poly1d

p = poly1d([3,4,5])
p
```


```python
p = poly1d([3,4,5])
print("Polinomio")
print(p)
print()
print("Coeficientes")
print(p.coeffs)
print()
print("Raices")
print(p.roots)
print()
print("Integrate")
print(p.integ())
print()
print("Detivative")
print(p.deriv())
```


```python
import numpy as np
from scipy.fft import fft, ifft
x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
y = fft(x)
yinv = ifft(y)

print(x)
print(y)
print(yinv)
```

#### Matplotlib y seaborn

    Matplotlib: Comprehensive 2-D plotting

    Seaborn: Statistical data visualization

##### *Ejemplos*


```python
%matplotlib inline
```


```python
import numpy as np
from scipy.fft import fft
import matplotlib.pyplot as plt

N = 600
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0*2.0*np.pi*x) + 0.5 * np.sin(80.0*2.0*np.pi*x)
yf = fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)

plt.plot(xf, 2.0/N * np.abs(yf[0:N//2]))
plt.grid()
plt.show()
```


```python
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="whitegrid")

# Load the example diamonds dataset
diamonds = sns.load_dataset("diamonds")

# Draw a scatter plot while assigning point colors and sizes to different
# variables in the dataset
f, ax = plt.subplots(figsize=(6.5, 6.5))
sns.despine(f, left=True, bottom=True)
clarity_ranking = ["I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"]
sns.scatterplot(
    x="carat", 
    y="price",
    hue="clarity", 
    size="depth",
    palette="ch:r=-.2,d=.3_r",
    hue_order=clarity_ranking,
    sizes=(1, 8), 
    linewidth=0,
    data=diamonds, 
    ax=ax
)
plt.show()
```

#### Pandas

    Data structures & analysis

##### *Ejemplos*


```python
import pandas as pd

dataframe = pd.read_csv("../datasets/neos.csv")
dataframe
```


```python
dataframe["Perihelion Distance"].min() * 150 * (10 ** 6) / (12.7 * (10 ** 3) )
```

#### Sympy

    Symbolic mathematics

##### *Ejemplos*


```python
from sympy import *

init_printing(use_unicode=True)
```


```python
x = symbols('x')

solveset(3*x**2 + 4*x + 5, x)
```


```python
from sympy import *

f = symbols('f', cls=Function)
x = symbols('x')
```


```python
f(x)
```


```python
f(x).diff(x)
```


```python
diffeq = Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x))
diffeq
```


```python
dsolve(diffeq, f(x))
```

#### Scikit learn

    Machine Learning in Python

##### *Ejemplos*


```python
import numpy as np
import matplotlib.pyplot as plt
# Though the following import is not directly being used, it is required
# for 3D projection to work
from mpl_toolkits.mplot3d import Axes3D

from sklearn.cluster import KMeans
from sklearn import datasets

np.random.seed(5)

iris = datasets.load_iris()
X = iris.data
y = iris.target

estimators = [
    ('k_means_iris_8', KMeans(n_clusters=8)),
    ('k_means_iris_3', KMeans(n_clusters=3)),
    ('k_means_iris_bad_init', KMeans(n_clusters=3, n_init=1, init='random'))
]

fignum = 1
titles = ['8 clusters', '3 clusters', '3 clusters, bad initialization']

for name, est in estimators:
    fig = plt.figure(fignum, figsize=(10, 10))
    ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
    
    est.fit(X)
    labels = est.labels_

    ax.scatter(
        X[:, 3], 
        X[:, 0], 
        X[:, 2],
        c=labels.astype(np.float), 
        edgecolor='k'
    )

    ax.w_xaxis.set_ticklabels([])
    ax.w_yaxis.set_ticklabels([])
    ax.w_zaxis.set_ticklabels([])
    ax.set_xlabel('Petal width')
    ax.set_ylabel('Sepal length')
    ax.set_zlabel('Petal length')
    ax.set_title(titles[fignum - 1])
    ax.dist = 12
    fignum = fignum + 1

# Plot the ground truth
fig = plt.figure(fignum, figsize=(10, 10))
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

for name, label in [('Setosa', 0), ('Versicolour', 1), ('Virginica', 2)]:
    ax.text3D(
        X[y == label, 3].mean(),
        X[y == label, 0].mean(),
        X[y == label, 2].mean() + 2, name,
        horizontalalignment='center',
        bbox=dict(alpha=.2, edgecolor='w', facecolor='w')
    )

# Reorder the labels to have colors matching the cluster results
y = np.choose(y, [1, 2, 0]).astype(np.float)
ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=y, edgecolor='k')

ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
ax.set_title('Ground Truth')
ax.dist = 12

plt.show()
```


```python

```

e6aef97c81ebb00cba575d02c2c706bd97f0b14e
ipynb
Jupyter Notebook
week-2/1-libraries/Libraries.ipynb
PCPUNMSM/Summer-School-2020
49fdf5b40a1f71e5da57836a98eb996d2beb17a1
MIT
2020-02-22T18:30:12.000Z
2021-11-14T19:24:23.000Z
week-2/1-libraries/Libraries.ipynb
PCPUNMSM/Summer-School-2020
49fdf5b40a1f71e5da57836a98eb996d2beb17a1
MIT
week-2/1-libraries/Libraries.ipynb
PCPUNMSM/Summer-School-2020
49fdf5b40a1f71e5da57836a98eb996d2beb17a1
MIT
2020-01-31T22:14:00.000Z
2020-02-08T16:37:08.000Z
Qwen/Qwen-72B
1. YES
2. YES