<jupyter_start><jupyter_text>Problem 1 Load Iris data. Estimate the SVM classifier of two types of Iris using hard margin SVM (C = Inf) cross-validation = 3. Use the code below.For fit measure use default cross-validation score -- share correct predictions. Can you estimate the model? If you cannot why?<jupyter_code># Common imports
import numpy as np
import os
# to make this notebook's output stable across runs
np.random.seed(42)
# To plot pretty figures
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
# import datasets
from sklearn import datasets
iris = datasets.load_iris()
X = iris["data"][:,(1,2)]  # Use all X
y = iris["target"]
virginica_or_versicolor = (y == 2) | (y == 1)
X = X[virginica_or_versicolor]
y = y[virginica_or_versicolor]<jupyter_output><empty_output><jupyter_text>Problem 2Run the same model with a soft margin: C = 1 and plot decision boundary. Don't forget to use plt.plot(X[:, 0][y==2], X[:, 1][y==2], "ms") when plotting instead of plt.plot(X[:, 0][y==0], X[:, 1][y==0], "ms"). Describe the results in Problem 2 and 1. Why do we get these results?  Problem 3: Reestimate model in problem 2 with a) optimal C parameter. Loop through a vector 1000 random numbers from .05 to 5.  b) scaled data and C = 1. Which produces better cross-validation score?If more than one parameter produces the highest cross validation score choose the smallest C that maximuzes cross-validation<jupyter_code>np.random.seed(42)




# Answer: the validation scores are the same<jupyter_output><empty_output><jupyter_text>Problem 4 Use full data for Irises. Use scaled data created below. Estimate baseline SVM model with C = 1, calculate cross-validation score with cv =5.  The estimate the same model with 10th degree polynomial of X. Report averages for both models.<jupyter_code>from sklearn.svm import SVC
# import datasets
from sklearn.preprocessing import PolynomialFeatures
from sklearn import datasets
iris = datasets.load_iris()
X = iris["data"]
y = iris["target"]<jupyter_output><empty_output><jupyter_text>Problem 5 Create a datasets of moons below. Estimate the SVM model using 10 degress polynomial and C =1. Create polynomials in two ways: a) use PolynomialFeatures(degree=10), b) use kernel SVC(kernel="poly", degree=10, coef0=1, C=1). Use cross-validation cv =5 and report average score. Measure the time of using a kernel relatively to the polynomial features.<jupyter_code>from sklearn.datasets import make_moons
X, y = make_moons(n_samples=10000, noise=0.28, random_state=42)<jupyter_output><empty_output><jupyter_text>Problem 6Run  with Sigmoid  10 degrees and Gaussian Kernel RBF with 4 degrees. Predict Y using cross-validation cv=5 and C =1. Use scaled X data. Report average score and time for each estimation. Is it higher than the 4-degree polinomial?<jupyter_code># set random_state = 42 in the SVC commands<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08822575413558223, 'fraction_numerical': 0.024002594875121634, 'mean_word_length': 5.4789915966386555, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '293357', 'license': 'MIT', 'n_tokens_mistral': 1005, 'n_tokens_neox': 929, 'n_words': 382, 'path': 'lesson_5_SVM/HW_5_SVM.ipynb', 'repo_name': 'JiaheXu/Statistical_ML', 'size': '3083'}