SVM(Support Vector Machines) is a supervised machine learning algorithm. More information about it can be found here.
You can learn more about SVM in the below video.
The below code will help you to create an SVM model for regression use cases.
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#### Create Gym Data for regression in Python #### import pandas as pd import numpy as np ColumnNames=['Hours','Calories', 'Weight'] DataValues=[[ 1.0, 2500, 95], [ 2.0, 2000, 85], [ 2.5, 1900, 83], [ 3.0, 1850, 81], [ 3.5, 1600, 80], [ 4.0, 1500, 78], [ 5.0, 1500, 77], [ 5.5, 1600, 80], [ 6.0, 1700, 75], [ 6.5, 1500, 70]] #Create the Data Frame GymData=pd.DataFrame(data=DataValues,columns=ColumnNames) GymData.head() #Separate Target Variable and Predictor Variables TargetVariable='Weight' Predictors=['Hours','Calories'] X=GymData[Predictors].values y=GymData[TargetVariable].values #Split the data into training and testing set from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ################################################################## ###### Support Vector Machines(SVM) Regression in Python ####### import pandas as pd from sklearn import svm RegModel = svm.SVR(C=2, kernel='linear') #Printing all the parameters of KNN print(RegModel) #Creating the model on Training Data SVM=RegModel.fit(X_train,y_train) prediction=SVM.predict(X_test) #Measuring Goodness of fit in Training data from sklearn import metrics print('R2 Value:',metrics.r2_score(y_train, SVM.predict(X_train))) #Measuring accuracy on Testing Data print('Accuracy',100- (np.mean(np.abs((y_test - prediction) / y_test)) * 100)) #Plotting the feature importance for Top 10 most important columns #The built in attribute SVM.coef_ works only for linear kernel %matplotlib inline feature_importances = pd.Series(SVM.coef_[0], index=Predictors) feature_importances.nlargest(10).plot(kind='barh') #Printing some sample values of prediction TestingDataResults=pd.DataFrame(data=X_test, columns=Predictors) TestingDataResults[TargetVariable]=y_test TestingDataResults[('Predicted'+TargetVariable)]=prediction TestingDataResults.head() |
Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!