예제 #1
0
# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 -x /opt/nec/nosupport/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

mat = pd.DataFrame([[1, 0, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0],
                    [0, 1, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0, 1, 0]],
                   dtype=np.float64)
lbl = np.array([0, 1, 1, 0], dtype=np.float64)

# fitting input matrix and label on linear nbm object
nbm = MultinomialNB(alpha=1.4).fit(mat, lbl)
nbm.debug_print()
print("predicting on nbm multinomial classifier model: ")
mnb = nbm.predict(mat)
print(mnb)
print("Accuracy of model")
nbm.score(mat, lbl)

nbm2 = BernoulliNB(alpha=1.4).fit(mat, lbl)
nbm2.debug_print()
print("predicting on nbm bernoulli classifier model: ")
bnb = nbm2.predict(mat)
print(bnb)
print("Accuracy of model")
nbm2.score(mat, lbl)
예제 #2
0
from frovedis.matrix.dvector import FrovedisDoubleDvector
from frovedis.mllib.naive_bayes import MultinomialNB
from frovedis.mllib.naive_bayes import BernoulliNB
import sys
import numpy as np
import pandas as pd

# Objective : When alpha numeric label pass

# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 -x /opt/nec/nosupport/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

mat = pd.DataFrame([[1, 0, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0],
                    [0, 1, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0, 1, 0]],
                   dtype=np.float64)
lbl = np.array([0, "a", 1, 0])

try:
    nbm = MultinomialNB(alpha=1).fit(mat, lbl)
except ValueError, e:
    print("Status: Passed")
else:
    print("Status: Failed")

FrovedisServer.shut_down()
예제 #3
0
from frovedis.mllib.naive_bayes import MultinomialNB
from frovedis.mllib.naive_bayes import BernoulliNB
import sys
import numpy as np
import pandas as pd

# Objective : When data contains special characters 

# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 /opt/nec/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

mat = pd.read_csv("./input/train_1.csv")
lbl = np.array([1, 2, 3, 4, 5, 6,7,8],dtype=np.float64)

try:
  # fitting input matrix and label on linear nbm object
  nbm = MultinomialNB(alpha=1, fit_prior=True, class_prior=None, verbose = 0).fit(mat,lbl)
except TypeError, e:
  print("Status: Passed")
else:
  print("Status: Failed")
  nbm.release()

FrovedisServer.shut_down()

예제 #4
0
# Objective: When alpha is negative

# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 -x /opt/nec/nosupport/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

mat = pd.DataFrame([[1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0],
                    [0, 1, 0, 1, 0, 1, 0],
                    [0, 1, 0, 0, 1, 0, 1],
                    [1, 0, 0, 1, 0, 1, 0]])
lbl = np.array([0, 1, 1, 0])

# fitting input matrix and label on linear nbm object
try: 
  nbm = MultinomialNB(alpha=-1.0).fit(mat,lbl)
  nbm2 = BernoulliNB(alpha=-1.0).fit(mat,lbl)
except ValueError, e:
  print("Status: Passed")
else:
  print("Status: Failed")
  nbm.release()
  nbm2.release()
  
FrovedisServer.shut_down()

예제 #5
0
# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 -x /opt/nec/nosupport/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

mat = np.asmatrix([[1, 0, 1, 0, 0, 1, 0],
                   [0, 1, 0, 1, 0, 1, 0],
                   [0, 1, 0, 0, 1, 0, 1],
                   [1, 0, 0, 1, 0, 1, 0]],dtype=np.float64)
lbl = np.array([0, 1, 1, 0],dtype=np.float64)

# fitting input matrix and label on linear nbm object
nbm = MultinomialNB(alpha=1, fit_prior=True, class_prior=None, verbose = 0).fit(mat,lbl)
nbm.debug_print()
print("predicting on nbm multinomial classifier model: ")
mnb =  nbm.predict(mat)
print("Accuracy of model", nbm.score(mat,lbl))

nbm2 = BernoulliNB(alpha=1.0,fit_prior=True,class_prior=None,binarize=0.0,verbose=0).fit(mat,lbl)
nbm2.debug_print()
print("predicting on nbm bernoulli classifier model: ")
bnb =  nbm2.predict(mat)
print("Accuracy of model", nbm2.score(mat,lbl))

if (lbl == mnb).all() and (lbl == bnb).all():
  print("Status: Passed")
else:
  print("Status: Failed")
예제 #6
0
from frovedis.mllib.naive_bayes import MultinomialNB, BernoulliNB
import sys
import numpy as np
import pandas as pd

# Objective: testing with dataframe matrix and numpy label

# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 -x /opt/nec/nosupport/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

mat = pd.DataFrame([[1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0],
                    [0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0],
                    [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0],
                    [1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0]])
lbl = np.array([0.0, 1.0, 1.0, 0.0])

