本文整理汇总了Python中sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis类的典型用法代码示例。如果您正苦于以下问题:Python QuadraticDiscriminantAnalysis类的具体用法?Python QuadraticDiscriminantAnalysis怎么用?Python QuadraticDiscriminantAnalysis使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QuadraticDiscriminantAnalysis类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: QuadraticDiscriminantAnalysiscls
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())
示例2: create_symbol_forecast_model
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: train
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))
)
示例4: SNPForecastingStrategy
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: test_qda_priors
def test_qda_priors():
clf = QuadraticDiscriminantAnalysis()
y_pred = clf.fit(X6, y6).predict(X6)
n_pos = np.sum(y_pred == 2)
neg = 1e-10
clf = QuadraticDiscriminantAnalysis(priors=np.array([neg, 1 - neg]))
y_pred = clf.fit(X6, y6).predict(X6)
n_pos2 = np.sum(y_pred == 2)
assert_greater(n_pos2, n_pos)
示例6: QD
def QD(pth):
train_desc=np.load(pth+'/training_features.npy')
nbr_occurences = np.sum( (train_desc > 0) * 1, axis = 0)
idf = np.array(np.log((1.0*len(image_paths)+1) / (1.0*nbr_occurences + 1)), 'float32')
# Scaling the words
stdSlr = StandardScaler().fit(train_desc)
train_desc = stdSlr.transform(train_desc)
modelQD=QuadraticDiscriminantAnalysis()
modelQD.fit(train_desc,np.array(train_labels))
joblib.dump((modelQD, img_classes, stdSlr), pth+"/qd-bof.pkl", compress=3)
test(pth, "qd-")
示例7: crossValidate
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
示例8: test
def test():
for i, (X, y) in enumerate([dataset_fixed_cov(), dataset_cov()]):
# Linear Discriminant Analysis
lda = LinearDiscriminantAnalysis(solver="svd", store_covariance=True)
y_pred = lda.fit(X, y).predict(X)
splot = plot_data(lda, X, y, y_pred, fig_index=2 * i + 1)
plot_lda_cov(lda, splot)
plt.axis('tight')
# Quadratic Discriminant Analysis
qda = QuadraticDiscriminantAnalysis(store_covariances=True)
y_pred = qda.fit(X, y).predict(X)
splot = plot_data(qda, X, y, y_pred, fig_index=2 * i + 2)
plot_qda_cov(qda, splot)
plt.axis('tight')
plt.suptitle('Linear Discriminant Analysis vs Quadratic Discriminant Analysis')
plt.show()
示例9: QuadraticDiscriminantAnalysisPredictor
class QuadraticDiscriminantAnalysisPredictor(PredictorBase):
'''
Quadratic Discriminant Analysis
'''
def __init__(self):
self.clf = QuadraticDiscriminantAnalysis()
def fit(self, X_train, y_train):
self.clf.fit(X_train, y_train)
def predict(self, X_test):
predictions = self.clf.predict_proba(X_test)
predictions_df = self.bundle_predictions(predictions)
return predictions_df
def get_k_best_k(self):
return 4
开发者ID:paul-reiners,项目名称:kaggle-shelter-animal-outcomes,代码行数:19,代码来源:quadratic_descriminant_analysis_predictor.py
示例10: test_qda_regularization
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)
示例11: set_up_classifier
def set_up_classifier(self):
historic_data = self.get_data()
# Key is to identify a trend (use close for now)
historic_data['return_5_timeframe'] = np.log(historic_data['Close'] / historic_data['Close'].shift(5)) * 100
historic_data.fillna(0.0001, inplace=True)
historic_data['vol_normalised'] = normalise_data(historic_data['Volume'])
# Bucket Return
def bucket_return(x, col):
if 0 < x[col] < 0.02:
return 1
if 0.02 < x[col] < 0.1:
return 2
if x[col] > 0.1:
return 3
if 0 > x[col] > -0.02:
return -1
if -0.02 > x[col] > -0.1:
return -2
if x[col] < -0.1:
return -3
else:
return 0
historic_data['Return'] = historic_data.apply(bucket_return, axis=1, args=['return_5_timeframe'])
historic_data['Move'] = historic_data['Close'] - historic_data['Open']
# X as predictor values, with Y as the response
x = historic_data[["Move"]]
y = historic_data["Return"]
model = QuadraticDiscriminantAnalysis()
model.fit(x, y)
return model
示例12: train_DA
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
示例13: fit_model
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)
示例14: test_qda
def test_qda():
# QDA classification.
# This checks that QDA implements fit and predict and returns
# correct values for a simple toy dataset.
clf = QuadraticDiscriminantAnalysis()
y_pred = clf.fit(X6, y6).predict(X6)
assert_array_equal(y_pred, y6)
# Assure that it works with 1D data
y_pred1 = clf.fit(X7, y6).predict(X7)
assert_array_equal(y_pred1, y6)
# Test probas estimates
y_proba_pred1 = clf.predict_proba(X7)
assert_array_equal((y_proba_pred1[:, 1] > 0.5) + 1, y6)
y_log_proba_pred1 = clf.predict_log_proba(X7)
assert_array_almost_equal(np.exp(y_log_proba_pred1), y_proba_pred1, 8)
y_pred3 = clf.fit(X6, y7).predict(X6)
# QDA shouldn't be able to separate those
assert np.any(y_pred3 != y7)
# Classes should have at least 2 elements
assert_raises(ValueError, clf.fit, X6, y4)
示例15: train_test_split
plt.tight_layout()
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)