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)