# one-liner prediction
ret1 = MultinomialNB(alpha=1.0).fit(mat, lbl).predict(mat)
ret2 = BernoulliNB(alpha=1.0).fit(mat, lbl).predict(mat)
if (ret1 == lbl).all() and (ret2 == lbl).all():
    print("Status: Passed")
else:
    print("Status: Failed")

FrovedisServer.shut_down()
예제 #7
0
# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 /opt/nec/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

#mat = np.random.randint(5, size=(6, 100))
#lbl = np.array([1, 2, 1, 1, 2, 1])
mat = pd.DataFrame([[1, 0, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0],
                    [0, 1, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0, 1, 0]],
                   dtype=np.float64)
lbl = np.array([0, 1, 1, 0], dtype=np.float64)

nbm = MultinomialNB(alpha=1.0).fit(mat, lbl)
nbm.debug_print()
print("predicting on nbm multinomial classifier model: ")
nbmc = nbm.predict(mat)
print("Accuracy of model: ", nbm.score(mat, lbl))

nbm2 = BernoulliNB(alpha=1.0).fit(mat, lbl)
nbm2.debug_print()
print("predicting on nbm bernoulli classifier model: ")
nbbc = nbm2.predict(mat)
print("Accuracy of model: ", nbm2.score(mat, lbl))

from sklearn.naive_bayes import MultinomialNB
clb = MultinomialNB(alpha=1.0).fit(mat, lbl)

from sklearn.naive_bayes import BernoulliNB
예제 #8
0
# Objective: testing of save API of multinomial model

# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print 'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 /opt/nec/frovedis/ve/bin/frovedis_server")'
    quit()
FrovedisServer.initialize(argvs[1])

mat = pd.DataFrame([[1, 0, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 1, 0],
                    [0, 1, 0, 0, 1, 0, 1], [1, 0, 0, 1, 0, 1, 0]],
                   dtype=np.float64)
lbl = np.array([0, 1, 1, 0], dtype=np.float64)

# fitting input matrix and label on linear nbm object
nbm = MultinomialNB(alpha=1.0).fit(mat, lbl)
os.system("rm -rf ./out/MNBModel")  #deleting if any
nbm.save("./out/MNBModel")
try:
    fh = open("./out/MNBModel/label", "rb")
except IOError:
    print "Failed"
else:
    print "Passed"
    fh.close()

os.system("rm -rf ./out/MNBModel")
nbm.release()
FrovedisServer.shut_down()
예제 #9
0
from frovedis.matrix.crs import FrovedisCRSMatrix
from frovedis.mllib.naive_bayes import MultinomialNB
from frovedis.mllib.naive_bayes import BernoulliNB

# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print ('Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 -x /opt/nec/nosupport/frovedis/ve/bin/frovedis_server")')
    quit()
FrovedisServer.initialize(argvs[1])

# Demo of Multinomial Naive Bayes
mat = FrovedisCRSMatrix(dtype=np.float64).load("./input/multi.txt")
lbl = np.array([1.0,0.0,1.0,1.0,1.0,0.0,1.0,1.0])
nbm1 = MultinomialNB(alpha=1.0).fit(mat,lbl)
print("\nmultinomial model: ")
nbm1.debug_print()

mnb = nbm1.predict(mat)
print("prediction on multinomial classifier model: ", mnb)
print("accuracy of model: ", nbm1.score(mat,lbl))

# Demo of Bernoulli Naive Bayes
mat = FrovedisCRSMatrix(dtype=np.float64).load("./input/bern.txt")
lbl = np.array([1.0,0.0,1.0,1.0,1.0,0.0,1.0,1.0])
nbm2 = BernoulliNB(alpha=1.0).fit(mat,lbl)
print("\nbernoulli model: ")
nbm2.debug_print()

bnb =  nbm2.predict(mat)
예제 #10
0
# initializing the Frovedis server
argvs = sys.argv
argc = len(argvs)
if (argc < 2):
    print(
        'Please give frovedis_server calling command as the first argument \n(e.g. "mpirun -np 2 /opt/nec/frovedis/ve/bin/frovedis_server")'
    )
    quit()

from frovedis.exrpc.server import FrovedisServer
FrovedisServer.initialize(argvs[1])

# classification data
from sklearn.datasets import load_breast_cancer
mat, lbl = load_breast_cancer(return_X_y=True)

mnb = MultinomialNB(alpha=1.0).fit(mat, lbl)
pred = mnb.predict(mat)
print("prediction on multinomial classifier model: ")
print(pred)
print("prediction accuracy: %.4f" % (mnb.score(mat, lbl)))

bnb = BernoulliNB(alpha=1.0).fit(mat, lbl)
pred = bnb.predict(mat)
print("prediction on bernoulli classifier model: ")
print(pred)
print("prediction accuracy: %.4f" % (bnb.score(mat, lbl)))

# Clean-up
FrovedisServer.shut_down()