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
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
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))
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
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
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]))
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)
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]))
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
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)
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
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)
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)
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
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
def perceptron(trainingData,trainingLabels): """ Implements a linear perceptron model as the machine learning algorithm. """ from sklearn.linear_model import Perceptron clf = Perceptron() clf.fit(trainingData,trainingLabels) print "Perceptron has been generated with a training set size of",len(trainingLabels) return clf
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))
def test_perceptron_correctness(): y_bin = y.copy() y_bin[y != 1] = -1 clf1 = MyPerceptron(n_iter=2) clf1.fit(X, y_bin) clf2 = Perceptron(n_iter=2) clf2.fit(X, y_bin) assert_array_almost_equal(clf1.w, clf2.coef_.ravel())
def linear_train(features_train, target_train): data_f = pandas.read_csv(features_train, header=None, sep=';') features = data_f.iloc[:, 1:] features = scale(features) data_t = pandas.read_csv(target_train, header=None, sep=';') target = data_t.iloc[:, 1] perc = Perceptron(random_state=242) perc.fit(features, target) return perc
def neural_net(train, test): y = [] trainY, trainX = loadData(train) testY, testX = loadData(test) neuralNet = Perceptron() neuralNet.fit(trainX, trainY) y = neuralNet.predict(testX) testError = 1 - neuralNet.score(testX, testY) print 'Test error: ' + str(testError) return y
def neural_net(): Xtrain,ytrain,Xtest,ytest = getSplitData() Xtrain, Xtest = getScaledData(Xtrain, Xtest) ntest = Xtest.shape[0] #Your code here clf = Perceptron() clf.fit(Xtrain, ytrain) yPredict = clf.predict(Xtest) #print "parameter: n_neighbors = ",n print "neural_net classification accuracy: ", accuracy_score(ytest,yPredict)
def main(): start = time.time() print "Reading train data and its features from: " + train_file data = cu.get_dataframe(train_file) global fea fea = features.extract_features(feature_names,data) percep = Perceptron(penalty=None, alpha=0.0001, fit_intercept=False, n_iter=5, shuffle=False, verbose=1, eta0=1.0, n_jobs=-1, seed=0, class_weight="auto", warm_start=False) X = [] for i in data["OwnerUndeletedAnswerCountAtPostTime"]: X.append([i]) # Must be array type object. Strings must be converted to # to integer values, otherwise fit method raises ValueError global y y = [] print "Collecting statuses" for element in data["OpenStatus"]: for index, status in enumerate(ques_status): if element == status: y.append(index) print "Fitting" percep.fit(fea, y) '''Make sure you have the up to date version of sklearn; v0.12 has the predict_proba method; http://scikit-learn.org/0.11/install.html ''' print "Reading test data and features" test_data = cu.get_dataframe(test_file) test_fea = features.extract_features(feature_names,test_data) print "Making predictions" global probs #probs = percep.predict_proba(test_fea) # only available for binary classification probs = percep.predict(test_fea) # shape of probs is [n_samples] # convert probs to shape [n_samples,n_classes] probs = np.resize(probs, (len(probs) / 5, 5)) #if is_full_train_set == 0: # print("Calculating priors and updating posteriors") # new_priors = cu.get_priors(full_train_file) # old_priors = cu.get_priors(train_file) # probs = cu.cap_and_update_priors(old_priors, probs, new_priors, 0.001) print "writing submission to " + submission_file cu.write_submission(submission_file, probs) finish = time.time() print "completed in %0.4f seconds" % (finish-start)
def train(im_features, image_classes): # Train the Perceptron clf = Perceptron(n_iter=100, eta0=0.1) clf.fit(im_features, np.array(image_classes)) n_folds = 10 kFoldScore = evaluate_cross_validation(clf, im_features, np.array(image_classes), n_folds) #print 'SVM Score:',clf.score(im_features, np.array(image_classes)) #print 'SVM Score:', kFoldScore return clf
def __test_perceptron(self, normalized): clf = Perceptron() X_train = self.train_data.iloc[:, 1:] y_train = self.train_data.iloc[:, 0] X_test = self.test_data.iloc[:, 1:] y_test = self.test_data.iloc[:, 0] if normalized: scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) clf.fit(X_train, y_train) predictions = clf.predict(X_test) return accuracy_score(y_test, predictions)
def test_model(training_data, testing_data, word2vec_model): v = DictVectorizer() train_features, train_labels = build_features(training_data, word2vec_model, v, 'train') test_features, test_labels = build_features(testing_data, word2vec_model, v) # create the perceptron model model = Perceptron(n_iter = 5) # fit the model to the training data model.fit(train_features, train_labels) # get the accuracy on the testing data accuracy = model.score(test_features, test_labels) return accuracy
def runTrial(numberOfTestPoints, iterationLimit, showChart = False): x1, y1, x2, y2, points = generatePoints(numberOfTestPoints) pclf = Perceptron() clf = SVC(C = 1000, kernel = 'linear') sample = np.array(points) X = np.c_[sample[:,1], sample[:,2]] y = sample[:,3] #print(y) pclf.fit(X,y) clf.fit(X,y) iterations, w = train(points, iterationLimit) #print("weights ", w) #print("coefficients", pclf.coef_) errorProb = findErrorProbability(x1,y1,x2,y2,w, 50000, clf, pclf) if showChart: if iterations == iterationLimit: print( "No solution found in " + str(iterations) + " iterations!") print( "Iterations: " + str(iterations) + ' | Weights: ' + str(w)) # plot points above(green) and below(blue) the target function. green_x = [] green_y = [] blue_x = [] blue_y = [] for x in points: if x[3] == 1: green_x.append(x[1]) green_y.append(x[2]) else: blue_x.append(x[1]) blue_y.append(x[2]) pylab.plot(green_x, green_y, 'go') pylab.plot(blue_x, blue_y, 'bo') # plot target function(black) and hypothesis function(red) lines x = np.array( [-1,1] ) slope = (y2-y1)/(x2-x1) intercept = y2 - slope * x2 pylab.plot(x, slope*x + intercept, 'k--') pylab.plot( x, -w[1]/w[2] * x - w[0] / w[2] , 'r' ) # this will throw an error if w[2] == 0 pylab.ylim([-1,1]) pylab.xlim([-1,1]) pylab.show() return iterations, w, errorProb
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))
def perceptron(kf,data,label,k): for train, test in kf: X_train, X_test, y_train, y_test = data[train,:], data[test,:], label[train], label[test] log = Perceptron(penalty="l2", alpha=0.003) logit = log.fit(X_train,y_train) y_pred = logit.predict(X_test) scores = cross_validation.cross_val_score(log, data, label, cv=k) return scores.mean()
def train_sk(ldocs, clname='perc'): docs = [bow_to_avmap(doc) for _, doc in ldocs] lbls = [l for l, _ in ldocs] vec = DictVectorizer() encoded_docs = vec.fit_transform(docs) if clname == 'perc': classifier = Perceptron(n_iter=20) elif clname == 'sgd': classifier = SGDClassifier(penalty='elasticnet', alpha=0.0001, l1_ratio=0.85, n_iter=1000, n_jobs=-1) elif clname == 'nb': classifier = MultinomialNB() elif clname == 'svm': classifier = LinearSVC() classifier.fit(encoded_docs, lbls) return vec, classifier
class learn_by_perceptron: def __init__(self, X=None, Y=None, path=r"..\..\per_dump.pkl", penalty='l1', alpha=0.00001, fit=True): if X is None or Y is None: self.clf = joblib.load(path) else: self.clf = Perceptron(penalty=penalty, alpha=alpha, n_jobs=6, class_weight='auto', shuffle=True) if fit: self.clf.fit(X, Y) self.dump(path) def predict(self, X): return self.clf.predict(X) def cross_val(self, X, Y, n, cpus=6): return cross_validation.cross_val_score(self.clf, X, Y, cv=n, n_jobs=cpus, scoring='f1') def dump(self, path=r"..\..\svm_dump.pkl"): joblib.dump(self.clf, path)
def objectiveFunc(args): global X_train_std, X_test_std, y_train, y_test ppn = Perceptron(n_iter=args['n_iter'], eta0=args['eta'], random_state=0) ppn.fit(X_train_std, y_train) y_pred = ppn.predict(X_test_std) return -accuracy_score(y_test, y_pred)
# 训练数据和测试数据 x_data_train = x[:data_split, :] x_data_test = x[data_split:, :] y_data_train = y[:data_split] y_data_test = y[data_split:] # 正例和反例 positive_x1 = [x[i, 0] for i in range(sample_num) if y[i] == 1] positive_x2 = [x[i, 1] for i in range(sample_num) if y[i] == 1] negative_x1 = [x[i, 0] for i in range(sample_num) if y[i] == 0] negative_x2 = [x[i, 1] for i in range(sample_num) if y[i] == 0] # 定义感知机 clf = Perceptron(fit_intercept=True, n_iter_no_change=30, shuffle=True) # 使用训练数据进行训练 clf.fit(x_data_train, y_data_train) # 得到训练结果,权重矩阵 print('Coef Matrix:', clf.coef_) # 决策函数中的常数,此处输出为:[0.] print('Intercept:', clf.intercept_) # 利用测试数据进行验证 acc = clf.score(x_data_test, y_data_test) print('ACC:', acc) # 画出正例和反例的散点图 plt.figure(figsize=(10, 5)) plt.scatter(positive_x1, positive_x2, c='red') plt.scatter(negative_x1, negative_x2, c='blue')
def test_predict_proba(create_X_y): X, y = create_X_y clf1 = Perceptron() clf1.fit(X, y) Rank([clf1, clf1]).fit(X, y)
#!/usr/bin/python3 # coding: utf-8 import pandas as pd from sklearn.svm import LinearSVC # 导入线性支持向量机分类器 from sklearn.linear_model import Perceptron # 导入感知机分类器 from sklearn.model_selection import train_test_split # 导入数据集切分模块 ################################################################## ## 准备数据 df = pd.read_csv("data.csv", header=0) x = df[["x", "y"]] y = df["class"] train_feature, test_feature, train_target, test_target = train_test_split(x, y, train_size=0.77, random_state=56) import matplotlib.pyplot as plt plt.scatter(x['x'], x['y'], c=y) plt.show() # 可以看到数据是分成两部分的, 有两个噪声... # 构建感知机预测模型 model = Perceptron() model.fit(train_feature, train_target) results = model.score(test_feature, test_target); print(results) # 0.913043478261; 感知机分类准确度 # 构建线性支持向量机分类模型 model2 = LinearSVC() model2.fit(train_feature, train_target) results2 = model2.score(test_feature, test_target); print(results2) # 1.0; 支持向量机分类准确度 ################################################################## ## 总结: # 1. 具体解释见: 实验楼相关参考, README.md # 2. data.csv 数据放置的很合理...
def acuu(x, y): l = [] k = [] from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.33, random_state=7) fit = Normalizer().fit(x_train) x_train = fit.fit_transform(x_train) # MODEL-4) Linear SVC # ------------------------------------------ from sklearn.svm import LinearSVC linear_svc = LinearSVC() linear_svc.fit(x_train, y_train) y_pred = linear_svc.predict(x_val) acc_linear_svc = round(accuracy_score(y_pred, y_val) * 100, 2) print("MODEL-2: Accuracy of LinearSVC : ", acc_linear_svc) l.append(acc_linear_svc) acc_linear_svck = cross_val_score(linear_svc, x_val, y_val, cv=10, scoring='accuracy') print("MODEL-2: Accuracy of Support Vector Machines by k-fold : ", round(acc_linear_svck.mean() *100, 2)) k.append(round(acc_linear_svck.mean() *100, 2)) # MODEL-5) Perceptron # ------------------------------------------ from sklearn.linear_model import Perceptron perceptron = Perceptron() perceptron.fit(x_train, y_train) y_pred = perceptron.predict(x_val) acc_perceptron = round(accuracy_score(y_pred, y_val) * 100, 2) print("MODEL-3: Accuracy of Perceptron : ", acc_perceptron) l.append(acc_perceptron) acc_perceptronk = cross_val_score(perceptron, x_val, y_val, cv=10, scoring='accuracy') print("MODEL-3: Accuracy of Support Vector Machines by k-fold : ", round(acc_perceptronk.mean() * 100, 2)) k.append(round(acc_perceptronk.mean() * 100, 2)) # MODEL-8) KNN or k-Nearest Neighbors # ------------------------------------------ from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier() knn.fit(x_train, y_train) y_pred = knn.predict(x_val) acc_knn = round(accuracy_score(y_pred, y_val) * 100, 2) print("MODEL-4: Accuracy of k-Nearest Neighbors : ", acc_knn) l.append(acc_knn) acc_knnk = cross_val_score(knn, x_val, y_val, cv=10, scoring='accuracy') print("MODEL-4: Accuracy of Support Vector Machines by k-fold : ", round(acc_knnk.mean() * 100, 2)) k.append(round(acc_knnk.mean() * 100, 2)) # MODEL-9) Stochastic Gradient Descent # ------------------------------------------ from sklearn.linear_model import SGDClassifier sgd = SGDClassifier() sgd.fit(x_train, y_train) y_pred = sgd.predict(x_val) acc_sgd = round(accuracy_score(y_pred, y_val) * 100, 2) print("MODEL-5: Accuracy of Stochastic Gradient Descent : ", acc_sgd) l.append(acc_sgd) acc_sgdk = cross_val_score(sgd, x_val, y_val, cv=10, scoring='accuracy') print("MODEL-5: Accuracy of Support Vector Machines by k-fold : ", round(acc_sgdk.mean() * 100, 2)) k.append(round(acc_sgdk.mean() * 100, 2)) # MODEL-6) XGBoost from xgboost import XGBClassifier classifier = XGBClassifier() classifier.fit(x_train, y_train) y_pred = classifier.predict(x_val) acc_xgb=round(accuracy_score(y_pred, y_val) * 100, 2) print("MODEL-6: Accuracy of XGBoost : ", acc_xgb) l.append(acc_xgb) acc_xgbk = cross_val_score(classifier, x_val, y_val, cv=10, scoring='accuracy') print("MODEL-6: Accuracy of Support Vector Machines by k-fold : ", round(acc_xgbk.mean() * 100, 2)) k.append(round(acc_xgbk.mean() * 100, 2)) classifiers=['LinearSVC', 'Perceptron','KNeighborsClassifier','SGDClassifier','XGBoost'] plt.bar(classifiers, l, color=['b','m','g','y','maroon','cyan']) plt.xticks(classifiers, rotation='vertical') plt.xlabel('Classifiers') plt.ylabel('Accuracies') plt.title("Accuracy_Score()") plt.show() l = [] plt.bar(classifiers, k, color=['b', 'm', 'g', 'y', 'maroon', 'cyan']) plt.xticks(classifiers, rotation='vertical') plt.xlabel('Classifiers') plt.ylabel('Accuracies') plt.title("KFolds10") plt.show() k=[]
# lets plot it to see fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for idx, set in enumerate(X): color = 'r' if y[idx] == -1 else 'b' ax.scatter(X[idx][0], X[idx][1], X[idx][2], c=color, marker='o') plt.show() plt.scatt perceptron = Perceptron(alpha=1e-5, random_state=1) perceptron.fit(X, y) print(perceptron.coef_) print(perceptron.intercept_) print(perceptron.predict(X)) print(perceptron.score(X, y)) # TODO plot decision boundary # multilayer ANN, good for more complex, non-linear data # XOR example X = [[0, 0], [0, 1], [1, 0], [1, 1]] y = [0, 1, 1, 0] mlp = MLPClassifier(solver='lbfgs',
# [email protected] # from sklearn import datasets from sklearn.linear_model import Perceptron # 讀進資料 iris = datasets.load_iris() X = iris.data[:, [0, 2]] y = iris.target # 生成分類器 ppn = Perceptron(n_iter=20, eta0=.01, random_state=1) # 訓練 (fit) ppn.fit(X, y) #plot_decision_regions(X, y, classifier= ppn) # 預測 (predict) yHat = ppn.predict(X) # 評估正確率 (之1) theTrueNumber = (y == yHat).sum() theTotalNumber = len((y == yHat)) theAccuracy = theTrueNumber / theTotalNumber print('theAccuracy= ', theAccuracy) # 評估正確率 (之2) theScore = ppn.score(X, y) print('theScore= ', theScore)
dataSet = x for i in range(len(dataSet)): X = dataSet[i][1] Y = dataSet[i][2] Z = dataSet[i][3] if y[i] == 1: p = ax.scatter(X, Y, Z, c='c', marker='^') else: p2 = ax.scatter(X, Y, Z, c='r', marker='o') ax.legend([p, p2], ['Label 1 data', 'Label -1 data']) xs, ys = np.meshgrid(np.arange(-0.2, 1.2, 0.02), np.arange(-0.2, 1.2, 0.02)) zs = -(w[0] + w[1] * xs + w[2] * ys) / w[3] ax.plot_surface(xs, ys, zs, color='b', alpha=0.3) plt.show() if __name__ == '__main__': X, Y = inputData("classification.txt") X = np.c_[np.ones(len(X)), np.array(X)] pla = Perceptron() pla.n_iter = 5000 print(pla.get_params()) pla = pla.fit(X, Y) accuracy = pla.score(X, Y) W = pla.coef_ print('Accuracy =', accuracy) print('Weights =', W) display(X, Y, W[0])
from sklearn import datasets from sklearn.cross_validation import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import Perceptron from sklearn.metrics import accuracy_score import numpy as np if __name__ == '__main__': iris = datasets.load_iris() X = iris.data[:, [2, 3]] y = iris.target X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) ml = Perceptron(eta0=0.01, n_iter=40, random_state=0) ml.fit(X_train_std, y_train) y_pred = ml.predict(X_test_std) print('총 테스트 개수: %d, 오류개수:%d' % (len(y_test), (y_test != y_pred).sum())) print('정확도: %2f' % accuracy_score(y_test, y_pred))
import numpy as np from sklearn.linear_model import Perceptron import pandas as pd from sklearn import metrics from sklearn.preprocessing import StandardScaler data_train = pd.read_csv("train.csv", header=None) data_test = pd.read_csv("test.csv", header=None) X_train = data_train[[1, 2]] X_test = data_test[[1, 2]] y_train = data_train[[0]] y_test = data_test[[0]] clf = Perceptron(random_state=241) clf.fit(X_train, y_train) predictions = clf.predict(X_test) accuracy_before = metrics.accuracy_score(y_test, predictions) scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) clf.fit(X_train_scaled, y_train) predictions = clf.predict(X_test_scaled) accuracy_after = metrics.accuracy_score(y_test, predictions) print(str(accuracy_before) + '____' + str(accuracy_after)) print(accuracy_after - accuracy_before)
from sklearn.linear_model import Perceptron from sklearn.preprocessing import StandardScaler data_train = read_csv('perceptron-train.csv', header=None) data_test = read_csv('perceptron-test.csv', header=None) y_train = data_train[0] y_test = data_test[0] X_train = data_train[data_train.columns[1:]] X_test = data_test[data_test.columns[1:]] perceptron = Perceptron(random_state=241) perceptron.fit(X_train, y_train) accuracy = perceptron.score(X_test, y_test) print() print('Accuracy:', accuracy) scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) perceptron2 = Perceptron(random_state=241) perceptron2.fit(X_train_scaled, y_train) accuracy2 = perceptron2.score(X_test_scaled, y_test)
max_n = None max_r = None max_Accuracy = -1 for a in n: #iterates over n for b in r: #iterates over r #Create the perceptron classifier clf = Perceptron( eta0=a, random_state=b, max_iter=1000 ) #eta0 = learning rate, random_state = used to shuffle the training data #Fitperceptron to the training data clf.fit(X_training, y_training) #make the classifier prediction for each test sample and start computing its accuracy #hint: to iterate over two collections simultaneously with zip() Example: #for (x_testSample, y_testSample) in zip(X_test, y_test): #to make a prediction do: clf.predict([x_testSample]) correctCount = 0 for (x_testSample, y_testSample) in zip(X_test, y_test): y_pred = clf.predict([x_testSample])[0] if (y_testSample == y_pred): correctCount += 1 accuracy = correctCount / len(X_test) #check if the calculated accuracy is higher than the previously one calculated. If so, update the highest accuracy and print it together with the perceprton hyperparameters #Example: "Highest Perceptron accuracy so far: 0.88, Parameters: learning rate=00.1, random_state=True"
from sklearn.preprocessing import StandardScaler scaler = StandardScaler() train = pandas.read_csv('perceptron-train.csv', header=None, names=["T", "P1", "P2"]) train_target = train["T"] train_data = train.drop("T", axis=1) test = pandas.read_csv('perceptron-test.csv', header=None, names=["T", "P1", "P2"]) test_target = test["T"] test_data = test.drop("T", axis=1) p = Perceptron(random_state=241) p.fit(train_data, train_target) predictions = p.predict(test_data) a = accuracy_score(test_target, predictions) print(a) train_data = scaler.fit_transform(train_data) test_data = scaler.transform(test_data) p.fit(train_data, train_target) predictions = p.predict(test_data) b = accuracy_score(test_target, predictions) print(b) print(b - a)
print("Domut Test:") X, Y = datasets.make_circles(n_samples=200) # print("X:", X) # print("Y:", Y) X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=1, stratify=Y) sc = StandardScaler() sc.fit(X) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) ppn = Perceptron(max_iter=30, eta0=0.01, verbose=False) ppn.fit(X_train_std, Y_train) Y_hat = ppn.predict(X_test_std) print('Good classified samples: %d' % (accuracy_score(Y_test, Y_hat, normalize=False))) print('Misclassified samples: %d' % (Y_test != Y_hat).sum()) print('Accuracy: %.2f' % accuracy_score(Y_test, Y_hat)) print('Accuracy: %.2f' % ppn.score(X_test_std, Y_test)) plot_decision_regions(X=X_test_std, y=Y_test, classifier=ppn) plt.xlabel('X0 [standardized]') plt.ylabel('X1 [standardized]') plt.legend(loc='upper left') plt.tight_layout() plt.show() title = "Learning Curves (Perceptron)"
def test_predict_proba(): X = X_dsel_ex1 y = y_dsel_ex1 clf1 = Perceptron() clf1.fit(X, y) LCA([clf1, clf1]).fit(X, y)
#prediction. #Look at page 259 for the equation. #The decision boundary of each output neuron is linear, so Perceptrons are incapable #of learning complex patterns. ##Creating a single LTU network using the Perceptron class: import numpy as np from sklearn.datasets import load_iris from sklearn.linear_model import Perceptron iris = load_iris() X = iris.data[:, (2, 3)] #petal length, petal width y = (iris.target == 0).astype(np.int) per_clf = Perceptron(random_state=42) per_clf.fit(X, y) y_pred = per_clf.predict([[2, 0.5]]) # Predicts the class of an iris with a #petal length of 2 and petal width of 0.5. print(y_pred) #the SGDClassifier can perform the same computation through setting #the hyperparameters to loss="perceptron", learning_rate = constant, #and eta = 0.1 and penalty = None. ##Multi-layer perceptron and backpropagation: #An MLP is composed of one input layer, one or more layers of LTUs, #called hidden layers, and one final layer of LTUs called the output layer. #Every layer except the output layer includes a bias neuron and is fully #connected to the next layer. #Backpropagation explained: for each training instance the back propagation
import numpy as np from sklearn.datasets import load_iris from sklearn.linear_model import Perceptron iris = load_iris() X = iris.data[:, (2, 3)] # petal length, petal width print(X) y = (iris.target == 0).astype(np.int) # Iris setosa? print(y) per_clf = Perceptron() per_clf.fit(X=X, y=y) y_prediction = per_clf.predict(X=[[2, 0.5]]) print(y_prediction)
X = array[:, 0:57] # 0:3 are the features, 4 is the class Y = array[:, 57] test_sz = 0.20 seed = 7 scoring = 'accuracy' # ratio of correct predictions / total nr of instances X_train, X_test, Y_train, Y_test = model_selection.train_test_split( X, Y, test_size=test_sz, random_state=seed) # Printing training errors clf1 = Perceptron(penalty='l1', random_state=0, max_iter=5000, alpha=0.0001) clf2 = Perceptron(penalty='l1', random_state=1, max_iter=5000, alpha=0.0001) clf3 = Perceptron(penalty='l1', random_state=2, max_iter=5000, alpha=0.0001) clf4 = Perceptron(penalty='l1', random_state=3, max_iter=5000, alpha=0.0001) clf1.fit(X_train, Y_train) p1 = clf1.predict(X_train) err1 = clf1.score(X_train, Y_train) clf2.fit(X_train, Y_train) p2 = clf2.predict(X_train) err2 = clf2.score(X_train, Y_train) clf3.fit(X_train, Y_train) p3 = clf3.predict(X_train) err3 = clf3.score(X_train, Y_train) clf4.fit(X_train, Y_train) p4 = clf4.predict(X_train) err4 = clf4.score(X_train, Y_train)
#importing relevant libraries from sklearn.linear_model import Perceptron import matplotlib.pyplot as plt import numpy as np from itertools import product #creating data points that represent a traditional AND Logic gate problem data=[[0,0],[1,0],[0,1],[1,1]] labels=[0,0,0,1] #producing an initial scatter plot of this problem plt.scatter([point[0] for point in data], [point[1] for point in data], c= labels) #initialising and fitting a perceptron classifier to this problem classifier =Perceptron(max_iter=40) classifier.fit(data,labels) print(classifier.score(data,labels)) #creating a heat map to show the decision boundary in which the perceptron algorithim settles upon x_values=np.linspace(0,1,100) y_values=np.linspace(0,1,100) point_grid=list(product(x_values,y_values)) distances=(classifier.decision_function(point_grid)) abs_distances=[abs(i) for i in distances] distances_matrix=np.reshape(abs_distances,(100,100)) heatmap=plt.pcolormesh(x_values,y_values,distances_matrix) plt.colorbar(heatmap) plt.show()
def test_predict_proba(): X = X_dsel_ex1 y = y_dsel_ex1 clf1 = Perceptron() clf1.fit(X, y) DESClustering([clf1, clf1]).fit(X_dsel_ex1, y_dsel_ex1)
if idx and idx % 10 == 9: val_idx.append(idx) elif idx and idx % 10 == 0: test_idx.append(idx) else: train_idx.append(idx) x_train, x_val, x_test = x[train_idx, :], x[val_idx, :], x[test_idx, :] y_train, y_val, y_test = y[train_idx], y[val_idx], y[test_idx] ''' Trains and tests Perceptron model from scikit-learn ''' model = Perceptron(penalty=None, alpha=0.0, tol=1) # Trains scikit-learn Perceptron model model.fit(x_train, y_train) print('Results using scikit-learn Perceptron model') # Test model on training set scores_train = model.score(x_train, y_train) print('Training set mean accuracy: {:.4f}'.format(scores_train)) # Test model on validation set scores_val = model.score(x_val, y_val) print('Validation set mean accuracy: {:.4f}'.format(scores_val)) # Test model on testing set scores_test = model.score(x_test, y_test) print('Testing set mean accuracy: {:.4f}'.format(scores_test))
scoring='accuracy', verbose=3) grid_search.fit(Train_Matrix, Train_Target_Matrix) clf = grid_search.best_estimator_ # data testing T_predict = clf.predict(Test_Matrix) print( "SVM: The prediction accuracy (tuned) for all testing sentence is : {:.2f}%." .format(100 * accuracy_score(Test_Target_Matrix, T_predict))) ## Perceptron ############### clfPerceptron = Perceptron(n_iter=100) clfPerceptron.fit(Train_Matrix, Train_Target_Matrix) # Make predictions using the testing set testDataPrediction = clfPerceptron.predict(Test_Matrix) # Make predictions using the testing set trainingDataPrediction = clfPerceptron.predict(Train_Matrix) print( "Perceptron: The prediction accuracy (tuned) for all testing sentence is : {:.2f}%." .format(100 * accuracy_score(Test_Target_Matrix, testDataPrediction))) ## Naive Bayes ############### naiveBayesClassifier = GaussianNB() naiveBayesClassifier.fit(Train_Matrix, Train_Target_Matrix)
# Labels counts in y: [50 50 50] print('Labels counts in y_train:', np.bincount(y_train)) # Labels counts in y_train: [35 35 35] print('Labels counts in y_test:', np.bincount(y_test)) # Labels counts in y_test: [15 15 15] from sklearn.preprocessing import StandardScaler sc = StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) from sklearn.linear_model import Perceptron ppn = Perceptron(n_iter=40, eta0=0.1, random_state=1) ppn.fit(X_train_std, y_train) y_pred = ppn.predict(X_test_std) print('Misclassified samples: %d' % (y_test != y_pred).sum()) # Misclassified samples: 3 from sklearn.metrics import accuracy_score print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) # Accuracy: 0.93 print('Accuracy: %.2f' % ppn.score(X_test_std, y_test)) # Accuracy: 0.93 from matplotlib.colors import ListedColormap import matplotlib.pyplot as plt def plot_decision_regions(X, y, classifier, test_idx=None,resolution=0.02):
x_train = train_df.drop(['Survived'], axis=1) y_train = train_df['Survived'] x_test = test_df l = LogisticRegression() l.fit(x_train, y_train) y_pred = l.predict(x_test) log_accuracy = round(l.score(x_train, y_train) * 100, 2) decision_tree = DecisionTreeClassifier() decision_tree.fit(x_train, y_train) y_pred = decision_tree.predict(x_test) acc_decision_tree = round(decision_tree.score(x_train, y_train) * 100, 2) svm = LinearSVC() svm.fit(x_train, y_train) y_pred = svm.predict(x_test) svm_accuracy = round(svm.score(x_train, y_train) * 100) perceptron = Perceptron(max_iter=5) perceptron.fit(x_train, y_train) y_pred = perceptron.predict(x_test) acc_perceptron = round(perceptron.score(x_train, y_train) * 100, 2) gaussian = GaussianNB() gaussian.fit(x_train, y_train) y_pred = gaussian.predict(x_test) acc_gaussian = round(gaussian.score(x_train, y_train) * 100, 2)
# Label Y = [ 'male', 'male', 'female', 'female', 'male', 'male', 'female', 'female', 'female', 'male', 'male' ] # Classifiers clf_tree = tree.DecisionTreeClassifier() clf_svm = SVC() clf_perceptron = Perceptron() clf_KNN = KNeighborsClassifier() # Training the models clf_tree.fit(X, Y) clf_svm.fit(X, Y) clf_perceptron.fit(X, Y) clf_KNN.fit(X, Y) # Testing the data pred_tree = clf_tree.predict(X) pred_svm = clf_svm.predict(X) pred_perceptron = clf_perceptron.predict(X) pred_KNN = clf_KNN.predict(X) # Checking the accuracy acc_tree = accuracy_score(Y, pred_tree) * 100 print('Accuracy for Decision Tree: {}'.format(acc_tree)) acc_svm = accuracy_score(Y, pred_svm) * 100 print('Accuracy for SVM: {}'.format(acc_svm))
def train(its, rd_state): classifier = Perceptron(max_iter=its,random_state=rd_state) self.classifier=classifier.fit(self.features,self.target)
test_data['pass'] = [ 'pass', 'pass', 'pass', 'pass', 'pass', 'pass', 'pass', 'pass', 'pass', 'pass', 'fail', 'fail', 'fail', 'fail', 'fail', 'fail', 'fail', 'fail', 'fail', 'fail' ] # Establish X and Y. X = test_data[['test', 'project']] Y = test_data['pass'] # Establish Perceptron Model. # 10,000 iterations to ensure accuracy since data is non-normalized. perceptron = Perceptron(n_iter=10000) # Fit Perceptron. perceptron.fit(X, Y) # Get Parameters. print('Score: ' + str(perceptron.score(X, Y))) # Establish a mesh for our plot. x_min, x_max = X.test.min() - 1, X.test.max() + 3 y_min, y_max = X.project.min() - 1, X.project.max() + 3 xx, yy = np.meshgrid(np.arange(x_min, x_max, .1), np.arange(y_min, y_max, .1)) # Predict over that mesh. Z = (perceptron.predict(np.c_[xx.ravel(), yy.ravel()]) == 'pass') # Reshape the prediction to be plottable. Z = Z.reshape(xx.shape)
@author: andre """ import pandas as pd from sklearn.linear_model import Perceptron from sklearn.metrics import accuracy_score from sklearn.preprocessing import StandardScaler import sys sys.path.append("..") '''loading samples of learning and testing''' df_train = pd.read_csv("perceptron-train.csv", header=None) X_train = df_train.loc[:, 1:] y_train = df_train[0] df_test = pd.read_csv("perceptron-test.csv", header=None) X_test = df_test.loc[:, 1:] y_test = df_test[0] '''learning perceptron''' model = Perceptron(max_iter=5, tol=None, random_state=241) model.fit(X_train, y_train) '''calculate quality of classifier on test sample''' acc_before = accuracy_score(y_test, model.predict(X_test)) acc_before '''normalize the sample''' scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) '''learning on the new sample''' model.fit(X_train_scaled, y_train) acc_after = accuracy_score(y_test, model.predict(X_test_scaled)) acc_after diff = acc_after - acc_before print(1, f"{diff:.3f}")
Y_pred = knn.predict(X_test) acc_knn = round(knn.score(X_train, Y_train) * 100, 2) print "Accuracy of KNN ", acc_knn #Gaussian NB gaussian = GaussianNB() gaussian.fit(X_train, Y_train) Y_pred = gaussian.predict(X_test) acc_gaussian = round(gaussian.score(X_train, Y_train) * 100, 2) print "Accuracy of gaussian NB ", acc_gaussian # Perceptron perceptron = Perceptron() perceptron.fit(X_train, Y_train) Y_pred = perceptron.predict(X_test) acc_perceptron = round(perceptron.score(X_train, Y_train) * 100, 2) print "Accuracy of perceptron", acc_perceptron # Linear SVC linear_svc = LinearSVC() linear_svc.fit(X_train, Y_train) Y_pred = linear_svc.predict(X_test) acc_linear_svc = round(linear_svc.score(X_train, Y_train) * 100, 2) print "Accuracy of linear svc", acc_linear_svc # Stochastic Gradient Descent sgd = SGDClassifier()
labels = getlabels(vocals_temporal_transformed) if (plot_features): plt.figure() #added just to make sure this goes on its own figure for i in range(4): plt.plot(test[i]) plt.plot(labels) plt.show() print('SFA Features Plotted') else: print('Skipping Feature Plotting') ## Compare SFA With Baseline For Linear Classification print('SFA Based Classifier with ', classifier_features, ' features') classifier_SFA.fit(test[:classifier_features].T, labels) print(classifier_SFA.score(test[:classifier_features].T, labels), '\n') SFA_save_score[a_round, iteration] = classifier_SFA.score( test[:classifier_features].T, labels) #make this whole code into a for loop and save the scores for a particular SNR here print('Baseline Classifier with ', baseline_features, ' features') classifier_baseline.fit(testing_data.T, labels) print(classifier_baseline.score(testing_data.T, labels)) ##Plot it #SFAClassifiedPlot(test,classifier_SFA,labels[:-5]) ###New metric: Distance between stimulus trajectories in SFA space