Máquinas de Aprendizaje
Please wait while flipbook is loading. For more related info, FAQs and issues please refer to DearFlip WordPress Flipbook Plugin Help documentation.
In [1]:
import numpy as np
import matplotlib.pyplot as plt
import random
import seaborn as sns
import pandas as pd
In [2]:
df = pd.read_csv('Life_Expectancy_Data.csv')
df = df.dropna()
df.head()
# sns.pairplot(df)
y = df.iloc[:,3].to_numpy()
X = df.loc[:,"Adult Mortality"].to_numpy()
# # df.head()
In [3]:
fig, ax = plt.subplots(1, figsize=(8,4))
ax.scatter(X, y, s=5, alpha=0.7)
ax.tick_params(labelsize=8)
plt.xlabel('residual sugar')
plt.ylabel('density')
plt.tight_layout()
In [4]:
theta=np.array((random.randrange(-10, 10),
random.randrange(-10, 10)))
theta=np.reshape(theta,(len(theta),1))
lr = 1e-1
epochs=5000
m=len(X)
m=np.mean(X)
std=np.std(X)
X=(X-m)/std
X_b=np.c_[np.ones((len(X),1)),X]
beta1=0.99
beta2=0.9
y=np.reshape(y,(len(y),1))
j_cost= np.zeros([epochs])
### inicialización v
pred = np.dot(X_b,theta)
h=(pred - y)
v=(X_b.T.dot(h))
s=v*1
for i in range(epochs):
pred= np.dot(X_b,theta)
arg=pred - y
grad= X_b.T.dot(arg)
###RMSprops
s=beta1*s+(1-beta1)*(grad**2)+1e-20
sqi=1/(np.sqrt(s))
#Momentum
v=beta2*v+(1-beta2)*grad
#Estimation theta
theta = theta -lr*sqi*v
j_cost[i]=np.sum(np.square(arg))
fig = plt.figure()
plt.plot(j_cost)
plt.show()
print(theta)
print( j_cost[-1])
[[69.3012567] [-6.1769697]] 64588.67189326878
In [5]:
X_grid = np.arange(min(X),max(X),0.01)
X_grid = X_grid.reshape(len(X_grid),1)
Xt=X_grid
X_grid =np.c_[np.ones((len(X_grid ),1)),X_grid]
print(X_grid.shape)
pred= np.dot(X_grid,theta)
plt.figure(figsize=(10,6))
plt.plot(X, y, 'g+', label = 'original data')
plt.plot(Xt, pred, 'b', label = 'prediction')
plt.title('Original data vs prediction')
plt.legend(loc="lower right")
plt.show()
(577, 2)
[[69.3012567] [-6.1769697]]
In [ ]:
Please wait while flipbook is loading. For more related info, FAQs and issues please refer to DearFlip WordPress Flipbook Plugin Help documentation.