Skip to main content

LbML

1 

import pandas as pd

import numpy as np

data = pd.read_csv('lab1.csv')

concepts = np.array(data)[:,:-1]

target = np.array(data)[:,-1]

def train(con,tar):

    for i,val in enumerate(tar):

        if val=='yes':

            specific_h = con[i].copy()

            break

            

    for i,val in enumerate(con):

        if tar[i]=='yes':

            for x in range(len(specific_h)):

                if val[x] != specific_h[x]:

                    specific_h[x] = '?'

                else:

                    pass

    return specific_h

print(train(concepts,target))


2

import numpy as np

import pandas as pd

data = pd.DataFrame(data=pd.read_csv('trainingexamples.csv'))

print('The Dataset is: \n')

print(data)

concepts = np.array(data.iloc[:,0:-1])

print('\n The Concepts are: \n',concepts)

target = np.array(data.iloc[:,-1])

print('\nThe target is: \n',target)


def learn(concepts, target):

    specific_h = concepts[0].copy()

    print(len(specific_h))

    general_h = [["?" for i in range(len(specific_h))] for i in range(len(specific_h))]

    print(general_h)

    for i, h in enumerate(concepts):

        if target[i] == "Yes":

            for x in range(len(specific_h)):

                if h[x] != specific_h[x]:

                    specific_h[x] = '?'

                    general_h[x][x] = '?'


        if target[i] == "No":

            for x in range(len(specific_h)):

                if h[x] != specific_h[x]:

                    general_h[x][x] = specific_h[x]

                else:

                    general_h[x][x] = '?'

    indices = [i for i,val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]

    for i in indices:

        general_h.remove(['?', '?', '?', '?', '?', '?'])

    return specific_h, general_h


s_final, g_final = learn(concepts, target)

print("\n\nFinal S:", s_final)

print("\n\nFinal G:", g_final)


3

import pandas as pd

import numpy as np

import math



class Node:


    def __init__(self, l):

        self.label = l

        self.branch = {}



def entropy(data):


    total_ex = len(data)

    p_ex = len(data.loc[data['PlayTennis'] == 'Yes'])

    n_ex = len(data.loc[data['PlayTennis'] == 'No'])

    en = 0

    if(p_ex > 0):

        en = -(p_ex/float(total_ex)) * \

            (math.log(p_ex, 2)-math.log(total_ex, 2))

    if(n_ex > 0):

        en += -(n_ex/float(total_ex)) * \

            (math.log(n_ex, 2)-math.log(total_ex, 2))


    return en



def gain(en_s, data_s, attrib):


    values = set(data_s[attrib])

    # print(values)

    gain = en_s

    for value in values:

        gain -= len(data_s.loc[data_s[attrib] == value]) / \

            float(len(data_s)) * entropy(data_s.loc[data_s[attrib] == value])


    return gain



def get_attr(data):


    en_s = entropy(data)

    attribute = ""

    max_gain = 0


    for attr in data.columns[:len(data.columns)-1]:

        g = gain(en_s, data, attr)

        if g > max_gain:

            max_gain = g

            attribute = attr


    return attribute



def decision_tree(data):

    root = Node("NULL")

    if(entropy(data) == 0):

        if(len(data.loc[data[data.columns[-1]] == "Yes"]) == len(data)):

            root.label = "Yes"

        else:

            root.label = "No"

        return root


    if(len(data.columns) == 1):

        return

    else:

        attr = get_attr(data)

        root.label = attr


        values = set(data[attr])

        # print(values)


        for value in values:

            root.branch[value] = decision_tree(

                data.loc[data[attr] == value].drop(attr, axis=1))


        return root



def get_rules(root, rule, rules):


    if not root.branch:

        rules.append(rule[:-1]+"=>"+root.label)

        return rules

    for val in root.branch:

        # print(val)

        get_rules(root.branch[val], rule+root.label+"="+str(val)+"^", rules)


    return rules



def test(tree, test_str):

    if not tree.branch:

        return tree.label

    print(str(test_str[tree.label]), tree.label, test_str)

    # if str(test_str[tree.label])=="True" or str(test_str[tree.label])=="False":

    # return tree.branch(str(test_str[tree.label]))

    return test(tree.branch[str(test_str[tree.label])], test_str)



data = pd.read_csv("tennis.csv")


