import numpy as np import joblib from imblearn.over_sampling import RandomOverSampler train_X = np.load(r"/home/renhaitao/code/train_rhtX.npy") train_y = np.load(r"/home/renhaitao/code/train_rhty.npy") test_X = np.load(r"/home/renhaitao/code/test_rhtX.npy") test_y = np.load(r"/home/renhaitao/code/test_rhty.npy") ros = RandomOverSampler(random_state=0) train_X, train_y = ros.fit_resample(train_X, train_y) # 数据集来源环境自己改 def svm(): from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.svm import LinearSVC svm_clf = Pipeline([ ("scaler", StandardScaler()), ("linear_svc", LinearSVC(max_iter=1000, C=0.5, loss="hinge")) ]) return svm_clf def rf(): from sklearn.ensemble import RandomForestClassifier rnd_clf = RandomForestClassifier(max_depth=50, n_estimators=500, n_jobs=50, max_features='sqrt') return rnd_clf def lr(): from sklearn.linear_model import LogisticRegression lr_clf = LogisticRegression(penalty='l2', max_iter=500, n_jobs=200, C=0.5, solver='sag') return lr_clf def lrcv(): from sklearn.linear_model import LogisticRegressionCV lr_clf = LogisticRegressionCV(penalty='l2', max_iter=500, n_jobs=200, C=0.5, solver='sag',cv=5) return lr_clf def knn(): from sklearn.neighbors import KNeighborsClassifier knn_clf = KNeighborsClassifier(n_neighbors=20, n_jobs=50) return knn_clf def vc(): from sklearn.ensemble import VotingClassifier vc_lr = lr() vc_rf = rf() vc_svm = svm() vc_clf = VotingClassifier( estimators=[('lr', vc_lr), ('rf', vc_rf), ('svc', vc_svm)], voting='hard' ) return vc_clf print(train_y.shape) print("调用模型") model = vc() print("开始训练") model.fit(train_X, train_y) print("训练结束") joblib.dump(model, 'vc.model') print(model.score(train_X, train_y)) print(model.score(test_X, test_y))
上传一些sklearn的小实践
最新推荐文章于 2024-08-22 20:57:06 发布