from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
    ipy.run_line_magic('matplotlib', 'inline')
import numpy as np
import matplotlib.pyplot as plt
# Useful module for dealing with the Gaussian density
from scipy.stats import norm, multivariate_normal
# installing packages for interactive graphs
import ipywidgets as widgets
from IPython.display import display
from ipywidgets import interact, interactive, fixed, interact_manual, IntSlider

data = np.loadtxt('wine.data.txt', delimiter=',')
# Names of features
featurenames = ['Alcohol', 'Malic acid', 'Ash', 'Alcalinity of ash','Magnesium', 'Total phenols',
                'Flavanoids', 'Nonflavanoid phenols', 'Proanthocyanins', 'Color intensity', 'Hue',
                'OD280/OD315 of diluted wines', 'Proline']

np.random.seed(0)
perm = np.random.permutation(178)
trainx = data[perm[0:130],1:14]
trainy = data[perm[0:130],0]
testx = data[perm[130:178], 1:14]
testy = data[perm[130:178],0]

@interact_manual( feature=IntSlider(0,0,12), label=IntSlider(1,1,3))
def density_plot(feature, label):
    plt.hist(trainx[trainy==label,feature], normed=True)
    #
    mu = np.mean(trainx[trainy==label,feature]) # mean
    var = np.var(trainx[trainy==label,feature]) # variance
    std = np.sqrt(var) # standard deviation
    #
    x_axis = np.linspace(mu - 3*std, mu + 3*std, 1000)
    plt.plot(x_axis, norm.pdf(x_axis,mu,std), 'r', lw=2)
    plt.title("Winery "+str(label) )
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.show()

def fit_generative_model(x,y,feature):
    k = 3 # number of classes
    mu = np.zeros(k+1) # list of means
    var = np.zeros(k+1) # list of variances
    pi = np.zeros(k+1) # list of class weights
    for label in range(1,k+1):
        indices = (y==label)
        mu[label] = np.mean(x[indices,feature])
        var[label] = np.var(x[indices,feature])
        pi[label] = float(sum(indices))/float(len(y))
    return mu, var, pi