tree = decision_tree(data)

rules = get_rules(tree, " ", [])


for rule in rules:

    print(rule)

# print(rules)


test_str = {}

print("Enter the test case input: ")


for attr in data.columns[:-1]:

    test_str[attr] = input(attr+": ")

print(test_str)


print(test(tree, test_str))


4

from math import exp

from random import seed

from random import random



def initialize_network(n_inputs, n_hidden, n_outputs):

    network = list()

    hidden_layer = [{'weights': [random() for i in range(n_inputs + 1)]}

                    for i in range(n_hidden)]

    network.append(hidden_layer)

    output_layer = [{'weights': [random() for i in range(n_hidden + 1)]}

                    for i in range(n_outputs)]

    network.append(output_layer)

    return network



def activate(weights, inputs):

    activation = weights[-1]

    for i in range(len(weights)-1):

        activation += weights[i] * inputs[i]

    return activation



def transfer(activation):

    return 1.0 / (1.0 + exp(-activation))



def forward_propagate(network, row):

    inputs = row

    for layer in network:

        new_inputs = []

        for neuron in layer:

            activation = activate(neuron['weights'], inputs)

            neuron['output'] = transfer(activation)

            new_inputs.append(neuron['output'])

        inputs = new_inputs

    return inputs



def transfer_derivative(output):

    return output * (1.0 - output)



def backward_propagate_error(network, expected):

    for i in reversed(range(len(network))):

        layer = network[i]

        errors = list()

        if i != len(network)-1:

            for j in range(len(layer)):

                error = 0.0

                for neuron in network[i + 1]:

                    error += (neuron['weights'][j] * neuron['delta'])

                errors.append(error)

        else:

            for j in range(len(layer)):

                neuron = layer[j]

                errors.append(expected[j] - neuron['output'])

        for j in range(len(layer)):

            neuron = layer[j]

            neuron['delta'] = errors[j] * transfer_derivative(neuron['output'])



def update_weights(network, row, l_rate):

    for i in range(len(network)):

        inputs = row[:-1]

        if i != 0:

            inputs = [neuron['output'] for neuron in network[i - 1]]

        for neuron in network[i]:

            for j in range(len(inputs)):

                neuron['weights'][j] += l_rate * neuron['delta'] * inputs[j]

            neuron['weights'][-1] += l_rate * neuron['delta']



