本文整理汇总了Python中sklearn.linear_model.Perceptron.fit方法的典型用法代码示例。如果您正苦于以下问题:Python Perceptron.fit方法的具体用法?Python Perceptron.fit怎么用?Python Perceptron.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.Perceptron
的用法示例。
在下文中一共展示了Perceptron.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PerceptronClassifier
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
class PerceptronClassifier(AbstractSKLearnClassifier):
def __init__(self):
AbstractSKLearnClassifier.__init__(self)
self.model = False
def set_label_encoder(self, labels):
AbstractSKLearnClassifier.set_label_encoder(self, labels)
def return_label_encoding(self, labels):
return AbstractSKLearnClassifier.return_label_encoding(self, labels)
def train_classifier(self, trainvectors, labels, alpha='1.0', iterations=10, jobs=1, v=2):
iterations = int(iterations)
jobs = int(jobs)
if alpha == 'search':
paramsearch = GridSearchCV(estimator=Perceptron(), param_grid=dict(alpha=numpy.linspace(0,2,20)[1:],n_iter=[iterations]), n_jobs=jobs)
paramsearch.fit(trainvectors,labels)
alpha = paramsearch.best_estimator_.alpha
else:
alpha = float(alpha)
self.model = Perceptron(alpha=alpha,n_iter=iterations,n_jobs=jobs)
self.model.fit(trainvectors, labels)
def return_classifier(self):
return self.model
def return_model_insights(self,vocab=False):
model_insights = []
return model_insights
示例2: PERCEPTRON
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def PERCEPTRON(data_train, data_train_vectors, data_test_vectors, **kwargs):
# Implementing classification model- using Perceptron
clf_p = Perceptron()
clf_p.fit(data_train_vectors, data_train.target)
y_pred = clf_p.predict(data_test_vectors)
return y_pred
示例3: run
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def run(self):
"""
Пуск задачи
"""
train_data = pd.read_csv(self.param.get('train'))
test_data = pd.read_csv(self.param.get('test'))
X_train = train_data[['1', '2']]
y_train = train_data['0']
X_test = test_data[['1', '2']]
y_test = test_data['0']
if self.param.get('scale') is True:
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
perceptron = Perceptron(random_state=241)
perceptron.fit(X_train, y_train)
predictions = perceptron.predict(X_test)
accuracy = accuracy_score(y_test, predictions)
with self.output().open('w') as output:
output.write(str(accuracy))
示例4: Perceptron_1
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def Perceptron_1(train_predictors,test_predictors,train_target,test_target):
clf = Perceptron()
clf.fit(train_predictors,train_target)
predicted = clf.predict(test_predictors)
accuracy = accuracy_score(test_target, predicted)
print "Accuracy for Linear Model Perceptron: "+str(accuracy)
return accuracy,predicted
示例5: percep
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def percep(X_tr, y_tr, X_te):
clf = Perceptron(n_iter = 1000)
X_tr_aug = add_dummy_feature(X_tr)
X_te_aug = add_dummy_feature(X_te)
clf.fit(X_tr_aug, y_tr)
y_pred = clf.predict(X_te_aug)
return y_pred
示例6: perceptron_histo
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def perceptron_histo():
"Interprétation des images comme histogrammes de couleurs et classification via le Perceptron"
alphas = np.arange(0.01,1.01,0.1)
best=np.zeros(4)
_, data, target, _ = utils.chargementHistogrammesImages(mer,ailleurs,1,-1)
X_train,X_test,Y_train,Y_test=train_test_split(data,target,test_size=0.3,random_state=random.seed())
for iterations in range(1,5):
for a in alphas:
start_time = time.time()
p = Perceptron(alpha=a, n_iter=iterations, random_state=random.seed(), n_jobs=-1)
x1=np.array(X_train)
x2=np.array(X_test)
p.fit(X=x1, y=Y_train)
score = p.score(x2,Y_test)
end_time = time.time()
if score>best[0]:
best[0] = score
best[1] = a
best[2] = iterations
best[3] = end_time-start_time
print("| Perceptron simple | V.Histo | alpha={:1.2f} iterations={:1.0f} | {:10.3f}ms | {:1.3f} |".format(best[1],best[2],best[3]*1000,best[0]))
示例7: ClassificationPLA
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
class ClassificationPLA(ClassficationBase.ClassificationBase):
def __init__(self, isTrain, isOutlierRemoval=0):
super(ClassificationPLA, self).__init__(isTrain, isOutlierRemoval)
# data preprocessing
self.dataPreprocessing()
# PLA object
self.clf = Perceptron()
def dataPreprocessing(self):
# deal with unbalanced data
self.dealingUnbalancedData()
# Standardization
#self.Standardization()
def training(self):
# train the K Nearest Neighbors model
self.clf.fit(self.X_train, self.y_train.ravel())
def predict(self):
# predict the test data
self.y_pred = self.clf.predict(self.X_test)
# print the error rate
self.y_pred = self.y_pred.reshape((self.y_pred.shape[0], 1))
err = 1 - np.sum(self.y_test == self.y_pred) * 1.0 / self.y_pred.shape[0]
print "Error rate: {}".format(err)
示例8: perceptron_vecteur
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def perceptron_vecteur():
"Interprétation des images comme vecteurs de pixels et classification via le Perceptron"
alphas = np.arange(0.01,1.01,0.1)
best=np.zeros(5)
for npix in range(50,200,50):
_, data, target, _ = utils.chargementVecteursImages(mer,ailleurs,1,-1,npix)
X_train,X_test,Y_train,Y_test=train_test_split(data,target,test_size=0.3,random_state=random.seed())
for iterations in range(1,5):
for a in alphas:
start_time = time.time()
p = Perceptron(alpha=a, n_iter=iterations, random_state=random.seed(), n_jobs=-1)
#X_train, etc, sont des tableaux à 3 dimensiosn par défaut, (93,1,30000) par exemple, qu'il faut remmener en 2 dimensions
x1=np.array(X_train)
x1 = np.reshape(x1, (x1.shape[0],x1.shape[2]))
x2=np.array(X_test)
x2 = np.reshape(x2, (x2.shape[0],x2.shape[2]))
p.fit(X=x1, y=Y_train)
score = p.score(x2,Y_test)
end_time = time.time()
if score>best[0]:
best[0] = score
best[1] = a
best[2] = iterations
best[3] = end_time-start_time
best[4] = npix
print("| Perceptron simple | V.Pix {:4.0f} | alpha={:1.2f} iterations={:1.0f} | {:10.3f}ms | {:1.3f} |".format(best[4],best[1],best[2],best[3]*1000,best[0]))
示例9: solve
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def solve(train_set_x, train_set_y, test_set_x, test_set_y):
clf = Perceptron(random_state=241)
clf.fit(X=train_set_x, y=train_set_y)
prediction = clf.predict(test_set_x)
accuracy = accuracy_score(test_set_y, prediction)
return accuracy
示例10: classify_perceptron
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def classify_perceptron():
print "perceptron"
(X_train, y_train), (X_test, y_test) = util.load_all_feat()
print "original X_train shape", X_train.shape
clf = Perceptron()
clf.fit(X_train, y_train)
pred = clf.predict(X_test)
print "accuracy score:", accuracy_score(y_test, pred)
示例11: train_data_perceptron
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def train_data_perceptron( tup, penalty ):
df, features, label = tup
percep = Perceptron( penalty = penalty, fit_intercept = True, eta0 = ETA, n_iter = CYCLES, n_jobs = 2 )
percep.fit( df[features], df[label] )
return percep
示例12: main
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def main():
iris = load_iris()
X = iris.data[:, (2, 3)] # 花弁の長さ、花弁の幅
y = (iris.target == 0.).astype(np.int32)
perceptron_classifier = Perceptron(random_state=42)
perceptron_classifier.fit(X, y)
y_prediction = perceptron_classifier.predict([[2, 0.5]])
print(y_prediction)
示例13: t1
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def t1():
from sklearn.linear_model import Perceptron
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([0, 1, 0])
clf = Perceptron()
clf.fit(X, y)
predictions = clf.predict(X)
print(predictions)
示例14: get_accuracy
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def get_accuracy(_data_train_features, _data_train_labels, _data_test_features, _data_test_labels):
# Обучите персептрон со стандартными параметрами и random_state=241.
clf = Perceptron(random_state=241, shuffle=True)
clf.fit(_data_train_features, numpy.ravel(_data_train_labels))
# Подсчитайте качество (долю правильно классифицированных объектов, accuracy)
# полученного классификатора на тестовой выборке.
predictions = clf.predict(_data_test_features)
score = accuracy_score(_data_test_labels, predictions)
return score
示例15: neural_net
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import fit [as 别名]
def neural_net(train, test):
y = []
xTrain, yTrain = loadData(train)
xTest, yTest = loadData(test)
nN = Perceptron()
nN.fit(xTrain, yTrain)
y = nN.predict(xTest)
testError = 1 - nN.score(xTest, yTest)
print 'Test error: ' , testError
return y