Hello all,
I am quite new to Python and Bayesian Linear Regression. I could write a code for the Bayesian Linear Regression model (Although I don’t know if it is good or not) to predict a SINGLE “x”. However, my data consists of 3 independent variables and 1 dependent variable and I like to predict my dependent variable based on 3 inputs, not just 1 input as you can see in the following code.
Could you please help me with how to change the code?
I also could not understand how the model trained itself because in the model context (pymc3) there is no sign of using X_train or X_test! That is confusing to me.
Thank you all.
import numpy as np
import pandas as pd
import scipy
import theano
import pymc3 as pm
dataset = pd.read_csv('PV-PCM.csv')
from sklearn.model_selection import train_test_split
X=dataset.iloc[:,:-1].values
y=dataset.iloc[:,3].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state=42)
with pm.Model() as linear_model:
# Intercept
intercept = pm.Normal('Intercept', mu = 0, sd = 10)
# Slope
slope = pm.Normal('slope', mu = 0, sd = 10)
# Standard deviation
sigma = pm.HalfNormal('sigma', sd = 10)
# Estimate of mean
mean = intercept + slope * dataset.iloc[:,0].values
# Observed values
Y_obs = pm.Normal('Y_obs', mu = mean, sd = sigma, observed = y)
# Sampler
step = pm.NUTS()
# Posterior distribution
linear_trace = pm.sample(1000, step)
pm.traceplot(linear_trace, figsize = (12, 12))
pm.plot_posterior(linear_trace, figsize = (12, 10))
bayes_prediction = linear_trace['Intercept'] + linear_trace['slope'] * "Any number for ONE IV"