本文整理汇总了Python中sklearn.linear_model.Perceptron.predict方法的典型用法代码示例。如果您正苦于以下问题:Python Perceptron.predict方法的具体用法?Python Perceptron.predict怎么用?Python Perceptron.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.linear_model.Perceptron
的用法示例。
在下文中一共展示了Perceptron.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ClassificationPLA
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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)
示例2: PERCEPTRON
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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: Perceptron_1
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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
示例4: run
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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))
示例5: percep
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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: t
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [as 别名]
def t():
# 1
from pandas import read_csv
df = read_csv('w2/perceptron-train.csv', header=None)
dt = read_csv('w2/perceptron-test.csv', header=None)
yf = df[0]
xf = df.drop([0], axis=1)
# print(yf, xf)
yt = dt[0]
xt = dt.drop([0], axis=1)
# print(yt, xt)
# 2
from sklearn.linear_model import Perceptron
clf = Perceptron(random_state=241)
clf.fit(xf, yf)
af1 = clf.score(xf, yf)
at1 = clf.score(xt, yt)
rf = clf.predict(xf)
rt = clf.predict(xt)
# print(list(yf))
# print(pf)
# print(list(yt))
# print(pt)
# 3
from sklearn.metrics import accuracy_score
af = accuracy_score(yf, rf)
at = accuracy_score(yt, rt)
print(af, at)
print(af1, at1)
# 4
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
xfs = scaler.fit_transform(xf)
xts = scaler.transform(xt)
clf.fit(xfs, yf)
afs1 = clf.score(xfs, yf)
ats1 = clf.score(xts, yt)
pfs = clf.predict(xfs)
pts = clf.predict(xts)
afs = accuracy_score(yf, pfs)
ats = accuracy_score(yt, pts)
print(afs, ats)
print(afs1, ats1)
pf('5', round(ats - at, 3))
示例7: main
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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)
示例8: PerceptronModel
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [as 别名]
class PerceptronModel(BaseModel):
def __init__(self, cached_features):
BaseModel.__init__(self, cached_features)
self.model = Perceptron(penalty="l2", random_state=1)
def _predict_internal(self, X_test):
return self.model.predict(X_test)
示例9: solve
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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 predict [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: t1
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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)
示例12: get_accuracy
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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
示例13: perceptron_classifier
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [as 别名]
def perceptron_classifier(data_train, data_test):
# Load train and test data sets
X_train = data_train.iloc[:, 1:].values
y_train = data_train.iloc[:, :1].values.ravel()
X_test = data_test.iloc[:, 1:].values
y_test = data_test.iloc[:, :1].values.ravel()
# Init Perceptron
clf = Perceptron(random_state=241)
# --- Perceptron w/o normalization of Training Data Set ---
# Fit Perceptron linear model using training data
clf.fit(X_train, y_train)
# Use the model to predict test data
y_test_prediction = clf.predict(X_test)
# Calculate accuracy:
accuracy_notnorm = metrics.accuracy_score(y_test, y_test_prediction)
# --- Perceptron w/ normalization of Training Data Set ---
# feature scaling (standardization/normalization)
scaler = preprocessing.StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Fit Perceptron using Training Set and predict results for tTest Set
clf.fit(X_train_scaled, y_train)
y_test_prediction = clf.predict(X_test_scaled)
accuracy_norm = metrics.accuracy_score(y_test, y_test_prediction)
# Note [FEATURE SCALING]:
# You MUST use fit_transform() over Training Set only.
# The scaler will compute necessary statistics like std_dev and mean [aka 'fit']
# and normalize Training Set [aka 'transform']
# But for the Test Set you must not fit the scaler again!
# Just re-use existing statistics and normalize the Test Set using transform() w/o fitting.
print('Accuracy (non-normalized):', accuracy_notnorm)
print('Accuracy (normalized):', accuracy_norm)
diff = accuracy_norm - accuracy_notnorm
print('Diff:', diff)
return diff
示例14: neural_net
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [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
示例15: test
# 需要导入模块: from sklearn.linear_model import Perceptron [as 别名]
# 或者: from sklearn.linear_model.Perceptron import predict [as 别名]
def test():
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: %s" % predictions)
print("Accuracy: %s" % accuracy_score(y, predictions))