def train_network(network, train, l_rate, n_epoch, n_outputs):

    for epoch in range(n_epoch):

        sum_error = 0

        for row in train:

            outputs = forward_propagate(network, row)

            expected = [0 for i in range(n_outputs)]

            expected[row[-1]] = 1

            sum_error += sum([(expected[i]-outputs[i]) **

                             2 for i in range(len(expected))])

            backward_propagate_error(network, expected)

            update_weights(network, row, l_rate)

        print('>epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error))



seed(1)

dataset = [[2.7810836, 2.550537003, 0],

           [1.465489372, 2.362125076, 0],

           [3.396561688, 4.400293529, 0],

           [1.38807019, 1.850220317, 0],

           [3.06407232, 3.005305973, 0],

           [7.627531214, 2.759262235, 1],

           [5.332441248, 2.088626775, 1],

           [6.922596716, 1.77106367, 1],

           [8.675418651, -0.242068655, 1],

           [7.673756466, 3.508563011, 1]]

n_inputs = len(dataset[0]) - 1

n_outputs = len(set([row[-1] for row in dataset]))

network = initialize_network(n_inputs, 2, n_outputs)

train_network(network, dataset, 0.5, 20, n_outputs)

for layer in network:

    print(layer)


5

import pandas as pd


data = pd.DataFrame(data=pd.read_csv('ConceptLearning.csv'))


te = len(data)

print(te)


np = len(data.loc[data[data.columns[-1]] == 'Yes'])

nn = te-np


training = data.sample(frac=0.75, replace=False)

test = pd.concat([data, training, training]).drop_duplicates(keep=False)

print('Training Set : \n', training)

print('\nTest Data Set : \n', test)


prob = {}

for col in training.columns[:-1]:

    prob[col] = {}

    vals = set(data[col])


    for val in vals:

        temp = training.loc[training[col] == val]


        pe = len(temp.loc[temp[temp.columns[-1]] == 'Yes'])

        ne = len(temp)-pe


        prob[col][val] = [pe/np, ne/nn]


prediction = []

right_prediction = 0

for i in range(len(test)):

    row = test.iloc[i, :]

    fpp = np/te

    fpn = nn/te

    for col in test.columns[:-1]:

        fpp *= prob[col][row[col]][0]

        fpn *= prob[col][row[col]][1]


    if fpp > fpn:

        prediction.append('Yes')

    else:

        prediction.append('No')

    if prediction[-1] == row[-1]:

        right_prediction += 1


print('\nActual Values : ', list(test[test.columns[-1]]))

print('Predicted : ', prediction)

print('Accuracy : ', right_prediction/len(test))


6

from sklearn import metrics

from sklearn.metrics import accuracy_score

from sklearn.naive_bayes import MultinomialNB

from sklearn.feature_extraction.text import TfidfTransformer

from sklearn.feature_extraction.text import CountVectorizer

from sklearn.datasets import fetch_20newsgroups

from sklearn.metrics import confusion_matrix

from sklearn.metrics import classification_report

import numpy as np


categories = ['alt.atheism', 'soc.religion.christian',

              'comp.graphics', 'sci.med']

twenty_train = fetch_20newsgroups(

    subset='train', categories=categories, shuffle=True)

twenty_test = fetch_20newsgroups(

    subset='test', categories=categories, shuffle=True)


print(len(twenty_train.data))

print(len(twenty_test.data))

print(twenty_train.target_names)

print("\n".join(twenty_train.data[0].split("\n")))

print("****", twenty_train.target[0])

count_vect = CountVectorizer()

X_train_tf = count_vect.fit_transform(twenty_train.data)

tfidf_transformer = TfidfTransformer()

X_train_tfidf = tfidf_transformer.fit_transform(X_train_tf)


mod = MultinomialNB()

mod.fit(X_train_tfidf, twenty_train.target)

X_test_tf = count_vect.transform(twenty_test.data)

X_test_tfidf = tfidf_transformer.transform(X_test_tf)

print(X_test_tfidf.shape)

predicted = mod.predict(X_test_tfidf)

print(predicted)

print("Accuracy:", accuracy_score(twenty_test.target, predicted))

print(classification_report(twenty_test.target,

      predicted, target_names=twenty_test.target_names))


print("confusion matrix is \n", metrics.confusion_matrix(

    twenty_test.target, predicted))



7

import bayespy as bp

import numpy as np

import csv

from colorama import init

from colorama import Fore, Back, Style

init()



ageEnum = {'SuperSeniorCitizen': 0, 'SeniorCitizen': 1,

           'MiddleAged': 2, 'Youth': 3, 'Teen': 4}


genderEnum = {'Male': 0, 'Female': 1}


familyHistoryEnum = {'Yes': 0, 'No': 1}


dietEnum = {'High': 0, 'Medium': 1, 'Low': 2}


lifeStyleEnum = {'Athlete': 0, 'Active': 1, 'Moderate': 2, 'Sedetary': 3}


cholesterolEnum = {'High': 0, 'BorderLine': 1, 'Normal': 2}


heartDiseaseEnum = {'Yes': 0, 'No': 1}


with open('heart_disease_data.csv') as csvfile:

    lines = csv.reader(csvfile)

    dataset = list(lines)

    data = []

    for x in dataset:

        data.append([ageEnum[x[0]], genderEnum[x[1]], familyHistoryEnum[x[2]], dietEnum[x[3]],

                    lifeStyleEnum[x[4]], cholesterolEnum[x[5]], heartDiseaseEnum[x[6]]])


data = np.array(data)

N = len(data)



p_age = bp.nodes.Dirichlet(1.0*np.ones(5))

age = bp.nodes.Categorical(p_age, plates=(N,))

age.observe(data[:, 0])


p_gender = bp.nodes.Dirichlet(1.0*np.ones(2))

gender = bp.nodes.Categorical(p_gender, plates=(N,))

gender.observe(data[:, 1])


p_familyhistory = bp.nodes.Dirichlet(1.0*np.ones(2))

familyhistory = bp.nodes.Categorical(p_familyhistory, plates=(N,))

familyhistory.observe(data[:, 2])


p_diet = bp.nodes.Dirichlet(1.0*np.ones(3))

diet = bp.nodes.Categorical(p_diet, plates=(N,))

diet.observe(data[:, 3])


p_lifestyle = bp.nodes.Dirichlet(1.0*np.ones(4))

lifestyle = bp.nodes.Categorical(p_lifestyle, plates=(N,))

lifestyle.observe(data[:, 4])


p_cholesterol = bp.nodes.Binomial(1.0*np.ones(3))

cholesterol = bp.nodes.Categorical(p_cholesterol, plates=(N,))

cholesterol.observe(data[:, 5])



p_heartdisease = bp.nodes.Dirichlet(np.ones(2), plates=(5, 2, 2, 3, 4, 3))

heartdisease = bp.nodes.MultiMixture(

    [age, gender, familyhistory, diet, lifestyle, cholesterol], bp.nodes.Categorical, p_heartdisease)

heartdisease.observe(data[:, 6])

p_heartdisease.update()



m = 0

while m == 0:

    print("\n")

    res = bp.nodes.MultiMixture([int(input('Enter Age: ' + str(ageEnum))), int(input('Enter Gender: ' + str(genderEnum))), int(input('Enter FamilyHistory: ' + str(familyHistoryEnum))), int(input('Enter dietEnum: ' + str(

        dietEnum))), int(input('Enter LifeStyle: ' + str(lifeStyleEnum))), int(input('Enter Cholesterol: ' + str(cholesterolEnum)))], bp.nodes.Categorical, p_heartdisease).get_moments()[0][heartDiseaseEnum['Yes']]

    print("Probability(HeartDisease) = " + str(res))


    m = int(input("Enter for Continue:0, Exit :1  "))


8a


from sklearn import datasets

from sklearn.metrics import accuracy_score

from sklearn.metrics import confusion_matrix

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np


from sklearn import preprocessing

from sklearn.mixture import GaussianMixture


iris = datasets.load_iris()


x = pd.DataFrame(iris.data)

x.columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']


y = pd.DataFrame(iris.target)

y.columns = ['Targets']


plt.figure(figsize=(14, 7))

colormap = np.array(['red', 'lime', 'black'])

plt.subplot(1, 2, 1)

plt.scatter(x.Sepal_Length, x.Sepal_Width, c=colormap[y.Targets], s=40)

plt.title('Sepal')

plt.subplot(1, 2, 2)

plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[y.Targets], s=40)

