本文共 678 字,大约阅读时间需要 2 分钟。
import osfrom sklearn.ensemble import RandomForestClassifierfrom sklearn import datasetsfrom sklearn.metrics import accuracy_scoreimport picklefrom sklearn.model_selection import train_test_splitsheet = datasets.load_wine()x,y=sheet.data,sheet.targetclf = RandomForestClassifier(n_estimators=14,random_state=1,max_depth=5)xtrain,xtest,ytrain,ytest=train_test_split(x,y,test_size=0.2,random_state=1)clf.fit(xtrain,ytrain)# 保存模型file_path = os.path.dirname(os.path.realpath(__file__))with open ('clf.pickle', 'wb') as f: pickle.dump(clf,f)# 读取模型with open ('clf.pickle', 'rb') as f: clf = pickle.load(f) y_pred=clf.predict(xtest) print(accuracy_score(ytest,y_pred))```python在这里插入代码片
转载地址:http://ywhws.baihongyu.com/