<jupyter_start><jupyter_text>Support Vector Machines Live Demos<jupyter_code>iris_attributes, iris_classes = load_iris().data, load_iris().target
iris_attributes.shape
iris_attributes_train, iris_attributes_test, iris_classes_train, iris_classes_test = train_test_split(
    iris_attributes, iris_classes, train_size = 0.7, stratify = iris_classes)
iris_attributes_train.shape
iris_attributes_test.shape
svm = LinearSVC(C = 10)
svm.fit(iris_attributes_train, iris_classes_train)
svm.score(iris_attributes_train, iris_classes_train)
svm.score(iris_attributes_test, iris_classes_test)
gaussian_svm = SVC(C = 0.0001)
gaussian_svm.fit(iris_attributes_train, iris_classes_train)
gaussian_svm.score(iris_attributes_train, iris_classes_train)
gaussian_svm.score(iris_attributes_test, iris_classes_test)
circles_data, circles_classes = make_circles(1000, noise = 0.05, factor = 0.5)
plt.gca().set_aspect("equal")
plt.scatter(circles_data[:, 0], circles_data[:, 1], c = circles_classes)
circles_data_train, circles_data_test, circles_classes_train, circles_classes_test = train_test_split(
    circles_data, circles_classes, train_size = 0.8)
svm_circles = LinearSVC(C = 1e9)
svm_circles.fit(circles_data_train, circles_classes_train)
svm_circles.score(circles_data_train, circles_classes_train)
svm_circles.score(circles_data_test, circles_classes_test)
svm_circles_gaussian = SVC(kernel = "rbf", C = 100)
svm_circles_gaussian.fit(circles_data_train, circles_classes_train)
svm_circles_gaussian.score(circles_data_train, circles_classes_train)
svm_circles_gaussian.score(circles_data_test, circles_classes_test)
def fit_and_score(estimator, train_X, train_y, test_X, test_y):
    estimator.fit(train_X, train_y)
    print("Train score: {}".format(estimator.score(train_X, train_y)))
    print("Test score: {}".format(estimator.score(test_X, test_y)))    
fit_and_score(svm_circles, circles_data_train, circles_classes_train, circles_data_test, circles_classes_test)
fit_and_score(svm_circles_gaussian, circles_data_train, circles_classes_train, circles_data_test, circles_classes_test)
fit_and_score(svm, iris_attributes_train, iris_classes_train, iris_attributes_test, iris_classes_test)
logistic_regression_circles = LogisticRegression(C = 1e9)
fit_and_score(logistic_regression_circles, circles_data_train, circles_classes_train, circles_data_test, circles_classes_test)
circles_data_poly = PolynomialFeatures(degree = 2).fit_transform(circles_data)
circles_data_poly_train, circles_data_poly_test, circles_classes_poly_train, circles_classes_poly_test = train_test_split(circles_data_poly, circles_classes, train_size = 0.8)
circles_data_poly_train.shape
circles_classes_poly_train.shape
fit_and_score(
    logistic_regression_circles,
    circles_data_poly_train,
    circles_classes_poly_train,
    circles_data_poly_test,
    circles_classes_poly_test)<jupyter_output>Train score: 1.0
Test score: 1.0
{'fraction_non_alphanumeric': 0.08244222145567437, 'fraction_numerical': 0.012418075198344257, 'mean_word_length': 10.55378486055777, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '313839', 'license': 'MIT', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1059, 'n_words': 146, 'path': 'MachineLearning/04.SupportVectorMachines/Support Vector Machines Demos.ipynb', 'repo_name': 'LuGeorgiev/Python-SoftUni', 'size': '2899'}