plt.title('Petal')


scaler = preprocessing.StandardScaler()

scaler.fit(x)

xsa = scaler.transform(x)

xs = pd.DataFrame(xsa, columns=x.columns)

xs.sample(5)


gmm = GaussianMixture(n_components=3)

gmm.fit(xs)

y_gmm = gmm.predict(xs)

print(y_gmm)


plt.figure(figsize=(14, 7))

colormap = np.array(['red', 'lime', 'black'])

plt.subplot(1, 2, 1)

plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[y.Targets], s=40)

plt.title('Real')

plt.subplot(1, 2, 2)

plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[y_gmm], s=40)

plt.title('EM')


print("accuracy_score", accuracy_score(y.Targets, y_gmm))

print("confusion_matrix\n", confusion_matrix(y.Targets, y_gmm))


8b


from sklearn import datasets

from sklearn.metrics import accuracy_score

from sklearn.metrics import confusion_matrix

import matplotlib.pyplot as plt

import pandas as pd

import numpy as np


from sklearn.cluster import KMeans


iris = datasets.load_iris()


x = pd.DataFrame(iris.data)

x.columns = ['Sepal_Length', 'Sepal_Width', 'Petal_Length', 'Petal_Width']


y = pd.DataFrame(iris.target)

y.columns = ['Targets']


plt.figure(figsize=(14, 7))

colormap = np.array(['red', 'lime', 'black'])

plt.subplot(1, 2, 1)

plt.scatter(x.Sepal_Length, x.Sepal_Width, c=colormap[y.Targets], s=40)

plt.title('Sepal')

plt.subplot(1, 2, 2)

plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[y.Targets], s=40)

plt.title('Petal')


model = KMeans(n_clusters=3)

model.fit(x)


plt.figure(figsize=(14, 7))

colormap = np.array(['red', 'lime', 'black'])

plt.subplot(1, 2, 1)

plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[y.Targets], s=40)

plt.title('Real')

plt.subplot(1, 2, 2)

