本文整理汇总了Python中sklearn.qda.QDA类的典型用法代码示例。如果您正苦于以下问题:Python QDA类的具体用法?Python QDA怎么用?Python QDA使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QDA类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __init__
class RegularizedQDA:
"""
Three types of regularization are possible:
- regularized the covariance of a class toward the
average variance within that class
- regularize the covariance of a class toward the
pooled covariance across all classes
- add some constant amount of variance to each feature
"""
def __init__(self, avg_weight = 0.1, pooled_weight = 0, extra_variance = 0):
self.avg_weight = avg_weight
self.pooled_weight = pooled_weight
self.extra_variance = extra_variance
self.model = QDA()
def fit(self, X, Y):
self.model.fit(X,Y)
I = np.eye(X.shape[1])
a = self.avg_weight
p = self.pooled_weight
ev = self.extra_variance
original_weight = 1.0 - a - p
scaled_pooled_cov = p * np.cov(X.T)
assert scaled_pooled_cov.shape == I.shape
assert all([C.shape == I.shape for C in self.model.rotations])
self.model.rotations = \
[original_weight * C + \
a * np.mean(np.diag(C)) * I + \
scaled_pooled_cov + ev * I \
for C in self.model.rotations]
def predict(self, X):
return self.model.predict(X)
开发者ID:iskandr,项目名称:data-experiments,代码行数:33,代码来源:regularized.py
示例2: test_all_methods
def test_all_methods(self):
x_cols = ["Lag2"]
formula = "Direction~Lag2"
# print self.df.shape[0]
train_data = self.df.ix[(self.df["Year"] >= 1990) & (self.df["Year"] <= 2008), :]
# print train_data.shape[0]
""" (d) logistic"""
model = smf.glm(formula, data=train_data, family=sm.families.Binomial())
result = model.fit()
test_data = self.df.ix[self.df["Year"] > 2008, :]
probs = Series(result.predict(sm.add_constant(test_data[["Lag2"]])))
pred_values = probs.map(lambda x: "Down" if x > 0.5 else "Up")
tp.output_table(pred_values.values, test_data[self.y_col].values)
train_X = train_data[x_cols].values
train_y = train_data[self.y_col].values
test_X = test_data[x_cols].values
test_y = test_data[self.y_col].values
""" (e) LDA """
lda_res = LDA().fit(train_X, train_y)
pred_y = lda_res.predict(test_X)
tp.output_table(pred_y, test_y)
""" (f) QDA """
qda_res = QDA().fit(train_X, train_y)
pred_y = qda_res.predict(test_X)
tp.output_table(pred_y, test_y)
""" (g) KNN """
clf = neighbors.KNeighborsClassifier(1, weights="uniform")
clf.fit(train_X, train_y)
pred_y = clf.predict(test_X)
tp.output_table(pred_y, test_y)
""" (h) logistic and LDA """
""" (i) Is the purpose of the last question going through all methods with no direction?"""
开发者ID:Aran00,项目名称:ISIRExerciseCode,代码行数:33,代码来源:Exec10.py
示例3: SNPForecastingStrategy
class SNPForecastingStrategy(Strategy):
def __init__(self,symbol,bars):
self.symbol=symbol
self.bars=bars
self.create_periods()
self.fit_model()
def create_periods(self):
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):
snpret=create_lagged_series(self.symbol,self.start_train,self.end_period,lags=5)
X=snpret[['Lag1','Lag2']]
Y=snpret['Direction']
X_train=X[X.index<self.start_test]
Y_train=Y[Y.index<self.start_test]
self.predictors=X[X.index>=self.start_test]
self.model=QDA()
self.model.fit(X_train,Y_train)
def generate_signals(self):
signals=pd.DataFrame(index=self.bars.index)
signals['signal']=0.0
signals['signal']=self.model.predict(self.predictors)
signals['signal'][0:5]=0.0
signals['positions']=signals['signal'].diff()
return signals
开发者ID:wzhang79,项目名称:python,代码行数:29,代码来源:snp_forecast.py
示例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 = QDA()
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
开发者ID:maitreyim,项目名称:PyStuff,代码行数:58,代码来源:snp.py
示例5: performSVMClass
def performSVMClass(X_train, y_train, X_test, y_test, parameters, fout, savemodel):
"""
SVM binary classification
"""
clf = QDA()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
return accuracy
开发者ID:jko0531,项目名称:Machine-Learning,代码行数:9,代码来源:prediction.py
示例6: performQDAClass
def performQDAClass(X_train, y_train, X_test, y_test):
"""
Gradient Tree Boosting binary Classification
"""
clf = QDA()
clf.fit(X_train, y_train)
accuracy = clf.score(X_test, y_test)
#auc = roc_auc_score(y_test, clf.predict(X_test))
return accuracy
开发者ID:FraPochetti,项目名称:StocksProject,代码行数:9,代码来源:functions.py
示例7: qda
def qda(data,labels,n,v_type):
train_data,train_labels,test_data,test_labels = split_data(data,labels,v_type)
clf = QDA()
clf.fit(train_data, train_labels)
y_pred = clf.predict(test_data)
pure_accuracy_rate = len([y_pred[x] for x in range(len(y_pred)) if y_pred[x] == test_labels[x]])/float(len(test_labels))
report = classification_report(y_pred, test_labels, target_names=rock_names)
cm = confusion_matrix(test_labels, y_pred)
return pure_accuracy_rate,report,y_pred,test_labels,test_data,clf,cm,"QDA"
开发者ID:evanmosseri,项目名称:The-Classification-of-Igneous-Rocks-Through-Oxide-Components,代码行数:10,代码来源:rocksep_utils.py
示例8: QDA
def QDA(self,membership,group_labels=None,std=3,ellipses=True,dpi=300,fontsize=10,MD=False,
legend=False, numbered=False,of='pdf'):
self.type = 'QDA'
membership = membership.astype(int)
qda = QDA()
self.fit = qda.fit(self.data, membership).predict(self.data)
if ellipses:
self.getEllipses(std,membership)
self.PlotXDA(membership,group_labels=group_labels,std=std,ellipses=ellipses,dpi=dpi,
fontsize=fontsize,MD=MD,legend=legend,numbered=numbered,of=of)
self.Store()
开发者ID:LabBlouin,项目名称:LabBlouinTools,代码行数:11,代码来源:Ordination.py
示例9: qda_predict
def qda_predict(train_data, test_data, train_cat, xx, yy):
# QDA CLASSIFIER
qda_classifier = QDA()
qda_fit = qda_classifier.fit(train_data, train_cat)
predicted = qda_fit.predict(test_data)
contour = qda_fit.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
contour = contour.reshape(xx.shape)
return predicted, contour
开发者ID:xykovax,项目名称:playground,代码行数:11,代码来源:demo.py
示例10: get_QDA
def get_QDA(Xtrain, Xtest, Ytrain, Ytest):
qda = QDA()
qda.fit(Xtrain,Ytrain)
# predLabels = qda.predict(Xtest)
# print("Classification Rate Test QDA: " + str(np.mean(Ytest==predLabels)*100) + " %")
scores = np.empty((4))
scores[0] = qda.score(Xtrain,Ytrain)
scores[1] = qda.score(Xtest,Ytest)
print('QDA, train: {0:.02f}% '.format(scores[0]*100))
print('QDA, test: {0:.02f}% '.format(scores[1]*100))
return qda
开发者ID:manuwhs,项目名称:Trapyng,代码行数:11,代码来源:system_modules.py
示例11: train_qda
def train_qda(X, y, priors=None, reg_param=0.0):
"""
Builds a quadratic discriminant analysis model
Returns:
clf: Fitted QDA model
"""
clf = QDA(priors=priors,
reg_param=reg_param)
clf = clf.fit(X,y)
print 'Quadratic Discriminant Analysis completed!'
return clf
开发者ID:LatencyTDH,项目名称:Pykit-Learn,代码行数:12,代码来源:classification_utils.py
示例12: QuadraticDiscriminantAnalysis
def QuadraticDiscriminantAnalysis(x_train, y_train, x_cv, y_cv):
"""
Quadratic Discriminant Analysis Classifier
"""
print "Quadratic Discriminant Analysis"
clfr = QDA()
clfr.fit(x_train, y_train)
#print 'Accuracy in training set: %f' % clfr.score(x_train, y_train)
#if y_cv != None:
#print 'Accuracy in cv set: %f' % clfr.score(x_cv, y_cv)
return clfr
开发者ID:tbs1980,项目名称:Kaggle_DecMeg2014,代码行数:12,代码来源:Classify.py
示例13: train_classifier
def train_classifier(xTrain_s, yTrain_s, kwargs):
"""
Train a naive baise classifier on xTrain and yTrain and return the trained
classifier
"""
if type(xTrain_s) != list:
classifier_s = QDA(**kwargs)
classifier_s.fit(xTrain_s, yTrain_s)
else:
classifier_s = train_classifier_8(xTrain_s, yTrain_s, kwargs)
return classifier_s
开发者ID:jbRegli,项目名称:Higgs,代码行数:13,代码来源:qda.py
示例14: QDA_onFullDataset
def QDA_onFullDataset():
#Parsing Full training dataset
XFull = common.parseFile('../UCI HAR Dataset/train/X_train.txt')
YFull = common.parseFile('../UCI HAR Dataset/train/y_train.txt')
#Parsing Full testing dataset
XFullTest = common.parseFile('../UCI HAR Dataset/test/X_test.txt')
YFullTest = common.parseFile('../UCI HAR Dataset/test/y_test.txt')
#Fitting data using QDA classifier
clf = QDA()
clf.fit(XFull, YFull.flatten())
#Testing the results
precision,recall,fscore = common.checkAccuracy(clf.predict(XFullTest),YFullTest,[1,2,3,4,5,6])
print fscore
开发者ID:Ninja91,项目名称:Human-Activity-Recognition,代码行数:16,代码来源:QDA.py
示例15: fit_model
def fit_model(self):
"""Fits a Quadratic Discriminat Analyser to the US
sock market index (^GPSC in Yahoo)."""
# Create a laggged 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 value, 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 prediciting factors for use
# in direction forecasting.
self.predictors = X[X.index >= self.start_test]
# Create the Quadractic Discriminant Analysis model
# and the forcasting strategy
self.model = QDA()
self.model.fit(X_train, y_train)
开发者ID:JeffreyJackovich,项目名称:intro-to-algorithmic-trading,代码行数:25,代码来源:snp_forecast.py
示例16: train
def train(self, classification_data, indices=None, settings_name=None, **kwargs):
super(QDAClassifier, self).train(classification_data, indices, settings_name, **kwargs)
indices = self.settings['indices']
self.qda = QDA(**self.classifier_kwargs)
self.qda.fit(classification_data.data[:, indices], classification_data.are_hurr_actual)
return self
开发者ID:markmuetz,项目名称:stormtracks,代码行数:8,代码来源:classification.py
示例17: fit_model
def fit_model(self):
snpret=create_lagged_series(self.symbol,self.start_train,self.end_period,lags=5)
X=snpret[['Lag1','Lag2']]
Y=snpret['Direction']
X_train=X[X.index<self.start_test]
Y_train=Y[Y.index<self.start_test]
self.predictors=X[X.index>=self.start_test]
self.model=QDA()
self.model.fit(X_train,Y_train)
开发者ID:wzhang79,项目名称:python,代码行数:9,代码来源:snp_forecast.py
示例18: runQDA
def runQDA(fileNamaParam, trainizingSizeParam):
# what percent will you use ?
testSplitSize = 1.0 - trainizingSizeParam
testAndTrainData = IO_.giveTestAndTrainingData(fileNamaParam)
trainData = testAndTrainData[0]
testData = testAndTrainData[1]
### classification
## get the test and training sets
featureSpace_train, featureSpace_test, vScore_train, vScore_test = cross_validation.train_test_split(trainData, testData, test_size=testSplitSize, random_state=0)
## fire up the model
theQDAModel = QDA()
theQDAModel.fit(featureSpace_train, vScore_train)
thePredictedScores = theQDAModel.predict(featureSpace_test)
#print "The original vector: "
#print vScore_test
#print "The predicted score vector: "
#print thePredictedScores
evalClassifier(vScore_test, thePredictedScores)
开发者ID:Pikomonto,项目名称:DataAnalysisAndLearning,代码行数:18,代码来源:classifiers.py
示例19: 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
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 = QDA()
model.fit(X_train, y_train)
return model
开发者ID:FayolChang,项目名称:mlp,代码行数:19,代码来源:snp_forecast.py
示例20: QDAResult3D
def QDAResult3D():
norTrainNum, nor_isTraining = randTestData(t_data_perc, norDataNum)
cnTrainNum, cn_isTraining = randTestData(t_data_perc, cnDataNum)
isTraining =np.hstack((nor_isTraining, cn_isTraining))
#Training QDA classifier
clf = QDA()
trained_clf = clf.fit(train_data[isTraining], labels[isTraining])
#Using the remaining data for testing
normal_pred = trained_clf.predict(normal_pt[nor_isTraining == False])
trueneg_n = (normal_pred == 0).sum()
specificity = trueneg_n/int(norDataNum - norTrainNum)
cancer_pred = trained_clf.predict(cancer_pt[cn_isTraining == False])
truepos_n = (cancer_pred == 1).sum()
sensitivity = truepos_n/int(cnDataNum - cnTrainNum)
return sensitivity, specificity
开发者ID:Ryan05799,项目名称:Ipython,代码行数:20,代码来源:QDA3DCrossValidate.py
注:本文中的sklearn.qda.QDA类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论