Skip to content

Instantly share code, notes, and snippets.

View Muhammad-Yunus's full-sized avatar
:octocat:
Working from home

Muhammad Yunus Muhammad-Yunus

:octocat:
Working from home
View GitHub Profile
@Muhammad-Yunus
Muhammad-Yunus / Init.py
Last active April 10, 2020 06:50
Single Layer Perceptron - logic AND Problem
import numpy as np
import matplotlib.pyplot as plt
NUM_FEATURES = 2
NUM_ITER = 100
learning_rate = 0.1
x = np.array([[0,0],[0,1],[1,0],[1,1]], np.float32)
y = np.array([0, 0, 0, 1], np.float32)
@Muhammad-Yunus
Muhammad-Yunus / train_slp.py
Created April 10, 2020 06:51
Single Layer Perceptron - logic AND (train)
for i in range(NUM_ITER):
y_pred = np.dot(x, W) + b
#apply activation
y_pred[y_pred > 0] = 1
y_pred[y_pred <= 0] = 0
#calculate error
err = y - y_pred
@Muhammad-Yunus
Muhammad-Yunus / test_slp.py
Created April 10, 2020 07:19
Single Layer Perceptron - logic AND (test)
#test
x_test = [[0,0],[0,1],[1,0],[1,1]]
for x_test_item in x_test :
y_test = np.dot(x_test_item, W) + b
y_test = 1 if y_test > 0 else 0
print(str(x_test_item[0]) + ' AND ' + str(x_test_item[1]) + ' = ' + str(y_test))
@Muhammad-Yunus
Muhammad-Yunus / plot_slp.py
Created April 10, 2020 07:41
Single Laer Perceptron - Logic AND (plot lineary sparable class)
# plot lineary separable class (logic AND)
plot_x = np.array([np.min(x[:, 0] - 0.2), np.max(x[:, 1]+0.2)])
plot_y = - 1 / W[1] * (W[0] * plot_x + b)
print('W:' + str(W))
print('b:' + str(b))
print('plot_y: '+ str(plot_y))
plt.scatter(x[:, 0], x[:, 1], c=y, s=100, cmap='viridis')
for i in range(NUM_ITER):
y_pred = np.dot(x, W) + b
#activation sigmoid
y_pred = 1.0 / (1.0 + np.exp(-y_pred))
err = y - y_pred
delta_W = learning_rate * np.dot(np.transpose(x) , err)
@Muhammad-Yunus
Muhammad-Yunus / load_dataset_mlp.py
Last active April 19, 2020 08:09
Multi Layer Perceptron Backpropagation
import pandas as pd
import numpy as np
Month_list = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember']
CO_TS = []
CO_TS_LIST = []
for Month in Month_list :
print("Read ISPU-di-Provinsi-DKI-Jakarta-Bulan-" + Month + ".csv")
CSV_CO_TS = pd.read_csv("ISPU-di-Provinsi-DKI-Jakarta-Bulan-" + Month + ".csv",
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=len(Month_list), ncols=1)
fig.subplots_adjust(hspace=0.5)
CO_TS = CO_TS[['tanggal', 'co']]
CO_TS.index = pd.to_datetime(CO_TS.tanggal)
CO_TS.drop(["tanggal"], axis=1, inplace=True)
@Muhammad-Yunus
Muhammad-Yunus / agg_stat_mlp.py
Last active April 12, 2020 09:02
Multi Layer Perceptron Backpropagation
DALIY_CO = CO_TS["CO_rolling_mean"] \
.groupby(CO_TS.index.day) \
.agg(['min', 'max', 'mean'])
DALIY_CO.index.name = "Day"
ax = DALIY_CO.plot(title='Daily CO Aggregate')
ax.legend(loc="lower right")
ax.set_ylabel('ug/m3')
plt.fill_between(x=DALIY_CO.index,
@Muhammad-Yunus
Muhammad-Yunus / ODE_DHO_init.py
Created April 15, 2020 00:42
Python Scikit - Ordinary Differential Equation
from scipy.integrate import odeint, ode
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(suppress=True, precision=10)
@Muhammad-Yunus
Muhammad-Yunus / ODE_DHO_RHS.py
Created April 15, 2020 00:43
Python scikit - Ordinary Differential Equation
def dy(y, t, zeta, w0):
x, p = y[0], y[1]
dx = p
dp = -2 * zeta * w0 * p - w0**2 * x
return [dx, dp]