3
我試圖實施邏輯多項式迴歸(AKA softmax迴歸)。在此示例中,我嘗試對虹膜數據集進行分類Softmax迴歸(多項Logistic)與PyMC3
我在指定模型時遇到問題,我收到find_MAP()
的優化錯誤。如果我避免使用find_MAP()
,如果我使用Categorical
表示可能性,或者如果我使用Mutinomial(n=1, p=p)
,則與後者完全相同,則得到所有零向量的「樣本」。
import pymc3 as pm
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
iris = sns.load_dataset("iris")
y_2 = pd.Categorical(iris['species']).labels
x_n = iris.columns[:-1]
x_2 = iris[x_n].values
x_2 = (x_2 - x_2.mean(axis=0))/x_2.std(axis=0)
indice = list(set(y_2))
with pm.Model() as modelo_s:
alfa = pm.Normal('alfa', mu=0, sd=100, shape=3)
beta = pm.Normal('beta', mu=0, sd=100, shape=(4,3))
mu = (alfa[indice] + pm.dot(x_2, beta[:,indice])).T
p = pm.exp(mu)/pm.sum(pm.exp(mu), axis=0)
yl = pm.Categorical('yl', p=p, observed=y_2)
#yl = pm.Multinomial('yl', n=1, p=p, observed=y_2)
start = pm.find_MAP()
step = pm.Metropolis()
trace_s = pm.sample(1000, step, start)