Give me example code of fitting svm
- User
Assistant: User: Give me example code of fitting svm Assistant:
- Assistant
Certainly! Here's an example of how you can fit a Support Vector Machine (SVM) using Python's popular machine learning library, Scikit-Learn. In this example, I'll use a simple dataset for binary classification.
# Import necessary libraries from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC from sklearn.metrics import accuracy_score # Load a simple dataset iris = datasets.load_iris() X = iris.data y = iris.target # For simplicity, we'll just use two classes (binary classification) # Let's filter out one of the classes X = X[y != 2] y = y[y != 2] # Split the dataset into training and test sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # Standardize the features scaler = StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) # Initialize and fit the SVM model svm_model = SVC(kernel='linear', C=1.0, random_state=42) svm_model.fit(X_train_scaled, y_train) # Predict on the test set y_pred = svm_model.predict(X_test_scaled) # Calculate the accuracy accuracy = accuracy_score(y_test, y_pred) print(f'Accuracy: {accuracy:.2f}')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.