<jupyter_start><jupyter_text>Different linguistics featuresIn this notebook, I see what happens if we use different linguistic features with QVEC.<jupyter_code>%matplotlib inline
import os
import csv
import pandas as pd
import numpy as np
from scipy import stats
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

data_path = '../../data'
tmp_path = '../../tmp'<jupyter_output>/home/bacon/miniconda3/lib/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
  from pandas.core import datetools<jupyter_text>Learnt embeddings<jupyter_code>size = 50
fname = 'embeddings/glove.6B.{}d.txt'.format(size)
embedding_path = os.path.join(data_path, fname)
embeddings = pd.read_csv(embedding_path, sep=' ', header=None, index_col=0, quoting=csv.QUOTE_NONE).T<jupyter_output><empty_output><jupyter_text>QVEC model<jupyter_code>def qvec(features, embeddings):
    """
    Returns correlations between columns of `features` and `embeddings`.
    
    The aligned feature is the one with the highest correlation.
    The qvec score is the sum of correlations of aligned features.
    """
    common_words = embeddings.columns.intersection(features.columns)
    S = features.loc[:,common_words]
    X = embeddings.loc[:,common_words]
    correlations = pd.DataFrame({i:X.corrwith(S.iloc[i], axis=1) for i in range(len(S))})
    correlations.columns = S.index
    return correlations
def qvec_for_large_features(features, embeddings, k=100):
    """
    Copy of `qvec` that only looks at most populated features.
    """
    common_words = embeddings.columns.intersection(features.columns)
    top_k_features = features.sum(axis=1).nlargest(k).index
    S = features.loc[top_k_features,common_words]
    X = embeddings.loc[:,common_words]
    correlations = pd.DataFrame({i:X.corrwith(S.iloc[i], axis=1) for i in range(len(S))})
    correlations.columns = S.index
    return correlations<jupyter_output><empty_output><jupyter_text>FrameNet - frame relations<jupyter_code>fname = os.path.join(data_path, 'evaluation/framenet/framenet-normalized-relations.csv')
relations = pd.read_csv(fname, index_col=0)
relations.head()
correlations = qvec_for_large_features(relations, embeddings, k=100)
V = len(embeddings.columns.intersection(relations.columns))
correlations.head()<jupyter_output><empty_output><jupyter_text>What dimensions and features are aligned?<jupyter_code>alignments = pd.DataFrame(correlations.idxmax(axis=1))
alignments.columns = ['feature']
alignments['max_corr'] = correlations.max(axis=1)
alignments.sort_values(by='max_corr', ascending=False).head(10)
alignments['feature'].value_counts().to_frame().head()<jupyter_output><empty_output><jupyter_text>What is QVEC doing?<jupyter_code>common_words = embeddings.columns.intersection(relations.columns)
S = relations[common_words]
X = embeddings[common_words]
def plot(i, j, X=X, S=S):
    """Plot ith dimension of embeddings against feature j."""
    x = X.loc[i]
    s = S.loc[j]
    sns.jointplot(x, s);
plot(122,'People_by_vocation')<jupyter_output><empty_output><jupyter_text>Top K words<jupyter_code>def highest_value(i, k=20, X=X):
    """Return the top `k` words with highest values for ith dimension in X."""
    dim = X.loc[i]
    return dim.nlargest(n=k).index
k = 10
largest = pd.DataFrame([highest_value(i, k) for i in alignments.index], index=alignments.index)
top_k = pd.merge(alignments, largest, left_index=True, right_index=True)
top_k.sort_values(by='max_corr', ascending=False).head(10)
def get_dims(feature, df=top_k):
    """Return the dimensions aligned with `feature` in `df`."""
    return df[df['feature']==feature].sort_values(by='max_corr', ascending=False)

get_dims('Transitive_action').head()
get_dims('Gradable_attributes')<jupyter_output><empty_output><jupyter_text>Universal dependencies<jupyter_code>fname = 'evaluation/dependency/UD_English/en-ud-features.csv'
path = os.path.join(data_path, fname)
ud = pd.read_csv(path)
ud.head(20)<jupyter_output>/home/bacon/miniconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2698: DtypeWarning: Columns (7) have mixed types. Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)
{'fraction_non_alphanumeric': 0.12505705157462346, 'fraction_numerical': 0.009584664536741214, 'mean_word_length': 6.103727714748785, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '649013', 'license': 'MIT', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1474, 'n_words': 306, 'path': 'semrep/evaluate/qvec/features.ipynb', 'repo_name': 'geoffbacon/semrep', 'size': '4382'}