"How to create/customize your own scorer function in scikit-learn with GridSearch" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "How to create/customize your own scorer function in scikit-learn with GridSearch" answered properly. Developers are finding an appropriate answer about How to create/customize your own scorer function in scikit-learn with GridSearch related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like How to create/customize your own scorer function in scikit-learn with GridSearch. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on How to create/customize your own scorer function in scikit-learn with GridSearch.
How to create/customize your own scorer function in scikit-learn with GridSearch
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import make_scorer
from sklearn.svm import SVR
import numpy as np
rng = np.random.RandomState(1)
def my_custom_loss_func(X_train_scaled, Y_train_scaled):
error, M = 0, 0
for i in range(0, len(Y_train_scaled)):
z = (Y_train_scaled[i] - M)
if X_train_scaled[i] > M and Y_train_scaled[i] > M and (X_train_scaled[i] - Y_train_scaled[i]) > 0:
error_i = (abs(Y_train_scaled[i] - X_train_scaled[i]))**(2*np.exp(z))
if X_train_scaled[i] > M and Y_train_scaled[i] > M and (X_train_scaled[i] - Y_train_scaled[i]) < 0:
error_i = -(abs((Y_train_scaled[i] - X_train_scaled[i]))**(2*np.exp(z)))
if X_train_scaled[i] > M and Y_train_scaled[i] < M:
error_i = -(abs(Y_train_scaled[i] - X_train_scaled[i]))**(2*np.exp(-z))
error += error_i
return error
# Generate sample data
X = 5 * rng.rand(10000, 1)
y = np.sin(X).ravel()
# Add noise to targets
y[::5] += 3 * (0.5 - rng.rand(X.shape[0]/5))
train_size = 100
my_scorer = make_scorer(my_custom_loss_func, greater_is_better=True)
svr = GridSearchCV(SVR(kernel='rbf', gamma=0.1),
scoring=my_scorer,
cv=5,
param_grid={"C": [1e0, 1e1, 1e2, 1e3],
"gamma": np.logspace(-2, 2, 5)})
svr.fit(X[:train_size], y[:train_size])
print svr.best_params_
print svr.score(X[train_size:], y[train_size:])
Source: stackoverflow.com
All those coders who are working on the Whatever based application and are stuck on How to create/customize your own scorer function in scikit-learn with GridSearch can get a collection of related answers to their query. Programmers need to enter their query on How to create/customize your own scorer function in scikit-learn with GridSearch related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about How to create/customize your own scorer function in scikit-learn with GridSearch for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of How to create/customize your own scorer function in scikit-learn with GridSearch while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "How to create/customize your own scorer function in scikit-learn with GridSearch". Visit this developer's friendly online web community, CodeProZone, and get your queries like How to create/customize your own scorer function in scikit-learn with GridSearch resolved professionally and stay updated to the latest Whatever updates.