Text visualization python package
Project description
Titulus
Text visualization python package
from titulus import color, print_
test = "Nous sommes le 12/24/2018 aujourd'hui. Mon numéro de tel est le (301)227-1340"
tokens = test.split()
weights = np.random.randint(low=0, high=10, size=len(tokens))
print_(' '.join(color(tokens, weights, n=10)))
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
categories = ['alt.atheism', 'talk.religion.misc']
newsgroups_train = fetch_20newsgroups(subset='train',
categories=categories)
newsgroups_test = fetch_20newsgroups(subset='test',
categories=categories)
X_train, X_test = newsgroups_train.data, newsgroups_test.data
y_train, y_test = newsgroups_train.target, newsgroups_test.target
idx = np.random.randint(len(X_vec_list))
tokens = tokenizer(X_train[idx])
token_idx = [voc.index(t) if t in voc else -1 for t in tokens]
weights = [X_vec_arr[idx, :][i] if i>0 else 0 for i in token_idx]
print_(' '.join(color(tokens, weights, start_hex="#FEFEFE", finish_hex="#00a4e4", n=20)))
text_clf = Pipeline([('vect', vectorizer),
('clf', SGDClassifier(loss='hinge', penalty='l2', tol=0.2,
alpha=1e-3, max_iter=15, random_state=42)),
])
_ = text_clf.fit(X_train, y_train)
X_vec = vectorizer.transform(X_train)
X_vec_arr = X_vec.toarray()
X_vec_list = [list(x) for x in X_vec_arr]
voc = vectorizer.get_feature_names()
idx = np.random.randint(len(X_vec_list))
tokens = tokenizer(X_train[idx])
token_idx = [voc.index(t) if t in voc else -1 for t in tokens]
weights_ = np.multiply(X_vec_arr[idx, :], text_clf.named_steps['clf'].coef_[0, :])
weights = [weights_[i] if i>0 else 0 for i in token_idx]
print_(' '.join(color(tokens, weights, start_hex="#34BF49", finish_hex="#BE0027", middle_hex="#FEFEFE", n=20)))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
titulus-0.0.1.tar.gz
(3.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file titulus-0.0.1.tar.gz.
File metadata
- Download URL: titulus-0.0.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11028e4997d7bd9491ea1bea80e989ceb0c0975b648c2f01297cb7703e074591
|
|
| MD5 |
dbf260f19aa888e93417f8d009a49121
|
|
| BLAKE2b-256 |
1fc363a4ab52a5d93208585df889d9f271355702544abfcc8aae9f1c3606d3fe
|
File details
Details for the file titulus-0.0.1-py3-none-any.whl.
File metadata
- Download URL: titulus-0.0.1-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c180b962ceac733d713d03e331e0e87fe3c43b24beb3fc2ae3e73ad00f0c2963
|
|
| MD5 |
e6b85a6292803fd43295484ea31bd15f
|
|
| BLAKE2b-256 |
f3885a107573e1a02129f53633e775a3084127dc72dc8effe72571c6c971b85c
|