本文整理汇总了Python中sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis.predict方法的典型用法代码示例。如果您正苦于以下问题:Python QuadraticDiscriminantAnalysis.predict方法的具体用法?Python QuadraticDiscriminantAnalysis.predict怎么用?Python QuadraticDiscriminantAnalysis.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis
的用法示例。
在下文中一共展示了QuadraticDiscriminantAnalysis.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: crossValidate
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
def crossValidate(attributes, outcomes, foldCount, ownFunction=True):
presList =[]; recallList = []
accrList = []; fMeasList = []
aucList = []
testingEstimate = []
otcmVal = list(set(outcomes))
params = {}; featLen = 4;
attrFolds = getFolds(attributes,foldCount)
otcmFolds = getFolds(outcomes,foldCount)
testDataList = copy.copy(attrFolds)
testOtcmList = copy.copy(otcmFolds)
for itr in range(foldCount):
trainDataList = []
trainOtcmList = []
for intitr in range (foldCount):
if intitr != itr:
trainDataList.append(attrFolds[intitr])
trainOtcmList.append(otcmFolds[intitr])
trainDataArr = np.array(trainDataList).reshape(-1,featLen)
trainOtcmArr = np.array(trainOtcmList).reshape(-1)
testDataArr = np.array(testDataList[itr]).reshape(-1,featLen)
testOtcmArr = np.array(testOtcmList[itr]).reshape(-1)
if ownFunction:
params = getParams(trainDataArr,trainOtcmArr,otcmVal,featLen)
testingEstimate = gdaNDEstimate(testDataArr,params,otcmVal)
else:
#clf = LinearDiscriminantAnalysis()
clf = QuadraticDiscriminantAnalysis()
clf.fit(trainDataArr,trainOtcmArr)
trainingEstimate = clf.predict(trainDataArr)
testingEstimate = clf.predict(testDataArr)
if itr == 0 and len(otcmVal)==2:
addTitle = "Own" if ownFunction else "Inbuilt"
metric = getMetrics(testOtcmArr,testingEstimate,otcmVal,showPlot=True,title="GDA2D Versicolor,Virginica - %s"%addTitle)
else:
metric = getMetrics(testOtcmArr,testingEstimate,otcmVal)
accrList.append(metric[0])
presList.append(metric[1])
recallList.append(metric[2])
fMeasList.append(metric[3])
aucList.append(metric[4])
return accrList, presList, recallList, fMeasList, aucList
示例2: create_symbol_forecast_model
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
def create_symbol_forecast_model(self):
# Create a lagged series of the S&P500 US stock market index
snpret = create_lagged_series(
self.symbol_list[0], self.model_start_date,
self.model_end_date, lags=5
)
# Use the prior two days of returns as predictor
# values, with direction as the response
x = snpret[["Lag1", "Lag2"]]
y = snpret["Direction"]
# Create training and test sets, each of them is series
start_test = self.model_start_test_date
x_train = x[x.index < start_test]
x_test = x[x.index >= start_test]
y_train = y[y.index < start_test]
y_test = y[y.index >= start_test]
model = QuadraticDiscriminantAnalysis()
model.fit(x_train, y_train)
# return nd array
pred_test = model.predict(x_test)
print("Error Rate is {0}".format((y_test != pred_test).sum() * 1. / len(y_test)))
return model
示例3: QuadraticDiscriminantAnalysiscls
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
class QuadraticDiscriminantAnalysiscls(object):
"""docstring for ClassName"""
def __init__(self):
self.qda_cls = QuadraticDiscriminantAnalysis()
self.prediction = None
self.train_x = None
self.train_y = None
def train_model(self, train_x, train_y):
try:
self.train_x = train_x
self.train_y = train_y
self.qda_cls.fit(train_x, train_y)
except:
print(traceback.format_exc())
def predict(self, test_x):
try:
self.test_x = test_x
self.prediction = self.qda_cls.predict(test_x)
return self.prediction
except:
print(traceback.format_exc())
def accuracy_score(self, test_y):
try:
# return r2_score(test_y, self.prediction)
return self.qda_cls.score(self.test_x, test_y)
except:
print(traceback.format_exc())
示例4: SNPForecastingStrategy
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
class SNPForecastingStrategy(Strategy):
"""
Requires:
symbol - A stock symbol on which to form a strategy on.
bars - A DataFrame of bars for the above symbol."""
def __init__(self, symbol, bars):
self.symbol = symbol
self.bars = bars
self.create_periods()
self.fit_model()
def create_periods(self):
"""Create training/test periods."""
self.start_train = datetime.datetime(2001,1,10)
self.start_test = datetime.datetime(2005,1,1)
self.end_period = datetime.datetime(2005,12,31)
def fit_model(self):
"""Fits a Quadratic Discriminant Analyser to the
US stock market index (^GPSC in Yahoo)."""
# Create a lagged series of the S&P500 US stock market index
snpret = create_lagged_series(self.symbol, self.start_train,
self.end_period, lags=5)
# Use the prior two days of returns as
# predictor values, with direction as the response
X = snpret[["Lag1","Lag2"]]
y = snpret["Direction"]
# Create training and test sets
X_train = X[X.index < self.start_test]
y_train = y[y.index < self.start_test]
# Create the predicting factors for use
# in direction forecasting
self.predictors = X[X.index >= self.start_test]
# Create the Quadratic Discriminant Analysis model
# and the forecasting strategy
self.model = QuadraticDiscriminantAnalysis()
self.model.fit(X_train, y_train)
def generate_signals(self):
"""Returns the DataFrame of symbols containing the signals
to go long, short or hold (1, -1 or 0)."""
signals = pd.DataFrame(index=self.bars.index)
signals['signal'] = 0.0
# Predict the subsequent period with the QDA model
signals['signal'] = self.model.predict(self.predictors)
# Remove the first five signal entries to eliminate
# NaN issues with the signals DataFrame
signals['signal'][0:5] = 0.0
signals['positions'] = signals['signal'].diff()
return signals
示例5: doQDA
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
def doQDA(x,digits,s):
myLDA = LDA()
myLDA.fit(x.PCA[:,:s],digits.train_Labels)
newtest = digits.test_Images -x.centers
[email protected](x.V[:s,:])
labels = myLDA.predict(newtest)
errors = class_error_rate(labels.reshape(1,labels.shape[0]),digits.test_Labels)
return errors
示例6: confusion
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
def confusion(digits):
myLDA = LDA()
x = center_matrix_SVD(digits.train_Images)
myLDA.fit(x.PCA[:,:50],digits.train_Labels)
newtest = digits.test_Images -x.centers
[email protected](x.V[:50,:])
labels = myLDA.predict(newtest)
import sklearn.metrics as f
print(f.confusion_matrix(digits.test_Labels,labels))
示例7: test_qda_regularization
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
def test_qda_regularization():
# the default is reg_param=0. and will cause issues
# when there is a constant variable
clf = QuadraticDiscriminantAnalysis()
with ignore_warnings():
y_pred = clf.fit(X2, y6).predict(X2)
assert np.any(y_pred != y6)
# adding a little regularization fixes the problem
clf = QuadraticDiscriminantAnalysis(reg_param=0.01)
with ignore_warnings():
clf.fit(X2, y6)
y_pred = clf.predict(X2)
assert_array_equal(y_pred, y6)
# Case n_samples_in_a_class < n_features
clf = QuadraticDiscriminantAnalysis(reg_param=0.1)
with ignore_warnings():
clf.fit(X5, y5)
y_pred5 = clf.predict(X5)
assert_array_equal(y_pred5, y5)
示例8: train_DA
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
def train_DA(self, X, y, lda_comp, qda_reg):
'''
Input:
qda_reg - reg_param
lda_comp - n_components
X - data matrix (train_num, feat_num)
y - target labels matrix (train_num, label_num)
Output:
best_clf - best classifier trained (QDA/LDA)
best_score - CV score of best classifier
Find best DA classifier.
'''
n_samples, n_feat = X.shape
cv_folds = 10
kf = KFold(n_samples, cv_folds, shuffle=False)
lda = LinearDiscriminantAnalysis(n_components = lda_comp)
qda = QuadraticDiscriminantAnalysis(reg_param = qda_reg)
score_total_lda = 0 #running total of metric score over all cv runs
score_total_qda = 0 #running total of metric score over all cv runs
for train_index, test_index in kf:
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
lda.fit(X_train, y_train)
cv_pred_lda = lda.predict(X_test)
score_lda = eval(self.metric + '(y_test[:,None], cv_pred_lda[:,None], "' + self.task + '")')
score_total_lda += score_lda
qda.fit(X_train,y_train)
cv_pred_qda = qda.predict(X_test)
score_qda = eval(self.metric + '(y_test[:,None], cv_pred_lda[:,None], "' + self.task + '")')
score_total_qda += score_qda
score_lda = score_total_lda/cv_folds
score_qda = score_total_qda/cv_folds
# We keep the best one
if(score_qda > score_lda):
qda.fit(X,y)
return qda, score_qda
else:
lda.fit(X,y)
return lda, score_lda
示例9: train_test_split
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
plt.ylabel('True label')
plt.xlabel('Predicted label')
#define X y
X, y = data.loc[:,data.columns != 'state'].values, data.loc[:,data.columns == 'state'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
#smoteen
sme = SMOTEENN(random_state=42)
os_X,os_y = sme.fit_sample(X_train,y_train)
#QDA
clf_QDA = QuadraticDiscriminantAnalysis(store_covariances=True)
clf_QDA.fit(os_X, os_y)
y_true, y_pred = y_test, clf_QDA.predict(X_test)
#F1_score, precision, recall, specifity, G score
print "F1_score : %.4g" % metrics.f1_score(y_true, y_pred)
print "Recall : %.4g" % metrics.recall_score(y_true, y_pred)
recall = metrics.recall_score(y_true, y_pred)
print "Precision : %.4g" % metrics.precision_score(y_true, y_pred)
#Compute confusion matrix
cnf_matrix = confusion_matrix(y_test,y_pred)
np.set_printoptions(precision=2)
print "Specifity: " , float(cnf_matrix[0,0])/(cnf_matrix[0,0]+cnf_matrix[0,1])
specifity = float(cnf_matrix[0,0])/(cnf_matrix[0,0]+cnf_matrix[0,1])
print "G score: " , math.sqrt(recall/ specifity)
#Plot non-normalized confusion matrix
示例10: LogisticRegression
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
logreg = LogisticRegression().fit(X_train, y_train)
y_pred = logreg.predict(X_test)
y_pred_train = logreg.predict(X_train)
log_acc = accuracy_score(y_pred, y_test) #0.64 highest
clf = DecisionTreeClassifier().fit(X_train, y_train)
y_pred = clf.predict(X_test)
clf_acc = accuracy_score(y_pred, y_test) #0.61
neigh = KNeighborsClassifier(n_neighbors=13).fit(X_train, y_train)
y_pred = neigh.predict(X_test)
nn_acc = accuracy_score(y_pred, y_test) #0.61
quad = QuadraticDiscriminantAnalysis().fit(X_train, y_train)
y_pred = quad.predict(X_test)
quad_acc = accuracy_score(y_pred, y_test) # 0.19 very low
ldaC = LDA(solver='lsqr', shrinkage='auto').fit(X_train, y_train) #LDA with shrinkage
y_pred = ldaC.predict(X_test)
lda_acc = accuracy_score(y_pred, y_test) #0.58
#########################################
from sklearn.cross_validation import KFold
from sklearn.cross_validation import StratifiedKFold
import matplotlib.pyplot as plt
def calc_params(X, y, clf, param_values, param_name, K, metric = 'accuracy'):
'''This function takes the classfier, the training data and labels, the name of the
parameter to vary, a list of values to vary by, and a number of folds needed for
cross validation and returns a the test and train scores (accuracy or recall) and also
示例11: __init__
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
class road_estimation:
def __init__(self, model_selection):
self._train_data, self._train_targets, self._valid_data, self._valid_targets, self._test_data, self._test_targets = (
data_load()
)
self._model_selection = model_selection
self._classifier = []
def train(self):
if self._model_selection == "svm":
# selected the svc in svm
self._classifier = svm.SVC()
elif self._model_selection == "nb":
self._classifier = GaussianNB()
elif self._model_selection == "knn":
# parameter n_jobs can be set to -1 to enable parallel calculating
self._classifier = KNeighborsClassifier(n_neighbors=7)
elif self._model_selection == "ada":
# Bunch of parameters, n_estimators, learning_rate
self._classifier = AdaBoostClassifier()
elif self._model_selection == "rf":
# many parameters including n_jobs
self._classifier = RandomForestClassifier(max_depth=5, n_estimators=10, max_features=1)
elif self._model_selection == "qda":
# complicated array like parameters, perhaps leave it default
self._classifier = QuadraticDiscriminantAnalysis()
else:
print "Please refer to one classifier"
self._classifier.fit(self._train_data, self._train_targets)
# predict on valid data
prediction_valid = self._classifier.predict(self._valid_data)
# print validation result for selected model.
print (
"Classification report for classifier %s on valid_data:\n%s\n"
% (self._model_selection, metrics.classification_report(self._valid_targets, prediction_valid))
)
def test(self):
# predict on test data
prediction_test = self._classifier.predict(self.test_data)
# print test result for selected model.
print (
"Classification report for classifier %s on test_data:\n%s\n"
% (self._model_selection, metrics.classification_report(self._test_targets, prediction_test))
)
def showPredictionImage(self):
f = Feature()
f.loadImage("um_000000.png")
f.extractFeatures()
fea_matrix = f.getFeaturesVectors()
predict = self._classifier.predict(fea_matrix)
image = np.copy(f.image)
num_superpixels = np.max(f.superpixel) + 1
for i in xrange(0, num_superpixels):
indices = np.where(f.superpixel == i)
if predict[i] == 1:
image[indices[0], indices[1], 0] = 1
image[indices[0], indices[1], 1] = 1
image[indices[0], indices[1], 2] = 0
plt.imshow(image)
plt.show()
# show prediction image with superpixels
plt.imshow(mark_boundaries(image, superpixels))
plt.show()
示例12: LinearDiscriminantAnalysis
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
trans = LinearDiscriminantAnalysis(n_components=3)
trans.fit(X,y)
X = trans.transform(X)
"""
# Split Up Data
x_train,x_valid,y_train,y_valid = train_test_split(X,y,test_size=0.3,random_state=None)
# Train classifier
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
clf = QuadraticDiscriminantAnalysis(reg_param=0.00001)
clf.fit(x_train,y_train)
# Run Predictions
from sklearn.metrics import confusion_matrix, accuracy_score
y_preds = clf.predict(x_valid)
print( confusion_matrix(y_valid,y_preds) );
print( "Accuracy: %f" % (accuracy_score(y_valid,y_preds)) );
f = open('qda_take1.txt', 'w')
f.write( str(confusion_matrix(y_valid,y_preds)) );
f.write( "\nAccuracy: %f" % (accuracy_score(y_valid,y_preds)) );
f.write( "\nclf = QuadraticDiscriminantAnalysis(0.00001)" );
# Now on to final submission
x_final = testing.iloc[:,1:].values
y_final = clf.predict(x_final).reshape([62096,]);
y_final = pd.DataFrame(y_final);
numbahs = testing['id']
df = pd.concat([numbahs,y_final],axis=1)
df.columns = ['id','country']
df.to_csv("qda_take1.csv",index=False)
示例13: QuadraticDiscriminantAnalysis
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
#
# CREATE MODEL
#
###########################################################################
# Define the estimator: quadratic discriminant analysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
qda = QuadraticDiscriminantAnalysis()
qda.fit(training_data[0], training_data[1])
from sklearn.metrics import accuracy_score
# record the best result
accuracies[i] = accuracy_score(test_data[1], qda.predict(test_data[0]))
mean_accuracy = accuracies.mean()
print("\n\nmean accuracy: %f" % mean_accuracy)
###############################################################################
#
# VISUALIZE
#
###############################################################################
import matplotlib.pyplot as plt
mean_accuracies = np.zeros(shape=(n,))
for i in range(n):
mean_accuracies[i] = accuracies[: i + 1].mean()
示例14: range
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
for i in range(9,18):
labels.append(2)
for i in range(18, 27):
labels.append(3)
'''
# Creation of random labels
for i in range(0,27):
labels.append(int(random.random() * 3) + 1)
print (labels)
'''
# QDA model
qda = QuadraticDiscriminantAnalysis()
qda.fit(comps, labels)
# MCC Calculation
y_pred = qda.predict(comps)
#print(labels)
#print(y_pred)
mcc = multimcc(labels,y_pred)
print("MCC="+str(mcc))
'''
# Plotting QDA contour
nx, ny = 200, 100
x_min, x_max = np.amin(comps[:,0]), np.amax(comps[:,0])
y_min, y_max = np.amin(comps[:,1]), np.amax(comps[:,1])
xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx),np.linspace(y_min, y_max, ny))
Z = qda.predict_proba(np.c_[xx.ravel(), yy.ravel()])
Z = Z[:, 1].reshape(xx.shape)
plt.contour(xx, yy, Z, [0.5], linewidths=5, colors = 'k', linestyles = 'dashed')
'''
示例15: Analysis
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import predict [as 别名]
plt.plot(pca.components_.reshape((2,data.shape[0],data.shape[1])))
#plt.plot(pca.explained_variance_, linewidth=2)
#plt.title('Principal Component Analysis (PCA) Feature Assessment')
# Creation of labels
labels = []
for i in range(0,27):
labels.append(1)
for i in range(27,53):
labels.append(2)
# LDA model
lda = QuadraticDiscriminantAnalysis()
lda.fit(comps, labels)
y_pred = lda.predict(comps)
print(labels)
print(y_pred)
mcc = matthews_corrcoef(labels,y_pred)
print("MCC="+str(mcc))
# Plotting LDA contour
nx, ny = 200, 100
x_min, x_max = np.amin(comps[:,0]), np.amax(comps[:,0])
y_min, y_max = np.amin(comps[:,1]), np.amax(comps[:,1])
xx, yy = np.meshgrid(np.linspace(x_min, x_max, nx),np.linspace(y_min, y_max, ny))
Z = lda.predict_proba(np.c_[xx.ravel(), yy.ravel()])
Z = Z[:, 1].reshape(xx.shape)
plt.contour(xx, yy, Z, [0.5], linewidths=5, colors = 'k', linestyles = 'dashed')