コード例 #1
0
 def test_with_generator(self):
     tmpX = (sample for sample in self.X)
     
     clf = GaussianNB()
     clf.fit(tmpX, self.Y)
     result = clf.predict([[-0.8, -1]])
     
     self.assertEqual(result, [1])      
コード例 #2
0
# from sklearn.naive_bayes import MultinomialNB
# from sklearn.preprocessing import OneHotEncoder
# X = xigua.iloc[:,:-1]
# y = xigua.iloc[:,-1]
# onehot = OneHotEncoder()
# X = onehot.fit_transform(X)
# y = np.asarray(y).reshape(-1, 1)

# clf = MultinomialNB()
# clf.fit(X, y)
# print("*********")
# print(clf.score(X, y))

#gaussian naive bayes test
import pandas as pd
from naive_bayes import GaussianNB
gender = pd.read_csv('Gender_classification.csv', header=0, encoding='utf-8')
test_sample = gender.iloc[-1, 1:]
gender_droped = gender.drop(gender.shape[0] - 1, axis=0, inplace=False)
Xtrain = gender_droped.iloc[:, 1:]
ytrain = gender_droped.人

clf = GaussianNB()
# print(clf.normal_density(x = 0, mu = 0, sigma = 1))
clf.fit(Xtrain, ytrain)
# print(clf.predict_single_instance(test_sample))
print(clf.predict(Xtrain))
print("the accuracy of training data:", clf.score(Xtrain, ytrain))

# from scipy.stats import norm
# print(norm.pdf(0 , loc = 0, scale = 1))
コード例 #3
0
                pl.pcolormesh(X, Y, Z, cmap='RdBu', vmin=z_min, vmax=z_max)
                pl.axis([x.min(), x.max(), y.min(), y.max()])
                pl.xlabel(i, fontsize=30)
                pl.ylabel(j, fontsize=30)
                cb = pl.colorbar()
                cb.set_label(metric, fontsize=30)
                save_path = '../MSPrediction-Python/plots/' + obj + '/' + clfName + '_' + metric + '_' + i + '_' + j + '.pdf'
                fig.savefig(save_path)


classifiers = {
    "LogisticRegression": LogisticRegression(),
    "KNN": KNeighborsClassifier(),
    "BayesBernoulli": BernoulliNB(),
    "BayesMultinomial": MultinomialNB(),
    "BayesGaussian": GaussianNB(),
    "BayesPoisson": PoissonNB(),
    "BayesGaussian2": GaussianNB2(),
    "SVM": SVC(probability=True),
    "RandomForest": RandomForestClassifier(),
    "LinearRegression": LinearRegression(),
    "BayesMixed": MixNB(),
    "BayesMixed2": MixNB2()
}

classifiers1 = {
    "LogisticRegression": LogisticRegression(),
    "BayesBernoulli": BernoulliNB(),
    "BayesGaussian": GaussianNB(),
    "BayesGaussian2": GaussianNB2(),
    "RandomForest": RandomForestClassifier(),
コード例 #4
0
from naive_bayes import GaussianNB
from sklearn import datasets
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np # linear algebra


iris = datasets.load_iris()
X = iris.data  # we only take the first 4 features.
y = iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=0)


NB = GaussianNB()
NB.fit(X_train, y_train)

probs=NB.predict(X_test)
y_pred=np.argmax(probs, 1)


print(f"Accuracy: {sum(y_pred==y_test)/X_test.shape[0]}")
コード例 #5
0
from naive_bayes import MultinomialNB, GaussianNB
import numpy as np
import pandas as pd

if __name__ == "__main__":
    df = pd.read_csv('iris.data', header=None)
    # print(df)
    x = df.iloc[:, [0, 1, 2, 3]].values
    y = df.iloc[:, 4].values
    # y = np.where(y == "Iris-setosa", 1, -1)
    # print(y)
    target = np.array([5.1, 3.5, 1.4, 0.2])
    target = np.array([5.9, 3.0, 4.2, 1.5])

    gnb = GaussianNB(x, y)
    gnb.fit()
    print(gnb.predict(target))

    # X = np.array([
    #     [1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
    #     ['S', 'M', 'M', 'S', 'S', 'S', 'M', 'M', 'L', 'L', 'L', 'M', 'M', 'L', 'L']
    # ])
    # X = X.T
    # y = np.array([-1, -1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1])
    #
    # target = np.array([2, 'S'])
    #
    # mnb = MultinomialNB(X, y)
    # mnb.setSmoothFactor(0)
    # mnb.fit()
    # print(mnb.predict(target))
コード例 #6
0
 def test_with_array(self):
     clf = GaussianNB()
     clf.fit(self.X, self.Y)
     result = clf.predict([[-0.8, -1]])
     
     self.assertEqual(result, [1])