plt.scatter(x.Petal_Length, x.Petal_Width, c=colormap[model.labels_], s=40)

plt.title('kmeans')


print("accuracy_score", accuracy_score(y.Targets, model.labels_))

print("confusion_matrix\n", confusion_matrix(y.Targets, model.labels_))


9

import csv

import random

import math

import operator



def loadDataset(filename, split, trainingSet=[], testSet=[]):

    with open(filename) as csvfile:

        lines = csv.reader(csvfile)

        dataset = list(lines)

        for x in range(len(dataset)-1):

            for y in range(4):

                dataset[x][y] = float(dataset[x][y])

            if random.random() < split:

                trainingSet.append(dataset[x])

            else:

                testSet.append(dataset[x])



def euclideanDistance(instance1, instance2, length):

    distance = 0

    for x in range(length):

        distance += pow((instance1[x] - instance2[x]), 2)

    return math.sqrt(distance)



def getNeighbors(trainingSet, testInstance, k):

    distances = []

    length = len(testInstance)-1

    for x in range(len(trainingSet)):

        dist = euclideanDistance(testInstance, trainingSet[x], length)

        distances.append((trainingSet[x], dist))

    distances.sort(key=operator.itemgetter(1))

    neighbors = []

    for x in range(k):

        neighbors.append(distances[x][0])

    return neighbors



def getResponse(neighbors):

    classVotes = {}

    for x in range(len(neighbors)):

        response = neighbors[x][-1]

        if response in classVotes:

            classVotes[response] += 1

        else:

            classVotes[response] = 1

    sortedVotes = sorted(classVotes.items(),

                         key=operator.itemgetter(1), reverse=True)

    return sortedVotes[0][0]



def getAccuracy(testSet, predictions):

    correct = 0

    for x in range(len(testSet)):

        if testSet[x][-1] == predictions[x]:

            correct += 1

    return (correct/float(len(testSet))) * 100.0



def main():


    trainingSet = []

    testSet = []

    split = 0.67

    loadDataset('KNN-input.csv', split, trainingSet, testSet)

    print('\n Number of Training data: ' + (repr(len(trainingSet))))

    print(' Number of Test Data: ' + (repr(len(testSet))))


    predictions = []

    k = 3

    print('\n The predictions are: ')

    for x in range(len(testSet)):

        neighbors = getNeighbors(trainingSet, testSet[x], k)

        result = getResponse(neighbors)

        predictions.append(result)

        print(' predicted=' + repr(result) +

              ', actual=' + repr(testSet[x][-1]))

    accuracy = getAccuracy(testSet, predictions)

    print('\n The Accuracy is: ' + repr(accuracy) + '%')

main()



10


import matplotlib.pyplot as plt

import pandas as pd

import numpy as np



def kernel(point, xmat, k):

    m, n = np.shape(xmat)

    weights = np.mat(np.eye((m)))

    for j in range(m):

        diff = point - X[j]

        weights[j, j] = np.exp(diff*diff.T/(-2.0*k**2))

    return weights



def localWeight(point, xmat, ymat, k):

    wei = kernel(point, xmat, k)

    W = (X.T*(wei*X)).I*(X.T*(wei*ymat.T))

    return W



def localWeightRegression(xmat, ymat, k):

    m, n = np.shape(xmat)

    ypred = np.zeros(m)

    for i in range(m):

        ypred[i] = xmat[i]*localWeight(xmat[i], xmat, ymat, k)

    return ypred



def graphPlot(X, ypred):

    sortindex = X[:, 1].argsort(0)

    xsort = X[sortindex][:, 0]

    fig = plt.figure()

    ax = fig.add_subplot(1, 1, 1)

    ax.scatter(bill, tip, color='green')

    ax.plot(xsort[:, 1], ypred[sortindex], color='red', linewidth=5)

    plt.xlabel('Total bill')

    plt.ylabel('Tip')

    plt.show()



data = pd.read_csv('tips.csv')

bill = np.array(data.total_bill)

tip = np.array(data.tip)


mbill = np.mat(bill)

mtip = np.mat(tip)

m = np.shape(mbill)[1]

one = np.mat(np.ones(m))

X = np.hstack((one.T, mbill.T))


ypred = localWeightRegression(X, mtip, 0.5)

graphPlot(X, ypred)




Comments