@interact_manual( feature=IntSlider(0,0,12) )
def show_densities(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    colors = ['r', 'k', 'g']
    for label in range(1,4):
        m = mu[label]
        s = np.sqrt(var[label])
        x_axis = np.linspace(m - 3*s, m+3*s, 1000)
        plt.plot(x_axis, norm.pdf(x_axis,m,s), colors[label-1], label="class " + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.legend()
    plt.show()

@interact( feature=IntSlider(0,0,12) )
def test_model(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)

    k = 3 # Labels 1,2,...,k
    n_test = len(testy) # Number of test points
    score = np.zeros((n_test,k+1))
    for i in range(0,n_test):
        for label in range(1,k+1):
            score[i,label] = np.log(pi[label]) + \
            norm.logpdf(testx[i,feature], mu[label], np.sqrt(var[label]))
    predictions = np.argmax(score[:,1:4], axis=1) + 1
    # Finally, tally up score
    errors = np.sum(predictions != testy)
    print("Test error using feature " + featurenames[feature] + ": " + str(errors) + "/" + str(n_test))

429a8782ec5d65714943f5b16c492ae23bafd459
<mask token>


@interact_manual(feature=IntSlider(0, 0, 12), label=IntSlider(1, 1, 3))
def density_plot(feature, label):
    plt.hist(trainx[trainy == label, feature], normed=True)
    mu = np.mean(trainx[trainy == label, feature])
    var = np.var(trainx[trainy == label, feature])
    std = np.sqrt(var)
    x_axis = np.linspace(mu - 3 * std, mu + 3 * std, 1000)
    plt.plot(x_axis, norm.pdf(x_axis, mu, std), 'r', lw=2)
    plt.title('Winery ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.show()


def fit_generative_model(x, y, feature):
    k = 3
    mu = np.zeros(k + 1)
    var = np.zeros(k + 1)
    pi = np.zeros(k + 1)
    for label in range(1, k + 1):
        indices = y == label
        mu[label] = np.mean(x[indices, feature])
        var[label] = np.var(x[indices, feature])
        pi[label] = float(sum(indices)) / float(len(y))
    return mu, var, pi


@interact_manual(feature=IntSlider(0, 0, 12))
def show_densities(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    colors = ['r', 'k', 'g']
    for label in range(1, 4):
        m = mu[label]
        s = np.sqrt(var[label])
        x_axis = np.linspace(m - 3 * s, m + 3 * s, 1000)
        plt.plot(x_axis, norm.pdf(x_axis, m, s), colors[label - 1], label=
            'class ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.legend()
    plt.show()


<mask token>

<mask token>


@interact_manual(feature=IntSlider(0, 0, 12), label=IntSlider(1, 1, 3))
def density_plot(feature, label):
    plt.hist(trainx[trainy == label, feature], normed=True)
    mu = np.mean(trainx[trainy == label, feature])
    var = np.var(trainx[trainy == label, feature])
    std = np.sqrt(var)
    x_axis = np.linspace(mu - 3 * std, mu + 3 * std, 1000)
    plt.plot(x_axis, norm.pdf(x_axis, mu, std), 'r', lw=2)
    plt.title('Winery ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.show()


def fit_generative_model(x, y, feature):
    k = 3
    mu = np.zeros(k + 1)
    var = np.zeros(k + 1)
    pi = np.zeros(k + 1)
    for label in range(1, k + 1):
        indices = y == label
        mu[label] = np.mean(x[indices, feature])
        var[label] = np.var(x[indices, feature])
        pi[label] = float(sum(indices)) / float(len(y))
    return mu, var, pi


@interact_manual(feature=IntSlider(0, 0, 12))
def show_densities(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    colors = ['r', 'k', 'g']
    for label in range(1, 4):
        m = mu[label]
        s = np.sqrt(var[label])
        x_axis = np.linspace(m - 3 * s, m + 3 * s, 1000)
        plt.plot(x_axis, norm.pdf(x_axis, m, s), colors[label - 1], label=
            'class ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.legend()
    plt.show()


@interact(feature=IntSlider(0, 0, 12))
def test_model(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    k = 3
    n_test = len(testy)
    score = np.zeros((n_test, k + 1))
    for i in range(0, n_test):
        for label in range(1, k + 1):
            score[i, label] = np.log(pi[label]) + norm.logpdf(testx[i,
                feature], mu[label], np.sqrt(var[label]))
    predictions = np.argmax(score[:, 1:4], axis=1) + 1
    errors = np.sum(predictions != testy)
    print('Test error using feature ' + featurenames[feature] + ': ' + str(
        errors) + '/' + str(n_test))

<mask token>
ipy = get_ipython()
if ipy is not None:
    ipy.run_line_magic('matplotlib', 'inline')
<mask token>
data = np.loadtxt('wine.data.txt', delimiter=',')
featurenames = ['Alcohol', 'Malic acid', 'Ash', 'Alcalinity of ash',
    'Magnesium', 'Total phenols', 'Flavanoids', 'Nonflavanoid phenols',
    'Proanthocyanins', 'Color intensity', 'Hue',
    'OD280/OD315 of diluted wines', 'Proline']
np.random.seed(0)
perm = np.random.permutation(178)
trainx = data[perm[0:130], 1:14]
trainy = data[perm[0:130], 0]
testx = data[perm[130:178], 1:14]
testy = data[perm[130:178], 0]


@interact_manual(feature=IntSlider(0, 0, 12), label=IntSlider(1, 1, 3))
def density_plot(feature, label):
    plt.hist(trainx[trainy == label, feature], normed=True)
    mu = np.mean(trainx[trainy == label, feature])
    var = np.var(trainx[trainy == label, feature])
    std = np.sqrt(var)
    x_axis = np.linspace(mu - 3 * std, mu + 3 * std, 1000)
    plt.plot(x_axis, norm.pdf(x_axis, mu, std), 'r', lw=2)
    plt.title('Winery ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.show()


def fit_generative_model(x, y, feature):
    k = 3
    mu = np.zeros(k + 1)
    var = np.zeros(k + 1)
    pi = np.zeros(k + 1)
    for label in range(1, k + 1):
        indices = y == label
        mu[label] = np.mean(x[indices, feature])
        var[label] = np.var(x[indices, feature])
        pi[label] = float(sum(indices)) / float(len(y))
    return mu, var, pi


@interact_manual(feature=IntSlider(0, 0, 12))
def show_densities(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    colors = ['r', 'k', 'g']
    for label in range(1, 4):
        m = mu[label]
        s = np.sqrt(var[label])
        x_axis = np.linspace(m - 3 * s, m + 3 * s, 1000)
        plt.plot(x_axis, norm.pdf(x_axis, m, s), colors[label - 1], label=
            'class ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.legend()
    plt.show()


@interact(feature=IntSlider(0, 0, 12))
def test_model(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    k = 3
    n_test = len(testy)
    score = np.zeros((n_test, k + 1))
    for i in range(0, n_test):
        for label in range(1, k + 1):
            score[i, label] = np.log(pi[label]) + norm.logpdf(testx[i,
                feature], mu[label], np.sqrt(var[label]))
    predictions = np.argmax(score[:, 1:4], axis=1) + 1
    errors = np.sum(predictions != testy)
    print('Test error using feature ' + featurenames[feature] + ': ' + str(
        errors) + '/' + str(n_test))

from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
    ipy.run_line_magic('matplotlib', 'inline')
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm, multivariate_normal
import ipywidgets as widgets
from IPython.display import display
from ipywidgets import interact, interactive, fixed, interact_manual, IntSlider
data = np.loadtxt('wine.data.txt', delimiter=',')
featurenames = ['Alcohol', 'Malic acid', 'Ash', 'Alcalinity of ash',
    'Magnesium', 'Total phenols', 'Flavanoids', 'Nonflavanoid phenols',
    'Proanthocyanins', 'Color intensity', 'Hue',
    'OD280/OD315 of diluted wines', 'Proline']
np.random.seed(0)
perm = np.random.permutation(178)
trainx = data[perm[0:130], 1:14]
trainy = data[perm[0:130], 0]
testx = data[perm[130:178], 1:14]
testy = data[perm[130:178], 0]


@interact_manual(feature=IntSlider(0, 0, 12), label=IntSlider(1, 1, 3))
def density_plot(feature, label):
    plt.hist(trainx[trainy == label, feature], normed=True)
    mu = np.mean(trainx[trainy == label, feature])
    var = np.var(trainx[trainy == label, feature])
    std = np.sqrt(var)
    x_axis = np.linspace(mu - 3 * std, mu + 3 * std, 1000)
    plt.plot(x_axis, norm.pdf(x_axis, mu, std), 'r', lw=2)
    plt.title('Winery ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.show()


def fit_generative_model(x, y, feature):
    k = 3
    mu = np.zeros(k + 1)
    var = np.zeros(k + 1)
    pi = np.zeros(k + 1)
    for label in range(1, k + 1):
        indices = y == label
        mu[label] = np.mean(x[indices, feature])
        var[label] = np.var(x[indices, feature])
        pi[label] = float(sum(indices)) / float(len(y))
    return mu, var, pi


@interact_manual(feature=IntSlider(0, 0, 12))
def show_densities(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    colors = ['r', 'k', 'g']
    for label in range(1, 4):
        m = mu[label]
        s = np.sqrt(var[label])
        x_axis = np.linspace(m - 3 * s, m + 3 * s, 1000)
        plt.plot(x_axis, norm.pdf(x_axis, m, s), colors[label - 1], label=
            'class ' + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.legend()
    plt.show()


@interact(feature=IntSlider(0, 0, 12))
def test_model(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    k = 3
    n_test = len(testy)
    score = np.zeros((n_test, k + 1))
    for i in range(0, n_test):
        for label in range(1, k + 1):
            score[i, label] = np.log(pi[label]) + norm.logpdf(testx[i,
                feature], mu[label], np.sqrt(var[label]))
    predictions = np.argmax(score[:, 1:4], axis=1) + 1
    errors = np.sum(predictions != testy)
    print('Test error using feature ' + featurenames[feature] + ': ' + str(
        errors) + '/' + str(n_test))

from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
    ipy.run_line_magic('matplotlib', 'inline')
import numpy as np
import matplotlib.pyplot as plt
# Useful module for dealing with the Gaussian density
from scipy.stats import norm, multivariate_normal
# installing packages for interactive graphs
import ipywidgets as widgets
from IPython.display import display
from ipywidgets import interact, interactive, fixed, interact_manual, IntSlider

data = np.loadtxt('wine.data.txt', delimiter=',')
# Names of features
featurenames = ['Alcohol', 'Malic acid', 'Ash', 'Alcalinity of ash','Magnesium', 'Total phenols',
                'Flavanoids', 'Nonflavanoid phenols', 'Proanthocyanins', 'Color intensity', 'Hue',
                'OD280/OD315 of diluted wines', 'Proline']

np.random.seed(0)
perm = np.random.permutation(178)
trainx = data[perm[0:130],1:14]
trainy = data[perm[0:130],0]
testx = data[perm[130:178], 1:14]
testy = data[perm[130:178],0]

@interact_manual( feature=IntSlider(0,0,12), label=IntSlider(1,1,3))
def density_plot(feature, label):
    plt.hist(trainx[trainy==label,feature], normed=True)
    #
    mu = np.mean(trainx[trainy==label,feature]) # mean
    var = np.var(trainx[trainy==label,feature]) # variance
    std = np.sqrt(var) # standard deviation
    #
    x_axis = np.linspace(mu - 3*std, mu + 3*std, 1000)
    plt.plot(x_axis, norm.pdf(x_axis,mu,std), 'r', lw=2)
    plt.title("Winery "+str(label) )
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.show()

def fit_generative_model(x,y,feature):
    k = 3 # number of classes
    mu = np.zeros(k+1) # list of means
    var = np.zeros(k+1) # list of variances
    pi = np.zeros(k+1) # list of class weights
    for label in range(1,k+1):
        indices = (y==label)
        mu[label] = np.mean(x[indices,feature])
        var[label] = np.var(x[indices,feature])
        pi[label] = float(sum(indices))/float(len(y))
    return mu, var, pi

@interact_manual( feature=IntSlider(0,0,12) )
def show_densities(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)
    colors = ['r', 'k', 'g']
    for label in range(1,4):
        m = mu[label]
        s = np.sqrt(var[label])
        x_axis = np.linspace(m - 3*s, m+3*s, 1000)
        plt.plot(x_axis, norm.pdf(x_axis,m,s), colors[label-1], label="class " + str(label))
    plt.xlabel(featurenames[feature], fontsize=14, color='red')
    plt.ylabel('Density', fontsize=14, color='red')
    plt.legend()
    plt.show()

@interact( feature=IntSlider(0,0,12) )
def test_model(feature):
    mu, var, pi = fit_generative_model(trainx, trainy, feature)

    k = 3 # Labels 1,2,...,k
    n_test = len(testy) # Number of test points
    score = np.zeros((n_test,k+1))
    for i in range(0,n_test):
        for label in range(1,k+1):
            score[i,label] = np.log(pi[label]) + \
            norm.logpdf(testx[i,feature], mu[label], np.sqrt(var[label]))
    predictions = np.argmax(score[:,1:4], axis=1) + 1
    # Finally, tally up score
    errors = np.sum(predictions != testy)
    print("Test error using feature " + featurenames[feature] + ": " + str(errors) + "/" + str(n_test))
