Error in running SVM and Logistic Regression in python
Question:
I am trying to tune the parameter between the SVM, Logistic regression, MLP and Random forest regression in the python but it shows a value error for SVM and logistic regression.
my sample data is this:
Wavelength Phase_velocity Shear_wave_velocity
1.50 202.69 240.73
1.68 192.72 240.73
1.79 205.54 240.73
17.08 218 229
16.73 243 269
17.72 245 269
16.72 212 253
17.26 214 253
........
The example code is:
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
import numpy as np
import pandas as pd
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
df = pd.read_csv("0.5-1.csv")
df.head()
X = df[['wavelength', 'phase velocity']]
y = df['shear wave velocity']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
print (len(X_train),len(X_test),len(y_train),len(y_test))
lr = LogisticRegression(solver='liblinear',multi_class='ovr')
lr.fit(X_train, y_train)
print (lr.score(X_test, y_test))
svm = SVC(gamma='auto')
svm.fit(X_train, y_train)
print (svm.score(X_test, y_test))
mlp = MLPRegressor(hidden_layer_sizes=(50,50,50), max_iter=2000, activation='relu')
mlp.fit(X_train,y_train)
print (mlp.score(X_test, y_test))
rf = RandomForestRegressor(n_estimators=40)
rf.fit(X_train, y_train)
print (rf.score(X_test, y_test))
the error is this:
Traceback (most recent call last):
File "G:My DriveANNtest .5-1 .5-1_tunecode.py", line 23, in <module>
lr.fit(X_train, y_train)
File "C:UserssadiaAppDataLocalProgramsPythonPython36libsite-packagessklearnlinear_modellogistic.py", line 1533, in fit
check_classification_targets(y)
File "C:UserssadiaAppDataLocalProgramsPythonPython36libsite-packagessklearnutilsmulticlass.py", line 169, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'
How to fix this error?
Answers:
Since your target variable is continuous in nature, you cannot use logisticRegression
, use a linearRegression
or SVR
instead of SVC
.
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
I am trying to tune the parameter between the SVM, Logistic regression, MLP and Random forest regression in the python but it shows a value error for SVM and logistic regression.
my sample data is this:
Wavelength Phase_velocity Shear_wave_velocity
1.50 202.69 240.73
1.68 192.72 240.73
1.79 205.54 240.73
17.08 218 229
16.73 243 269
17.72 245 269
16.72 212 253
17.26 214 253
........
The example code is:
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
import numpy as np
import pandas as pd
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
df = pd.read_csv("0.5-1.csv")
df.head()
X = df[['wavelength', 'phase velocity']]
y = df['shear wave velocity']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
print (len(X_train),len(X_test),len(y_train),len(y_test))
lr = LogisticRegression(solver='liblinear',multi_class='ovr')
lr.fit(X_train, y_train)
print (lr.score(X_test, y_test))
svm = SVC(gamma='auto')
svm.fit(X_train, y_train)
print (svm.score(X_test, y_test))
mlp = MLPRegressor(hidden_layer_sizes=(50,50,50), max_iter=2000, activation='relu')
mlp.fit(X_train,y_train)
print (mlp.score(X_test, y_test))
rf = RandomForestRegressor(n_estimators=40)
rf.fit(X_train, y_train)
print (rf.score(X_test, y_test))
the error is this:
Traceback (most recent call last):
File "G:My DriveANNtest .5-1 .5-1_tunecode.py", line 23, in <module>
lr.fit(X_train, y_train)
File "C:UserssadiaAppDataLocalProgramsPythonPython36libsite-packagessklearnlinear_modellogistic.py", line 1533, in fit
check_classification_targets(y)
File "C:UserssadiaAppDataLocalProgramsPythonPython36libsite-packagessklearnutilsmulticlass.py", line 169, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous'
How to fix this error?
Since your target variable is continuous in nature, you cannot use logisticRegression
, use a linearRegression
or SVR
instead of SVC
.
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR