<jupyter_start><jupyter_text>数据预处理<jupyter_code>df = pd.read_excel('data_processed.xlsx')
drop_cols = ['发帖吧','情绪','多空','发帖id','发帖内容','查询时间','发帖时间']
def preprocess(df, drop_cols):
    word_number = []
    for i in df['发帖内容']:
        word_number.append(len(str(i)))
    df['发帖字数'] = word_number
    df.drop(columns=drop_cols,inplace=True)
    df = df.replace('NAN',0)
    return df

df = preprocess(df, drop_cols)
df = df[df['身份']!=0]
print(df.isnull().sum())
df.info()
# LabelEncoder
encoded_df = df.drop(columns='身份').apply(LabelEncoder().fit_transform)
encoded_df.head()

# Onehot
cols = ['发帖人']
onehot_df = pd.get_dummies(df, columns=cols)
onehot_df.drop(columns=['身份'],inplace=True)
onehot_df.head()<jupyter_output><empty_output><jupyter_text>CART决策树预测模型<jupyter_code>X_le = encoded_df.values
Y_le = df['身份']
X_train_le, X_test_le, y_train_le, y_test_le = train_test_split(X_le, Y_le, test_size=0.3, random_state=88)
X_oh = onehot_df.values
Y_oh = df['身份']
X_train_oh, X_test_oh, y_train_oh, y_test_oh = train_test_split(X_oh, Y_oh, test_size=0.3, random_state=88)

DTC_oh = tree.DecisionTreeClassifier(max_depth=20,min_samples_leaf=10)
DTC_oh.fit(X_train_oh,y_train_oh.ravel())
DTC_oh_pred = DTC_oh.predict(X_test_oh)
print('The accuracy of OneHot DecisionTree is', metrics.accuracy_score(DTC_oh_pred, y_test_oh))
print('特征重要性排序前五：',onehot_df.columns[DTC_oh.feature_importances_.argsort()[::-1][0:5]])
#print(DTC_oh.feature_importances_)

DTC_le = tree.DecisionTreeClassifier(max_depth=20,min_samples_leaf=10,max_features=16,max_leaf_nodes=10)
DTC_le.fit(X_train_le,y_train_le.ravel())
DTC_le_pred = DTC_le.predict(X_test_le)
print('The accuracy of LabelEncoder DecisionTree is', metrics.accuracy_score(DTC_le_pred, y_test_le))
print('特征重要性排序前五：',encoded_df.columns[DTC_le.feature_importances_.argsort()[::-1][0:5]])
#print(DTC_le.feature_importances_)<jupyter_output>The accuracy of OneHot DecisionTree is 0.9673590504451038
特征重要性排序前五： Index(['发帖字数', '发帖人粉丝数', '阅读量', '发帖人_李李杰杰', '发帖人总访问量'], dtype='object')
The accuracy of LabelEncoder DecisionTree is 0.9614243323442137
特征重要性排序前五： Index(['发帖字数', '发帖人粉丝数', '阅读量', '发帖人总访问量', '发帖人今日访问量'], dtype='object')<jupyter_text>随机森林预测结果<jupyter_code>encoded_user = df[['发帖人']].apply(LabelEncoder().fit_transform)
df['发帖人'] = encoded_user
X = np.array(df.drop(columns='身份'))
y = np.array(df['身份'])
X_train_rf, X_test_rf, y_train_rf, y_test_rf = train_test_split(X, y, test_size=0.2, random_state=7)

rfc = RandomForestClassifier(n_estimators=20,max_depth=15,max_leaf_nodes=20)
rfc.fit(X_train_rf,y_train_rf.ravel())
rfc_pred = rfc.predict(X_test_rf)
print('The accuracy of RandomForest is', metrics.accuracy_score(rfc_pred, y_test_rf))
print('特征重要性排序前五：',encoded_df.columns[rfc.feature_importances_.argsort()[::-1][0:5]])
print('Feature Importance:')
dict(zip(encoded_df.columns,rfc.feature_importances_))<jupyter_output>The accuracy of RandomForest is 0.9799554565701559
特征重要性排序前五： Index(['发帖字数', '发帖人评论数', '发帖人粉丝数', '阅读量', 'Time_delta'], dtype='object')
Feature Importance:<jupyter_text>神经网络预测效果<jupyter_code>X_train_mlp, X_test_mlp, y_train_mlp, y_test_mlp = train_test_split(X, y, test_size=0.2, random_state=7)

mlp = MLPClassifier(hidden_layer_sizes=(100,20),alpha=0.0001,max_iter=600,solver='lbfgs') 
mlp.fit(X_train_mlp,y_train_mlp.ravel())
mlp_pred = mlp.predict(X_test_mlp)
print('The loss on training set is',mlp.loss_)
print('The accuracy of Multi-layer Perceptron Classifier is', metrics.accuracy_score(mlp_pred, y_test_mlp))

matrix_train = metrics.confusion_matrix(y_train_mlp,mlp.predict(X_train_mlp))
print('Confusion matrix on training set：\n',matrix_train)
report_train = metrics.classification_report(y_train_mlp,mlp.predict(X_train_mlp))
print('Classification on training set：\n',report_train)<jupyter_output>The loss on training set is 0.2660709443164722
The accuracy of Multi-layer Perceptron Classifier is 0.9192139737991266
Confusion matrix on training set：
 [[1522  119]
 [  35  152]]
