Professional Documents
Culture Documents
Support Vector Machine A) Classification
Support Vector Machine A) Classification
a)Classification
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
dataset=pd.read_csv("BreastCancer.csv")
data=dataset.iloc[:,2:]
target=dataset.iloc[:,1:2]
X_train,X_test,Y_train,Y_test=train_test_split(data,target,test_size=0.03)
clf = SVC(kernel='linear')
clf.fit(X_train,Y_train)
Y_pred=clf.predict(X_test)
print("Actual Output:\n",Y_test)
print("Predicted Output:",Y_pred)
print("Confusion Matrix:")
print(confusion_matrix(Y_test,Y_pred))
print(classification_report(Y_test,Y_pred))
b)Regression
print(X)
print(y)
print("\n \n")
y = y.reshape(len(y),1)
print(y)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)
print(X)
print(y)
# Training the SVR model on the whole dataset
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)
# Predicting a new result
print("New Value")
print(sc_y.inverse_transform(regressor.predict(sc_X.transform([[6.5]])).res
hape(-1,1)))
# Visualising the SVR results
plt.scatter(sc_X.inverse_transform(X), sc_y.inverse_transform(y).reshape(-
1,1), color = 'red')
plt.plot(sc_X.inverse_transform(X),
sc_y.inverse_transform(regressor.predict(X).reshape(-1,1)), color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()