<jupyter_start><jupyter_text>infofollowing [towards datascience](https://towardsdatascience.com/hands-on-bayesian-statistics-with-python-pymc3-arviz-499db9a59501) articleDoes some Bayesian inference on the "Spanish high speed rail tickets pricing" dataset that can be found [here](https://raw.githubusercontent.com/susanli2016/Machine-Learning-with-Python/master/data/renfe_small.csv)  imports<jupyter_code>from scipy import stats
import arviz as az
import numpy as np
import matplotlib.pyplot as plt
import pymc3 as pm
import seaborn as sns
import pandas as pd
from theano import shared
from sklearn import preprocessing
print(f"Running PyMC3 v{pm.__version__}")
! wget "https://raw.githubusercontent.com/susanli2016/Machine-Learning-with-Python/master/data/renfe_small.csv"
data = pd.read_csv("renfe_small.csv")
data
print("summary shows the fraction of cases per column that have NaNs i.e., missing data:\n")
data.isnull().sum()/len(data)
# fill NA/NaN 'train_class' and 'fare' with mode values
most_common_train_class = data['train_class'].mode().iloc[0]
most_common_fare = data['fare'].mode().iloc[0]

data['train_class'] = data['train_class'].fillna(most_common_train_class)
data['fare'] = data['fare'].fillna(most_common_fare)

# fill NaN 'price' with mean values of 'fare' types
data['price'] = data.groupby('fare').transform(lambda x: x.fillna(x.mean()))
data.groupby('fare').sum()
data['price'].values
plt.figure()
plt.hist(data['price'].values, density=True)
az.plot_kde(data['price'].values, plot_kwargs={'c':'k'}, rug=True)
plt.yticks([]);
plt.xlabel("price")
# this could be a Gaussian distribution with a tail at high prices
# lets assume a Gaussian for our model of the price distribution data
# we do not know the mean or standard deviation so we must set priors for them<jupyter_output><empty_output><jupyter_text>model<jupyter_code># we perform inference on the ticket price data
with pm.Model() as model_g:
    mean = pm.Uniform('mean', lower=0, upper=300)
    std = pm.Uniform('std', lower=1e-3, upper=100)
    y = pm.Normal('y', mu=mean, sd=std, observed=data['price'].values)
with model_g:
    trace_g = pm.sample(1000, tune=1000, cores=2)
with model_g:
    az.plot_trace(trace_g)
trace_g
loc=np.median(trace_g["mean"])
print(loc)
scale=np.median(trace_g["std"])
print(scale)
gauss = stats.norm
x = np.linspace(min(data['price'].values), max(data['price'].values), 1000)
yhat = gauss.pdf(x, loc=loc, scale=scale)
plt.figure()
plt.hist(data['price'].values, density=True)
az.plot_kde(data['price'].values, plot_kwargs={'c':'k'}, rug=True)
plt.plot(x, yhat)
# plt.yticks([]);
plt.xlabel("price")
# plt.yscale('log')
plt.figure()
plt.hist(data['price'].values, density=True)
az.plot_kde(data['price'].values, plot_kwargs={'c':'k'}, rug=True)
plt.plot(x, yhat)
# plt.yticks([]);
plt.xlabel("price")
plt.yscale('log')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15846422338568936, 'fraction_numerical': 0.015357766143106457, 'mean_word_length': 6.6021220159151195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '115828', 'license': 'MIT', 'n_tokens_mistral': 1060, 'n_tokens_neox': 1006, 'n_words': 230, 'path': 'personal-examples/towards-example/inference-example.ipynb', 'repo_name': 'Cyberface/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers', 'size': '2865'}