本文整理汇总了Python中sklearn.metrics.precision_score函数的典型用法代码示例。如果您正苦于以下问题:Python precision_score函数的具体用法?Python precision_score怎么用?Python precision_score使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了precision_score函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: nearest_centroid
def nearest_centroid(input_file,Output,test_size):
ncol=tools.file_col_coma(input_file)
data = np.loadtxt(input_file, delimiter=',', usecols=range(ncol-1))
X = data[:,1:]
y = data[:,0]
n_samples, n_features = X.shape
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
print X_train.shape, X_test.shape
clf = NearestCentroid()
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print "Nearest Centroid Classifier "
print "classification accuracy:", metrics.accuracy_score(y_test, y_pred)
print "precision:", metrics.precision_score(y_test, y_pred)
print "recall:", metrics.recall_score(y_test, y_pred)
print "f1 score:", metrics.f1_score(y_test, y_pred)
print "\n"
results = Output+"Nearest_Centroid_metrics_test.txt"
file = open(results, "w")
file.write("Nearest Centroid Classifier estimator accuracy\n")
file.write("Classification Accuracy Score: %f\n"%metrics.accuracy_score(y_test, y_pred))
file.write("Precision Score: %f\n"%metrics.precision_score(y_test, y_pred))
file.write("Recall Score: %f\n"%metrics.recall_score(y_test, y_pred))
file.write("F1 Score: %f\n"%metrics.f1_score(y_test, y_pred))
file.write("\n")
file.write("True Value, Predicted Value, Iteration\n")
for n in xrange(len(y_test)):
file.write("%f,%f,%i\n"%(y_test[n],y_pred[n],(n+1)))
file.close()
title = "Nearest Centroid %f"%test_size
save = Output + "Nearest_Centroid_confusion_matrix"+"_%s.png"%test_size
plot_confusion_matrix(y_test, y_pred,title,save)
lvltrace.lvltrace("LVLSortie dans stochasticGD split_test")
示例2: gaussianNB
def gaussianNB(input_file,Output,test_size):
lvltrace.lvltrace("LVLEntree dans gaussianNB split_test")
ncol=tools.file_col_coma(input_file)
data = np.loadtxt(input_file, delimiter=',', usecols=range(ncol-1))
X = data[:,1:]
y = data[:,0]
n_samples, n_features = X.shape
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
print X_train.shape, X_test.shape
# Instantiate the estimator
clf = GaussianNB()
# Fit the estimator to the data
clf.fit(X_train, y_train)
# Use the model to predict the last several labels
y_pred = clf.predict(X_test)
print "Gaussian Naive Bayes estimator accuracy "
print "classification accuracy:", metrics.accuracy_score(y_test, y_pred)
print "precision:", metrics.precision_score(y_test, y_pred)
print "recall:", metrics.recall_score(y_test, y_pred)
print "f1 score:", metrics.f1_score(y_test, y_pred)
results = Output+"GaussianNB_metrics_test.txt"
file = open(results, "w")
file.write("Gaussian Naive Bayes estimator accuracy\n")
file.write("Classification Accuracy Score: %f\n"%metrics.accuracy_score(y_test, y_pred))
file.write("Precision Score: %f\n"%metrics.precision_score(y_test, y_pred))
file.write("Recall Score: %f\n"%metrics.recall_score(y_test, y_pred))
file.write("F1 Score: %f\n"%metrics.f1_score(y_test, y_pred))
file.write("True Value, Predicted Value, Iteration\n")
for n in xrange(len(y_test)):
file.write("%f,%f,%i\n"%(y_test[n],y_pred[n],(n+1)))
file.close()
title = "Gaussian Naive Bayes %f"%test_size
save = Output + "Gaussian_NB_confusion_matrix"+"_%s.png"%test_size
plot_confusion_matrix(y_test, y_pred,title,save)
lvltrace.lvltrace("LVLsortie dans gaussianNB split_test")
示例3: predictSVD
def predictSVD(svd, row, column, d):
# start = timeit.default_timer()
u = svd[0] #clf.components_
s = svd[1] #clf.explained_variance_
vt = svd[2] #clf.fit_transform(X)
# print " fitting done.";
# stop = timeit.default_timer()
# print " runtime: " + str(stop - start)
# print "d:"
# print d
# matrixY = clf.components_
probsY = []
# print "dot products:"
for i in range(len(row)):
# print np.dot(u[:,column[i]], v[row[i],:])
prob = np.sum(u[column[i],:]*s*vt[:,row[i]])
if(prob < 0): prob = 0
if(prob > 1): prob = 1
probsY.append(prob)
probsY = np.array(probsY)
preds = np.zeros(shape=len(probsY))
preds[probsY >= 0.5] = 1
print "Precision"
print precision_score(d, preds)
print "Recall"
print recall_score(d, preds)
print "F-Score"
print f1_score(d, preds)
return probsY, preds
示例4: SVC_linear
def SVC_linear(input_file,Output,test_size):
lvltrace.lvltrace("LVLEntree dans SVC_linear split_test")
ncol=tools.file_col_coma(input_file)
data = np.loadtxt(input_file, delimiter=',', usecols=range(ncol-1))
X = data[:,1:]
y = data[:,0]
n_samples, n_features = X.shape
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size)
print X_train.shape, X_test.shape
clf=svm.SVC(kernel='linear')
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print "C-Support Vector Classifcation (with RBF linear) "
print "y_test, y_pred, iteration"
print "classification accuracy:", metrics.accuracy_score(y_test, y_pred)
print "precision:", metrics.precision_score(y_test, y_pred)
print "recall:", metrics.recall_score(y_test, y_pred)
print "f1 score:", metrics.f1_score(y_test, y_pred)
print "\n"
results = Output+"SVM_Linear_Kernel_metrics_test.txt"
file = open(results, "w")
file.write("Support Vector Machine with Linear Kernel estimator accuracy\n")
file.write("Classification Accuracy Score: %f\n"%metrics.accuracy_score(y_test, y_pred))
file.write("Precision Score: %f\n"%metrics.precision_score(y_test, y_pred))
file.write("Recall Score: %f\n"%metrics.recall_score(y_test, y_pred))
file.write("F1 Score: %f\n"%metrics.f1_score(y_test, y_pred))
file.write("\n")
file.write("True Value, Predicted Value, Iteration\n")
for n in xrange(len(y_test)):
file.write("%f,%f,%i\n"%(y_test[n],y_pred[n],(n+1)))
file.close()
title = "SVC linear %f"%test_size
save = Output + "SVC_linear_confusion_matrix"+"_%s.png"%test_size
plot_confusion_matrix(y_test, y_pred,title,save)
lvltrace.lvltrace("LVLsortie dans SVC_linear split_test")
示例5: evaluate
def evaluate(ytest, ypred, filename='metrics.txt'):
true_result = [1 if item > 0.5 else 0 for item in ytest]
pred_result = [1 if item > 0.5 else 0 for item in ypred]
cm = confusion_matrix(true_result, pred_result)
print('\nConfusion matrix:')
print(cm)
print("\nLoss classified as loss", cm[0][0])
print("Wins classified as wins", cm[1][1])
print("Wins classified as loss", cm[1][0])
print("Loss classified as wins", cm[0][1])
print('\nAccuracy:\t', accuracy_score(true_result, pred_result))
print('Precision:\t', precision_score(true_result, pred_result))
print('Recall: \t', recall_score(true_result, pred_result))
print('F1 score:\t', f1_score(true_result, pred_result))
print('Mean absolute error:\t', mean_absolute_error(ytest, ypred))
# print to file
print("Loss classified as loss", cm[0][0], file=open(filename, "a"))
print("Wins classified as wins", cm[1][1], file=open(filename, "a"))
print("Wins classified as loss", cm[1][0], file=open(filename, "a"))
print("Loss classified as wins", cm[0][1], file=open(filename, "a"))
print('\nAccuracy:\t', accuracy_score(true_result, pred_result), file=open(filename, "a"))
print('Precision:\t', precision_score(true_result, pred_result), file=open(filename, "a"))
print('Recall: \t', recall_score(true_result, pred_result), file=open(filename, "a"))
print('F1 score:\t', f1_score(true_result, pred_result), file=open(filename, "a"))
print('Mean absolute error:\t', mean_absolute_error(ytest, ypred), file=open(filename, "a"))
示例6: trainModel
def trainModel(self,folds):
kf = cross_validation.StratifiedKFold(self.y_total,n_folds=folds,shuffle=True,random_state=random.randint(1,100))
for (train_index,test_index) in (kf):
self.X_train = [self.X_total[i] for i in train_index]
self.X_test = [self.X_total[i] for i in test_index]
self.y_train = [self.y_total[i] for i in train_index]
self.y_test = [self.y_total[i] for i in test_index]
print "################"
print "Original"
print np.array(self.y_test)
print "################"
self.clf = self.clf.fit(self.X_train,self.y_train)
print "Predicted"
y_pred = self.clf.predict(self.X_test)
print y_pred
print "################"
print "Evaluation\n"
cm = confusion_matrix(self.y_test,y_pred)
print cm
print "Precision Score:"
print precision_score(self.y_test,y_pred,average="macro")
print "Recall Score:"
print recall_score(self.y_test,y_pred,average="macro")
print "Accuracy Score:"
print accuracy_score(self.y_test,y_pred)
示例7: learning_curve_mod
def learning_curve_mod(data, labels, clf, percents, d=100, avg=3, test_size=.2):
"""
This method calculates the performance of the training and cross validation test set as the training
set size increases and returns the performance at each percent
Args:
:param data: (md.array) The raw data to use for training and cross validation testing
:param labels: (nd.array) the labels associated with the data
:param clf: (sklearn classifier) the classifier to be used for training
:param percents: (nd.array) a list of percent of training data to use
:param d: (int) The number of principle components to calculate
:param avg: (int) The number of iterations to average when calculating performance
:param test_size: (double [0,1]) The size of the testing set
Return:
:return: train_accuracies (list) performance on the training set
:return: test_accuracies (list) performance on the testing set
"""
# split into train and testing dataset
x_train, x_test, y_train, y_test = train_test_split(data.T, labels, test_size=test_size, random_state=0)
x_test = x_test.T
train_accuracies = []
test_accuracies = []
for percent in percents:
temp_train_accuracies = []
temp_test_accuracies = []
print percent
for i in range(0, avg):
x_train_2, x_test_2, y_train_2, y_test_2 = train_test_split(x_train, y_train, test_size=percent)
x_train_2 = x_train_2.T
# Subtract off the mean
mean_face = np.mean(x_train_2, axis=1)
x_train_2 = x_train_2 - mean_face
# Find low dimensional subspace using PCA
pca = PCA(n_components=d)
pca.fit(x_train_2)
model = pca.transform(x_train_2)
# Project the known faces onto the face space
label_map = np.dot(x_train_2.T, model)
# Train a KNN classifier
clf.fit(label_map, y_train_2)
# project the unknown faces onto face space
W_train = np.dot(x_train_2.T - mean_face.T, model)
W_test = np.dot(x_test.T - mean_face.T, model)
test_prediction = clf.predict(W_test)
temp_test_accuracies.append(metrics.precision_score(y_test, test_prediction))
train_prediction = clf.predict(W_train)
temp_train_accuracies.append(metrics.precision_score(y_train_2, train_prediction))
train_accuracies.append(np.mean(temp_train_accuracies))
test_accuracies.append(np.mean(temp_test_accuracies))
return train_accuracies, test_accuracies
示例8: main
def main():
resize_shape = 64
print "data is loading..."
train_X, train_Y, test_X, test_Y = load_data(resize_shape)
print "data is loaded"
print "feature engineering..."
learning_rate = 0.01
training_iters = 100000
batch_size = 128
display_step = 10
# Network Parameters
n_input = resize_shape*resize_shape # MNIST data input (img shape: 28*28)
n_classes = 62 # MNIST total classes (0-9 digits)
dropout = 0.5 # Dropout, probability to keep units
with tf.Session() as sess:
cnn = CNN(sess, learning_rate, training_iters, batch_size, display_step, n_input, n_classes, dropout,resize_shape)
train_X = cnn.inference(train_X)
test_X = cnn.inference(test_X)
print "feature engineering is complete"
print 'training phase'
clf = svm.LinearSVC().fit(train_X, train_Y)
print 'test phase'
predicts = clf.predict(test_X)
# measure function
print 'measure phase'
print confusion_matrix(test_Y, predicts)
print f1_score(test_Y, predicts, average=None)
print precision_score(test_Y, predicts, average=None)
print recall_score(test_Y, predicts, average=None)
print accuracy_score(test_Y, predicts)
示例9: randomforest
def randomforest(input_file,Output):
lvltrace.lvltrace("LVLEntree dans randomforest")
ncol=tools.file_col_coma(input_file)
data = np.loadtxt(input_file, delimiter=',', usecols=range(ncol-1))
X = data[:,1:]
y = data[:,0]
n_samples, n_features = X.shape
clf = RandomForestClassifier(n_estimators=10)
clf.fit(X,y)
y_pred = clf.predict(X)
print "#########################################################################################################\n"
print "The Random forest algo "
print "classification accuracy:", metrics.accuracy_score(y, y_pred)
print "precision:", metrics.precision_score(y, y_pred)
print "recall:", metrics.recall_score(y, y_pred)
print "f1 score:", metrics.f1_score(y, y_pred)
print "\n"
print "#########################################################################################################\n"
results = Output+"Random_Forest_metrics.txt"
file = open(results, "w")
file.write("Random Forest Classifier estimator accuracy\n")
file.write("Classification Accuracy Score: %f\n"%metrics.accuracy_score(y, y_pred))
file.write("Precision Score: %f\n"%metrics.precision_score(y, y_pred))
file.write("Recall Score: %f\n"%metrics.recall_score(y, y_pred))
file.write("F1 Score: %f\n"%metrics.f1_score(y, y_pred))
file.write("\n")
file.write("True Value, Predicted Value, Iteration\n")
for n in xrange(len(y)):
file.write("%f,%f,%i\n"%(y[n],y_pred[n],(n+1)))
file.close()
title = "The Random forest"
save = Output + "Random_Forest_confusion_matrix.png"
plot_confusion_matrix(y, y_pred,title,save)
lvltrace.lvltrace("LVLSortie dans randomforest")
示例10: _clf_mlp
def _clf_mlp(trX,teX,trY,teY):
print "MLP"
print trX.shape,"trX shape"
print "Enter Layer for MLP"
layer=input()
# print "enter delIdx"
# delIdx=input()
# while(delIdx):
# trX=np.delete(trX,-1,axis=0)
# trY=np.delete(trY,-1,axis=0)
# delIdx=delIdx-1
print "factors",factors(trX.shape[0])
teY=teY.astype(np.int32)
trY=trY.astype(np.int32)
print trX.shape,"trX shape"
print "enter no of mini batch"
mini_batch=int(input())
mlp = TfMultiLayerPerceptron(eta=0.01,
epochs=100,
hidden_layers=layer,
activations=['relu' for i in range(len(layer))],
print_progress=3,
minibatches=mini_batch,
optimizer='adam',
random_seed=1)
mlp.fit(trX,trY)
pred=mlp.predict(teX)
print _f_count(teY),"test f count"
pred=pred.astype(np.int32)
print _f_count(pred),"pred f count"
conf_mat=confusion_matrix(teY, pred)
process_cm(conf_mat, to_print=True)
print precision_score(teY,pred),"Precision Score"
print recall_score(teY,pred),"Recall Score"
print roc_auc_score(teY,pred), "ROC_AUC"
示例11: stochasticGD
def stochasticGD(input_file,Output):
lvltrace.lvltrace("LVLEntree dans stochasticGD")
ncol=tools.file_col_coma(input_file)
data = np.loadtxt(input_file, delimiter=',', usecols=range(ncol-1))
X = data[:,1:]
y = data[:,0]
n_samples, n_features = X.shape
clf = SGDClassifier(loss="hinge", penalty="l2")
clf.fit(X,y)
y_pred = clf.predict(X)
print "#########################################################################################################\n"
print "Stochastic Gradient Descent "
print "classification accuracy:", metrics.accuracy_score(y, y_pred)
print "precision:", metrics.precision_score(y, y_pred)
print "recall:", metrics.recall_score(y, y_pred)
print "f1 score:", metrics.f1_score(y, y_pred)
print "\n"
print "#########################################################################################################\n"
results = Output+"Stochastic_GD_metrics.txt"
file = open(results, "w")
file.write("Stochastic Gradient Descent estimator accuracy\n")
file.write("Classification Accuracy Score: %f\n"%metrics.accuracy_score(y, y_pred))
file.write("Precision Score: %f\n"%metrics.precision_score(y, y_pred))
file.write("Recall Score: %f\n"%metrics.recall_score(y, y_pred))
file.write("F1 Score: %f\n"%metrics.f1_score(y, y_pred))
file.write("\n")
file.write("True Value, Predicted Value, Iteration\n")
for n in xrange(len(y)):
file.write("%f,%f,%i\n"%(y[n],y_pred[n],(n+1)))
file.close()
title = "Stochastic Gradient Descent"
save = Output + "Stochastic_GD_confusion_matrix.png"
plot_confusion_matrix(y, y_pred,title,save)
lvltrace.lvltrace("LVLSortie dans stochasticGD")
示例12: SVC_linear
def SVC_linear(input_file,Output):
lvltrace.lvltrace("LVLEntree dans SVC_linear")
ncol=tools.file_col_coma(input_file)
data = np.loadtxt(input_file, delimiter=',', usecols=range(ncol-1))
X = data[:,1:]
y = data[:,0]
n_samples, n_features = X.shape
clf=svm.SVC(kernel='linear')
clf.fit(X,y)
y_pred = clf.predict(X)
print "#########################################################################################################\n"
print "C-Support Vector Classifcation (with linear kernel) "
print "classification accuracy:", metrics.accuracy_score(y, y_pred)
print "precision:", metrics.precision_score(y, y_pred)
print "recall:", metrics.recall_score(y, y_pred)
print "f1 score:", metrics.f1_score(y, y_pred)
print "\n"
print "#########################################################################################################\n"
results = Output+"SVM_Linear_Kernel_metrics.txt"
file = open(results, "w")
file.write("Support Vector Machine with Linear Kernel estimator accuracy\n")
file.write("Classification Accuracy Score: %f\n"%metrics.accuracy_score(y, y_pred))
file.write("Precision Score: %f\n"%metrics.precision_score(y, y_pred))
file.write("Recall Score: %f\n"%metrics.recall_score(y, y_pred))
file.write("F1 Score: %f\n"%metrics.f1_score(y, y_pred))
file.write("\n")
file.write("True Value, Predicted Value, Iteration\n")
for n in xrange(len(y)):
file.write("%f,%f,%i\n"%(y[n],y_pred[n],(n+1)))
file.close()
title = "SVC - linear Kernel"
save = Output + "SVC_linear_confusion_matrix.png"
plot_confusion_matrix(y, y_pred,title,save)
lvltrace.lvltrace("LVLSortie dans SVC_linear")
示例13: run_model
def run_model(X_test, X_train, y_test, y_train, prob_threshold = 20, layers = 5, nodes = 64, dropout = 50):
print "run_model RUNNING"
# Grab the model
model = get_model(X_test, layers =layers, dropout = dropout)
model.fit(X_train, y_train, nb_epoch=20, batch_size=16, verbose = 0)
# Get the training and test predictions from our model fit.
train_predictions = model.predict_proba(X_train)
test_predictions = model.predict_proba(X_test)
# Set these to either 0 or 1 based off the probability threshold we
# passed in (divide by 100 becuase we passed in intergers).
train_preds = (train_predictions) >= prob_threshold / 100.0
test_preds = (test_predictions) >= prob_threshold / 100.0
# Calculate the precision and recall. Only output until
precision_score_train = precision_score(y_train, train_preds)
precision_score_test = precision_score(y_test, test_preds)
acc_train = accuracy_score(y_train, train_preds)
acc_test = accuracy_score(y_test, test_preds)
recall_score_train = recall_score(y_train, train_preds)
recall_score_test = recall_score(y_test, test_preds)
return precision_score_train, precision_score_test, recall_score_train, recall_score_test, acc_train, acc_test, model
示例14: score
def score(y_true, y_pred):
precision_weighted = metrics.precision_score(
y_true, y_pred, average='weighted')
precision_ave = np.mean(metrics.precision_score(
y_true, y_pred, average=None)[::12])
recall_weighted = metrics.recall_score(
y_true, y_pred, average='weighted')
recall_ave = np.mean(metrics.recall_score(
y_true, y_pred, average=None)[::12])
f1_weighted = metrics.f1_score(
y_true, y_pred, average='weighted')
f1_ave = np.mean(metrics.f1_score(
y_true, y_pred, average=None)[::12])
stat_line = " Precision: %0.4f\t Recall: %0.4f\tf1: %0.4f"
res1 = "Weighted: " + stat_line % (100*precision_weighted,
100*recall_weighted,
100*f1_weighted)
res2 = "Averaged: " + stat_line % (100*precision_ave,
100*recall_ave,
100*f1_ave)
res3 = "-"*72
outputs = [res3, res1, res2, res3]
return "\n".join(outputs)
示例15: do_cv
def do_cv(self):
from sklearn.metrics import precision_score
data = util.get_x_y_for_cv()
X = data['x']
y = data['y']['Survived']
skf = StratifiedKFold(y, n_folds=5)
e_test = []
e_train = []
for train_idx, test_idx in skf:
train_x = X.iloc[train_idx]
train_y = y.iloc[train_idx]
test_x = X.iloc[test_idx]
test_y = y.iloc[test_idx]
self.model.fit(train_x, train_y)
yhat = self.model.predict(test_x)
e_test.append(precision_score(test_y, yhat))
yhat_train = self.model.predict(train_x)
e_train.append(precision_score(train_y, yhat_train))
print np.mean(e_train)
print np.mean(e_test)