Classification on training set：
               precision    recall  f1-score   support

           1       0.98      0.93      0.95      1641
           2       0.56      0.81      0.66       187

    accuracy                           0.92      1828
   macro avg       0.77      0.87      0.81      1828
weighted avg       0.93      0.92      0.92      1828<jupyter_text>CrossValidation参数调优并使用调优后的参数再次进行上述预测<jupyter_code>from sklearn.model_selection import GridSearchCV
param_grid = {'n_estimators': [10,15,20],
              'max_depth': [10,15,20],
              'max_leaf_nodes': [20,30,40]}
rfc = RandomForestClassifier()
rfc_cv = GridSearchCV(rfc, param_grid=param_grid, cv = 10)
rfc_cv.fit(X, y.ravel())
print('Best hyper-parameter of RandomForrest model:',rfc_cv.best_params_)
print(rfc_cv.best_score_)

param_grid = {'alpha': [0.01,0.001,0.0001],
              'hidden_layer_sizes': [(100,),(100,20),(50,10),(30,10)]}
mlp = MLPClassifier(max_iter = 1000)
mlp_cv = GridSearchCV(mlp, param_grid=param_grid, cv = 3)
mlp_cv.fit(X, y.ravel())
print('Best hyper-parameter of MLP model:',mlp_cv.best_params_)
print(mlp_cv.best_score_)

param_grid = {'max_features': [10,15,16],
              'max_depth': [15,20,25,30],
              'max_leaf_nodes': [5,10,15,20,30,40]}
dtc = tree.DecisionTreeClassifier()
dtc_cv = GridSearchCV(dtc, param_grid=param_grid, cv = 10)
dtc_cv.fit(X_le, Y_le.ravel())
print('Best hyper-parameter of DecisionTree on LabelEncoder model:',dtc_cv.best_params_)
print(dtc_cv.best_score_)
dtc_cv.fit(X_oh, Y_oh.ravel())
print('Best hyper-parameter of DecisionTree on OneHot model:',dtc_cv.best_params_)
print(dtc_cv.best_score_)
scores = [DTC_le.score(X_test_le,y_test_le),
          DTC_oh.score(X_test_oh,y_test_oh), 
          rfc.score(X_test_rf,y_test_rf), 
          mlp.score(X_test_mlp,y_test_mlp)]
score_df = pd.DataFrame({
    'Model' : ['Decision Tree on LabelEncoder','Decision Tree on OneHot','Random Forest','MLP'],
    'Scores' : scores
})
score_df.set_index('Model')
score_df
# 将评论内容写成分开的文本进行人工情绪分类
reviews = pd.read_excel('data_processed.xlsx')[['label','发帖内容','情绪','多空']]
reviews = reviews[reviews['label']==1].reset_index().drop(columns=['index','label'])[['发帖内容','情绪','多空']]

# 情绪文本分类
for i in range(-1,2):
    reviews_i = reviews[reviews['情绪']==i] 
    if i == 1:
        for line in range(len(reviews_i)):
            f = open('train/pos/{}_1.txt'.format(line),'w+') # labeled comments
            f.write(reviews_i[i])
            f.close()
    elif i == 0:
        for line in range(len(reviews_i)):
            f = open('train/neu/{}_0.txt'.format(line),'w+') # labeled comments
            f.write(reviews_i[i])
            f.close()      
    else:
        for line in range(len(reviews_i)):
            f = open('train/neg/{}_-1.txt'.format(line),'w+') # labeled comments
            f.write(reviews_i[i])
            f.close()
# 多空分类
for i in range(-1,3):
    reviews_i = reviews[reviews['多空']==i] 
    if i == 1:
        for line in range(len(reviews_i)):
            f = open('train/pos/{}_1.txt'.format(line),'w+') # labeled comments
            f.write(reviews_i[i])
            f.close()
    elif i == 0:
        for line in range(len(reviews_i)):
            f = open('train/null/{}_0.txt'.format(line),'w+') # labeled comments
            f.write(reviews_i[i])
            f.close()      
    elif: i == -1
        for line in range(len(reviews_i)):
            f = open('train/neg/{}_-1.txt'.format(line),'w+') # labeled comments
            f.write(reviews_i[i])
            f.close()
    else:
        for line in range(len(reviews_i)):
            f = open('train/Undefined/{}_2.txt'.format(line),'w+') # labeled comments
            f.write(reviews_i[i])
            f.close()
test = pd.read_excel('test.xlsx')
drop_cols = ['发帖吧','发帖id','查询时间']
test = preprocess(test, drop_cols)

encoded_test = test[['发帖人']].apply(LabelEncoder().fit_transform)
test['发帖人'] = encoded_test
X_test = np.array(test.drop(columns=['发帖内容','发帖时间']))

y_pred = rfc.predict(X_test)
test['身份'] = y_pred
test.to_excel('identity_labeled.xlsx')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15493306891422906, 'fraction_numerical': 0.038547347545860186, 'mean_word_length': 4.611265646731572, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '31081', 'license': 'MIT', 'n_tokens_mistral': 3481, 'n_tokens_neox': 3404, 'n_words': 484, 'path': 'code/Identity Classification.ipynb', 'repo_name': 'Sunny-Island/Text-based-sentiment-factor', 'size': '8068'}