svm代码实现python
时间: 2023-08-27 09:16:13 浏览: 56
Sure! Here's an example of how you can implement SVM (Support Vector Machine) in Python using the scikit-learn library:
```python
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Assuming you have your data and labels ready
# X - Input features, y - Labels
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create an SVM classifier
classifier = svm.SVC(kernel='linear')
# Fit the classifier to the training data
classifier.fit(X_train, y_train)
# Make predictions on the testing data
predictions = classifier.predict(X_test)
# Calculate the accuracy of the model
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)
```
In this example, we use the `svm.SVC` class from scikit-learn to create an SVM classifier with a linear kernel. We split the data into training and testing sets using `train_test_split`, fit the classifier to the training data using `fit`, and then make predictions on the testing data using `predict`. Finally, we calculate the accuracy of the model using the `accuracy_score` function.
Note that this is just a basic example, and you can customize your SVM implementation by modifying parameters such as the kernel type, regularization, or hyperparameters tuning.