【发布时间】:2018-12-20 07:20:50
【问题描述】:
我正在 Jupyter 中使用 sklearn 运行分类算法。 我想使用 SMOTE,因为我的一组仅占其他两组的 35%。所以我想对那个组(第 1 组)进行过采样,但我不知道如何整合它。 (编辑:我知道 SMOTE 脚本,但我想知道它在下面的脚本中的位置)。 帮忙?
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split (X, y, test_size = 0.1)
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform (X_test)
from sklearn.svm import SVC
clf = SVC(kernel = 'linear')
clf.fit (X_train, y_train.ravel())
y_pred = clf.predict(X_test)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix (y_test, y_pred)
from sklearn.model_selection import cross_val_score
accuracies = cross_val_score (estimator = clf, X = X_train, y = y_train, cv = 10)
accuracies.mean()
accuracies.std()
from sklearn.model_selection import GridSearchCV
parameters = [{'C':[1, 10, 100], 'kernel':['linear']},
{'C':[1, 10, 100],
'kernel':['rbf'],
'gamma': [0.05, 0.001, 0.005]}]
grid_search = GridSearchCV (estimator = clf, param_grid = parameters, scoring = 'accuracy', cv = 10)
grid_search = grid_search.fit (X_train,y_train)
best_accuracy = grid_search.best_score_
print (best_accuracy)
best_parameters = grid_search.best_params_
print (best_parameters)
【问题讨论】:
标签: machine-learning scikit-learn classification