-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapters_words.py
77 lines (67 loc) · 2.92 KB
/
chapters_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pandas as pd
import pyreadr
import numpy as np
from sklearn import preprocessing
from sklearn.cluster import SpectralBiclustering
import matplotlib.pyplot as plt
def preprocess_data(data, key):
'''
Preprocesses data given the dictionary and key read from pyreadr
Inputs:
data: the dictionary of data as read by pyreadr
key: which DataFrame to access and process
Outputs:
scaled_df: the processed dataset
'''
# Use data.keys() to identify there is only one key: 'authors'
dfauthors = data[key]
# Four authors: 'Austen', 'London', 'Milton', 'Shakespeare'
scaler = preprocessing.StandardScaler()
scaled_df = pd.DataFrame(scaler.fit_transform(dfauthors), columns=dfauthors.columns)
return dfauthors, scaled_df
def biclustering(scaled_df, dfauthors):
'''
Biclustering for both the book chapters and stop words simultaneously
Inputs:
scaled_df: the preprocessed DataFrame
dfauthors: the original dataset for obtaining labels
Outputs:
Plots the data after biclustering and after rearranging to
obtain a checkerboard structure
'''
# Fits the Biclustering model
n_clusters = (4, 4)
model = SpectralBiclustering(n_clusters=n_clusters)
model.fit(scaled_df)
scaled_df = scaled_df.to_numpy()
fit_data = scaled_df[np.argsort(model.row_labels_)]
fit_data = fit_data[:, np.argsort(model.column_labels_)]
# Plots the biclustering results
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.matshow(fit_data, cmap=plt.cm.Blues, aspect='auto')
ax.set_title("Biclustering on Book Chapters and Stop Words")
ax.set_xticks(np.arange(dfauthors.columns.size))
ax.set_yticks(np.arange(0, dfauthors.index.values.size, 30))
ax.set_xticklabels(dfauthors.columns[np.argsort(model.column_labels_)], rotation=90)
ax.set_yticklabels(dfauthors.index.values[np.argsort(model.row_labels_)][::30])
fig.savefig('Results Chapters & Words/BothBookWordsBiClustering1.png')
plt.show()
# Rearranges the biclustered data to obtain a checkerboard structure
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.matshow(
np.outer(np.sort(model.row_labels_) + 1, np.sort(model.column_labels_) + 1),
cmap=plt.cm.Blues,
aspect='auto'
)
ax.set_title("Checkerboard structure of rearranged data")
ax.set_xticks(np.arange(dfauthors.columns.size))
ax.set_yticks(np.arange(0, dfauthors.index.values.size, 30))
ax.set_xticklabels(dfauthors.columns[np.argsort(model.column_labels_)], rotation=90)
ax.set_yticklabels(dfauthors.index.values[np.argsort(model.row_labels_)][::30])
fig.savefig('Results Chapters & Words/BothBookWordsBiClustering2.png')
plt.show()
data = pyreadr.read_r('authors.rda')
dfauthors, scaled_df = preprocess_data(data, 'authors')
biclustering(scaled_df, dfauthors)