I'm trying to do hyperparameter tuning and every time I run this code.
from sklearn.model_selection import GridSearchCV
param_grid = {'C':[0,1,1,100,1000], 'kernel':['rbf','poly','sigmoid','linear'],'degree':[1,2,3,4,5,6]}
grid =GridSearchCV(svc.sc(),param_grid)
grid.fit(X_train,y_train)
I get this error
TypeError Traceback (most recent call last)
<ipython-input-64-74de9eeb3cae> in <module>
3
4 param_grid = {'C':[0,1,1,100,1000], 'kernel':['rbf','poly','sigmoid','linear'],'degree':[1,2,3,4,5,6]}
----> 5 grid =GridSearchCV(svc.sc(),param_grid)
6 grid.fit(X_train,y_train)
TypeError: 'numpy.float64' object is not callable
Any idea what to do? Also svc.sc is the way defined the model.
What is svc.sc()? Either way, you're probably not meant to call it at that point, just pass it as the callback to GridSearchCV, i.e. drop the parentheses:
grid = GridSearchCV(svc.sc, param_grid)
Related
This document shows that a XGBoost API trained model can be sliced by following code:
from sklearn.datasets import make_classification
import xgboost as xgb
booster = xgb.train({
'num_parallel_tree': 4, 'subsample': 0.5, 'num_class': 3},
num_boost_round=num_boost_round, dtrain=dtrain)
sliced: xgb.Booster = booster[3:7]
I tried it and it worked.
Since XGBoost provides Scikit-Learn Wrapper interface, I tried something like this:
from xgboost import XGBClassifier
clf_xgb = XGBClassifier().fit(X_train, y_train)
clf_xgb_sliced: clf_xgb.Booster = booster[3:7]
But got following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-18-84155815d877> in <module>
----> 1 clf_xgb_sliced: clf_xgb.Booster = booster[3:7]
AttributeError: 'XGBClassifier' object has no attribute 'Booster'
Since XGBClassifier has no attribute 'Booster', is there any way to slice a Scikit-Learn Wrapper interface trained XGBClassifier(/XGBRegressor) model?
The problem is with the type hint you are giving clf_xgb.Booster which does not match an existing argument. Try:
clf_xgb_sliced: xgb.Booster = clf_xgb.get_booster()[3:7]
instead.
I tried to use the code below for fitting a robust regression model using RANSAC
from sklearn.linear_model import RANSACRegressor
ransac = RANSACRegressor(LinearRegression(),
max_trials=100,
min_samples=50,
residual_metric=lambda x: np.sum(np.abs(x), axis=1),
residual_threshold=5.0,
random_state=0)
ransac.fit(X,y)
And I get the following error below:
TypeError Traceback (most recent call last)
<ipython-input-38-832d8b5d351b> in <module>
5 residual_metric=lambda x: np.sum(np.abs(x), axis=1),
6 residual_threshold=5.0,
----> 7 random_state=0)
8 ransac.fit(X,y)
TypeError: __init__() got an unexpected keyword argument 'residual_metric'
Can you help me know what's wrong?
Most likely you got this code that was using an old version of ransac. The input residual_metric is deprecated. If you run without that, it works ok:
from sklearn.linear_model import RANSACRegressor, LinearRegression
ransac = RANSACRegressor(LinearRegression(),
max_trials=100,
min_samples=50,
residual_threshold=5.0,
random_state=0)
ransac
RANSACRegressor(base_estimator=LinearRegression(), min_samples=50,
random_state=0, residual_threshold=5.0)
I am new to Pyspark. I am using logistic regression API. I followed some tutorials and worked this way :
from pyspark.ml.classification import LogisticRegression
train, test = df.randomSplit([0.80, 0.20], seed = some_seed)
LR = LogisticRegression(featuresCol = 'features', labelCol = 'label', maxIter=some_iter)
LR_model = LR.fit(train)
When I call
trainingSummary = LR_model.summary
trainingSummary.roc
I get
--------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-319-bf79768ab64e> in <module>()
1 trainingSummary = LR_model.summary
2
----> 3 trainingSummary.roc
AttributeError: 'LogisticRegressionTrainingSummary' object has no attribute 'roc'
Someone has an idea ?
Why does RidgeClassifierCV from sklearn.linear_model not have a random_state keyword argument?
It cross-validates RidgeClassifier, which does have random_state. Should this keyword not be passed on? Or am I missing something here?
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-612-4d3d523acbab> in <module>()
1 # Apply ridge regression
----> 2 model = linear_model.RidgeClassifierCV(cv=3, normalize=True, random_state=random_state)
3 model.fit(Xtrain, ytrain)
4 model = linear_model.RidgeClassifier(alpha=model.alpha_, normalize=True,
5 random_state=random_state)
TypeError: __init__() got an unexpected keyword argument 'random_state'
I would like to use different parameters of scikit's SVC classifier with cross-vlidation, so I tried the following:
Then, let's use SVC algorithm:
from sklearn import svm
print('Support vector machine(SVM): {:.2f}'.format(metrics.accuracy_score(
y, stratified_cv(X, y, svm.SVC(kernel='linear')))))
But it seems I can not access to the object:
AttributeError Traceback (most recent call last)
<ipython-input-16-dacd8d429376> in <module>()
5
6 print('Support vector machine(SVM): {:.2f}'.format(metrics.accuracy_score(
----> 7 y, stratified_cv(X, y, svm.SVC(kernel='linear')))))
8
AttributeError: 'SVC' object has no attribute 'SVC'
Interestingly, when I try this:
print('Support vector machine(SVM): {:.2f}'.format(metrics.accuracy_score(
y, stratified_cv(X, y, svm.SVC))))
I get:
Support vector machine(SVM): 0.46
What could be happening?...any idea of given the above cross validation strategy, how to set up my own SVM configuration?. Thanks in advance guys!
You need a partial from python. In general, your function requires you to pass something that can be called with clf_class(**kwargs), so if you pass a particular object (obtained through clf = SVC(kernel='linear')) it won't work, as you try to do
SVC(kernel='linear')(**kwargs) # error!
you want to call
SVC(kernel='linear', **kwargs)
so you can declare the partial function in python
from functools import partial
linear_svm = partial(svm.SVC, kernel='linear')
and now you can call
linear_svm(**kwargs)