-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVM.py
36 lines (27 loc) · 987 Bytes
/
SVM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pandas as pd
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
# Load the data into a DataFrame
data = pd.read_csv('data.csv')
# Drop any rows with missing values
data = data.dropna()
# Encode any categorical features using a label encoder
le = LabelEncoder()
for col in data.columns:
if data[col].dtype == 'object':
data[col] = le.fit_transform(data[col])
# Split the data into features and target
X = data.drop('target', axis=1)
y = data['target']
# 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)
# Create a support vector machine classifier
clf = SVC()
# Train the classifier on the training data
clf = clf.fit(X_train, y_train)
# Predict the labels of the test data
y_pred = clf.predict(X_test)
# Evaluate the accuracy of the classifier
accuracy = clf.score(X_test, y_test)
print(f'Accuracy: {accuracy}')