# importing scikit learn with make_blobs from sklearn.datasets.samples_generator import make_blobs # creating datasets X containing n_samples # Y containing two classes X, Y = make_blobs(n_samples=500, centers=2, random_state=0, cluster_std=0.40) import matplotlib.pyplot as plt # plotting scatters plt.scatter(X[:, 0], X[:, 1], c=Y, s=50, cmap='spring'); plt.show()# Method 1 from sklearn.svm import LinearSVC # OR from sklearn.svm import SVC # instatiate a scikit-learn SVM model # to indicate the class imbalance at fit time, set class_weight='balanced' # for reproducible output across multiple function calls, set random_state to a given integer value svm = LinearSVC(class_weight='balanced', random_state=42, loss="hinge", fit_intercept=False) # svm = SVC(kernel='linear', gamma=.5, probability=True) # Another way # train a linear Support Vector Machine model using Scikit-Learn t0 = time.time() svm.fit(X_train, y_train) sklearn_time = time.time() - t0 # Method 2 : Use snapml library # in contrast to scikit-learn's LinearSVC, Snap ML offers multi-threaded CPU/GPU training of SVMs from snapml import SupportVectorMachine snapml_svm_gpu = SupportVectorMachine(class_weight='balanced', random_state=42, use_gpu=True, fit_intercept=False) snapml_svm_cpu = SupportVectorMachine(class_weight='balanced', random_state=42, n_jobs=4, fit_intercept=False) t0 = time.time() model = snapml_svm_cpu.fit(X_train, y_train) snapml_time = time.time() - t0 # Predict y_pred = svm.predict(X_test) # Evaluate model roc_auc_score(y_test, y_pred) # Get confidence score for probability y_pred_conf = svm.decision_function(X_test) # Evaluate hinge loss hinge_loss(y_test, y_pred_conf)