import pandas as pd
负责数据的读写和初步处理
from sklearn.linear_model import LogisticRegression
调用线性回归模型logistic
from sklearn.feature_extraction_text import CountVectorize
调用文本特征提取中的统计词频功能
df_train=pd.read_csv(‘./train_set.csv’)
df_test=pd.read_csv(‘./test_set.csv’)
df_train.drop(columns=[‘Id’ ,’article’],inplace=True)
df_test.drop(columns=[‘article ’],inplace=True)
vectorize=CountVectorize(ngram_rang=(1,2),min_df=3,max_df=0.9,max_features=100000)
vectorize.fit(df_train[‘word_seg’])
x_train=vectorize.transform(df_train[‘word_seg’])
y_train=df_train[‘class’]-1
x_test=vectorize.transform(df_test[‘word_seg’])
lg=LogisticRegression(C=4,dual=True)
lg.fit(x_train,y_train)
y_test=lg.predict(x_test)
df_test[‘class’]=y_test.tolist()
df_test[‘class’]+=1
df_result=df_test.loc[:,[‘Id ’,’class’]]
df_result.to_csv(‘result.csv’,index=False)
网友评论