<jupyter_start><jupyter_text>Classification<jupyter_code>y_label = "party"
bias_cols = [col for col in labelled_tweets if col.endswith("_bias")]
intensity_cols = [col for col in labelled_tweets if col.endswith("_inte")]
moral_cols = bias_cols + intensity_cols
Y = labelled_tweets[y_label].values
X = labelled_tweets[moral_cols].values
print(X.shape)
print(np.expand_dims(Y, axis=1).shape)
np.concatenate([X, np.expand_dims(Y, axis=1)], axis=1)
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, shuffle=True)
classifier = LogisticRegression(max_iter=1000).fit(X_train, Y_train)
    
Y_pred = classifier.predict(X_test)
classification_report(Y_test, Y_pred, output_dict=True)
print(classification_report(Y_test, Y_pred))
classifier.coef_
coef_df = pd.DataFrame.from_records(classifier.coef_, index=classifier.classes_, columns=moral_cols)
print(coef_df.to_latex())
coef_df
coef_df = coef_df.stack().to_frame().reset_index()
coef_df[["moral", "bias/inte"]] = coef_df["level_1"].str.split("_", expand=True)
coef_df = coef_df.drop(columns=["level_1"])
coef_df = coef_df.rename(columns={"level_0": "handle", 0: "coef"})
coef_df
bias_df = coef_df[coef_df["bias/inte"] == "bias"]
sns.pointplot(data=bias_df, x="moral", y="coef", hue="handle")
inte_df = coef_df[coef_df["bias/inte"] == "inte"]
sns.pointplot(data=inte_df, x="moral", y="coef", hue="handle")
# pairplot on traindata: https://scikit-learn.org/stable/auto_examples/inspection/plot_linear_model_coefficient_interpretation.html
dataset = labelled_tweets[[y_label] + moral_cols].copy()
dataset  # output clear for privacy
ds = dataset.sample(10_000)
%%time
g = sns.pairplot(ds, kind="reg", diag_kind="kde", hue="party")
g.map_lower(sns.kdeplot, levels=4, color=".2")
plt.savefig("pairplot_us.pdf", dpi=300)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.143888354186718, 'fraction_numerical': 0.011549566891241578, 'mean_word_length': 8.625, 'pattern_counts': {'":': 1, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '45681', 'license': 'MIT', 'n_tokens_mistral': 817, 'n_tokens_neox': 787, 'n_words': 131, 'path': '3_moral_based_framing_in_us_based_tweets.ipynb', 'repo_name': 'socialcomplab/icwsm21-framing', 'size': '2078'}