本文整理汇总了Python中sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis.fit方法的典型用法代码示例。如果您正苦于以下问题:Python QuadraticDiscriminantAnalysis.fit方法的具体用法?Python QuadraticDiscriminantAnalysis.fit怎么用?Python QuadraticDiscriminantAnalysis.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis
的用法示例。
在下文中一共展示了QuadraticDiscriminantAnalysis.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: QuadraticDiscriminantAnalysiscls
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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())
示例2: create_symbol_forecast_model
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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: SNPForecastingStrategy
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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
示例4: doQDA
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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
示例5: confusion
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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))
示例6: test_qda_priors
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [as 别名]
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)
示例7: QD
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [as 别名]
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-")
示例8: get_QDA
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [as 别名]
def get_QDA(Xtrain, Ytrain, Xtest = None , Ytest = None, verbose = 0):
qda = QDA()
qda.fit(Xtrain,Ytrain)
scores = np.empty((2))
if (verbose == 1):
scores[0] = qda.score(Xtrain,Ytrain)
print('QDA, train: {0:.02f}% '.format(scores[0]*100))
if (type(Xtest) != type(None)):
scores[1] = qda.score(Xtest,Ytest)
print('QDA, test: {0:.02f}% '.format(scores[1]*100))
return qda
示例9: crossValidate
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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
示例10: train_DA
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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
示例11: QuadraticDiscriminantAnalysisPredictor
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [as 别名]
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,代码行数:21,代码来源:quadratic_descriminant_analysis_predictor.py
示例12: test_qda_regularization
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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)
示例13: create_symbol_forecast_model
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [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"]
# Skip days with NaN
skip_till_date = self.model_start_date + relativedelta(days=3)
X = X[X.index > skip_till_date]
y = y[y.index > skip_till_date]
logging.debug(snpret[snpret.index > skip_till_date])
model = QDA()
model.fit(X, y)
return model
示例14: test_qda
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [as 别名]
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: set_up_classifier
# 需要导入模块: from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis [as 别名]
# 或者: from sklearn.discriminant_analysis.QuadraticDiscriminantAnalysis import fit [as 别名]
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