python xgb模型 预测_如何用一个新句子测试XGB模型?

importmatplotlib.pyplotaspltfromxgboostimportplot_treeimportxgboostasxgbimportpandasaspdimportnumpyasnpimportpickleimportstringimportnltkimportcsvimportosfromsklearnimportdatasetsfromsklearn.model_selectionimporttrain_test_splitfromsklearn.datasetsimportdump_svmlight_filefromsklearn.metricsimportprecision_scorefromsklearn.externalsimportjoblibfromsklearn.metricsimportconfusion_matrixfromsklearn.feature_extraction.textimportTfidfVectorizerfromsklearn.feature_extractionimportDictVectorizerfromsklearn.feature_extraction.textimportCountVectorizerfromnltk.corpusimportstopwordsfromnltk.tokenizeimportword_tokenizedefpp(text):# tokenize into words# remove stopwordsstop=stopwords.words('german')tokens=[wordforsentinnltk.sent_tokenize(text)forwordinnltk.word_tokenize(sent)]tokens=[tokenfortokenintokensiftokennotinstop]# remove words less than three letterstokens=[wordforwordintokensiflen(word)>=3]# lower capitalizationtokens=[word.lower()forwordintokens]# lemmatizelmtzr=nltk.WordNetLemmatizer()tokens=[lmtzr.lemmatize(word)forwordintokens]preprocessed_text=' '.join(tokens)returnpreprocessed_text

df=pd.read_csv("file03.csv",sep=",",usecols=["Security_Flag","Summary"])y=df["Security_Flag"]# from dataframe to array for train test splittingy=y.values

Z=[]forrowindf['Summary']:l=pp(row)Z.append(l)vectorizer=CountVectorizer()X=vectorizer.fit_transform(Z)X=X.toarray()#X = pd.DataFrame(data=X[0:,0:])X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=41)dtrain=xgb.DMatrix(X_train,label=y_train)dtest=xgb.DMatrix(X_test,label=y_test)param={'max_depth':3,# the maximum depth of each tree'eta':0.3,# the training step for each iteration'silent':1,# logging mode - quiet'objective':'multi:softprob',# error evaluation for multiclass training# 'objective': 'binary:logistic', # error evaluation for multiclass training'num_class':2}# the number of classes that exist in this datsetnum_round=20# the number of training iterationsbst=xgb.train(param,dtrain,num_round)preds=bst.predict(dtest)best_preds=np.asarray([np.argmax(line)forlineinpreds])stest=xgb.DMatrix([X_test[0]])spred=bst.predict(stest)print(confusion_matrix(y_test,best_preds))whileTrue:ts=input("Enter a sentence: ")ts=pp(ts)Z.append(ts)Y=vectorizer.fit_transform(Z)Y=Y.toarray()test=xgb.DMatrix([Y[-1]])spred=bst.predict(test)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值