Skip to content

Instantly share code, notes, and snippets.

View naenumtou's full-sized avatar
🏠
Working from home

Sasiwut Chaiyadecha naenumtou

🏠
Working from home
View GitHub Profile
# Exact generator
exactMatrix = ExactMatrix(exactGenerator)
# Show matrix
plotMatrix(exactMatrix, 'Exact transition matrix', 'Percent')
# Define exact transition matrix
def ExactMatrix(exactQ):
# Define identity matrix
I = pd.DataFrame(
np.identity(exactQ.shape[0]),
index = exactQ.index,
columns = exactQ.columns
)
# Exact transition matrix
exactMatrix = None
# Exact generator
exactGenerator = ExactGenerator(generatorMatrix)
# Show matrix
plotMatrix(exactGenerator, 'Exact generator')
# Define exact generator function
def ExactGenerator(Q):
# Define Gi and Bi
# Gi
GiMatrix = Q.copy()
for i in range(Q.shape[1]):
for j in range(Q.shape[0]):
if i == j: #Diagonal matrix
GiMatrix.iloc[i, j] = abs(GiMatrix.iloc[i, j])
else: #Non-Diagonal matrix
# Generator matrix
generatorMatrix = GeneratorMatrix(matrix)
# Show matrix
plotMatrix(generatorMatrix, 'Generator matrix')
# Define generator matrix function
def GeneratorMatrix(P):
# Define identity matrix
I = pd.DataFrame(
np.identity(P.shape[0]),
index = P.index,
columns = P.columns
)
# Generator matrix
generatedMatrix = None
# Set auto reload
%reload_ext autoreload
%autoreload 2
# Import libraries
import warnings
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Export
pd.concat(
[
pd.DataFrame(X, columns = ['X1']),
pd.DataFrame(y, columns = ['y'])
],
axis = 1
).to_csv(
'Theil-Sen_Data.csv',
index = 0
# Compare
plt.figure(figsize = (10, 6))
plt.scatter(
X,
y,
c = 'teal'
)
plt.plot(
X,
OLS.predict(X),
# Theil-Sen
TS = TheilSenRegressor()
TS.fit(X, y.ravel())
# Plot
plt.figure(figsize = (10, 6))
plt.scatter(
X,
y,
c = 'teal'