Ejecución Paso A Paso
Ejecución Paso A Paso
Ejecución Paso A Paso
In [4]:
#===========================================================
#DESARROLLADOR : Ing.Ramirez Quispe, Robert Marlindo. #
#DOCENTE : Dr. Walter Obando Licera #
#PROGRAMA : Redes Neuronales Artificiales #
# en la Hidrología. #
#FECHA : 05/09/2020 DD/MM/AA #
#LUGAR : UNI Lima - Perú #
# Maestría en Ingeneiría Hidráulica #
#BLOGGER : https://ramirezquispe1.blogspot.com/ #
#===========================================================#
In [5]:
# Importar librerias
import scipy.stats as stats # librería estadística
import numpy as np # librería numérica
import pandas as pd # tablas
import matplotlib.pyplot as plt # gráficos
import seaborn as sns # gráficos más elaborados
import itertools # objetos de iteración
In [6]:
Q_simulado Q_observado
0 22.243 25.360
1 22.214 23.394
2 22.263 23.781
3 22.440 23.716
4 22.842 24.367
In [7]:
data_caudales.shape
Out[7]:
(1217, 2)
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 1/12
5/9/2020 rn_caudales - Jupyter Notebook
In [9]:
# 1. Resumen estadístico
plt.figure(figsize=(10,2)) #tamaño de la ventana
sns.heatmap(round(data_caudales.describe()[1:].transpose(),2), linewidth=2, annot=True, fmt
plt.xticks(fontsize=20)
plt.yticks(fontsize=12)
plt.title("Resumen de variables estadísticas")
plt.show()
In [10]:
In [12]:
In [13]:
data_caudales.head()
Out[13]:
Q_simulado Q_observado
0 22.243 25.360
1 22.214 23.394
2 22.263 23.781
3 22.440 23.716
4 22.842 24.367
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 2/12
5/9/2020 rn_caudales - Jupyter Notebook
In [14]:
# Gráfico de 2 variables
import matplotlib.pyplot as plt
plt.figure(figsize=(12,6))
plt.plot(data_caudales.Q_simulado, marker='x', linestyle=':', color='b', label = "Q_simulad
plt.plot(data_caudales.Q_observado, marker='*', linestyle='-', color='g', label = "Q_observ
plt.grid(True) # Activa cuadrícula del gráfico pero no se muestra
plt.legend()
Out[14]:
<matplotlib.legend.Legend at 0x1df06dec190>
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 3/12
5/9/2020 rn_caudales - Jupyter Notebook
In [15]:
In [16]:
train_X.head()
Out[16]:
Q_simulado
169 29.031
801 22.000
1008 8.744
1066 14.303
274 24.586
In [17]:
train_Y.head()
Out[17]:
169 24.629
801 24.170
1008 9.006
1066 14.008
274 32.606
Name: Q_observado, dtype: float64
In [18]:
train_X.shape
Out[18]:
(851, 1)
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 4/12
5/9/2020 rn_caudales - Jupyter Notebook
In [12]:
test_X.head()
Out[12]:
Q_simulado
542 15.874
569 19.643
891 25.248
920 30.599
883 42.823
In [20]:
print (algorithm)
print ("===============")
algorithm.fit(dtrainx,dtrainy)
prediction = algorithm.predict(dtestx)
print ("RMSE :", np.sqrt(mean_squared_error(dtesty, prediction)) )
prediction = pd.DataFrame(prediction)
cross_val = cross_val_score(algorithm,dtrainx,dtrainy,cv=20,scoring="neg_mean_squared_e
cross_val = cross_val.ravel()
print ("Validación cruzada")
print ("===============")
print ("cv-mean :",cross_val.mean())
print ("cv-std :",cross_val.std())
print ("cv-max :",cross_val.max())
print ("cv-min :",cross_val.min())
plt.figure(figsize=(10,35))
plt.subplot(211)
testy = dtesty.reset_index()[target]
plt.legend(loc="best")
plt.title("Caudal Observado vs Caudal RNA ")
plt.xlabel("Indice")
plt.ylabel("Valores")
plt.grid(True) # Activa cuadrícula del gráfico pero no se muestra
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 5/12
5/9/2020 rn_caudales - Jupyter Notebook
In [22]:
# Regresión Lineal
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
model(lr,train_X,train_Y,test_X,test_Y,"coef")
LinearRegression()
===============
RMSE : 4.546930060654723
Validación cruzada
===============
cv-mean : -20.832059607668857
cv-std : 8.328806583173176
cv-max : -7.685452152252439
cv-min : -39.97126907544244
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 6/12
5/9/2020 rn_caudales - Jupyter Notebook
In [23]:
# Regresión de Lasso
from sklearn.linear_model import Ridge,Lasso
ls = Lasso()
model(ls,train_X,train_Y,test_X,test_Y,"coef")
Lasso()
===============
RMSE : 4.554992299413336
Validación cruzada
===============
cv-mean : -20.83846304999165
cv-std : 8.434246103606126
cv-max : -7.703670180417129
cv-min : -40.40778511526412
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 7/12
5/9/2020 rn_caudales - Jupyter Notebook
In [24]:
# Regresión de Ridge
rigde = Ridge()
model(rigde,train_X,train_Y,test_X,test_Y,"coef")
Ridge()
===============
RMSE : 4.5469383538694155
Validación cruzada
===============
cv-mean : -20.83205750902434
cv-std : 8.328931074527857
cv-max : -7.685468099957004
cv-min : -39.971781602554316
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 8/12
5/9/2020 rn_caudales - Jupyter Notebook
In [25]:
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 9/12
5/9/2020 rn_caudales - Jupyter Notebook
In [26]:
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 10/12
5/9/2020 rn_caudales - Jupyter Notebook
In [27]:
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 11/12
5/9/2020 rn_caudales - Jupyter Notebook
In [28]:
localhost:8888/notebooks/python_uni_/_pdise_havanzada/rn_caudales.ipynb# 12/12