Test Failed
Pull Request — main (#5)
by Mohammad
04:56
created

w2v_vis   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A tracking_policy_agendas.word2vec.w2v_vis.reduce_dimensions() 11 11 1
A tracking_policy_agendas.word2vec.w2v_vis.plot_with_matplotlib() 11 11 2
1
from sklearn.manifold import TSNE
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
import matplotlib.pyplot as plt
3
import numpy as np
4
import random
0 ignored issues
show
introduced by
standard import "import random" should be placed before "from sklearn.manifold import TSNE"
Loading history...
5
6
7 View Code Duplication
def reduce_dimensions(w2v_model):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
8
    num_dimensions = 2
9
    vectors = np.asarray(w2v_model.wv.vectors)
10
    labels = np.asarray(w2v_model.wv.index_to_key)
0 ignored issues
show
Comprehensibility Bug introduced by
labels is re-defining a name which is already available in the outer-scope (previously defined on line 41).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
11
12
    tsne = TSNE(n_components=num_dimensions, random_state=42)
13
    vectors = tsne.fit_transform(vectors)
14
15
    x_evals = [v[0] for v in vectors]
0 ignored issues
show
Comprehensibility Bug introduced by
x_evals is re-defining a name which is already available in the outer-scope (previously defined on line 41).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
16
    y_evals = [v[1] for v in vectors]
0 ignored issues
show
Comprehensibility Bug introduced by
y_evals is re-defining a name which is already available in the outer-scope (previously defined on line 41).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
17
    return x_evals, y_evals, labels
18
19
20 View Code Duplication
def plot_with_matplotlib(x_evals, y_evals, labels):
0 ignored issues
show
Comprehensibility Bug introduced by
y_evals is re-defining a name which is already available in the outer-scope (previously defined on line 41).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
introduced by
Missing function or method docstring
Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
Comprehensibility Bug introduced by
labels is re-defining a name which is already available in the outer-scope (previously defined on line 41).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Comprehensibility Bug introduced by
x_evals is re-defining a name which is already available in the outer-scope (previously defined on line 41).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
21
    random.seed(0)
22
23
    plt.figure(figsize=(12, 12))
24
    plt.scatter(x_evals, y_evals)
25
    indices = list(range(len(labels)))
26
    selected_indices = random.sample(indices, 25)
27
    for i in selected_indices:
28
        plt.annotate(labels[i], (x_evals[i], y_evals[i]))
29
    plt.savefig('w2v_plot.png')
30
    plt.show()
31
32
33 View Code Duplication
if __name__ == '__main__':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
34
    from .w2v_corpus import W2VCorpus
35
    import pandas as pd
36
    import gensim.models
37
    corpus = pd.read_excel('test_corpus.xlsx')
38
    print(corpus.head())
39
    corpus = W2VCorpus(list(corpus[0]))
40
    model = gensim.models.Word2Vec(sentences=corpus)
41
    x_evals, y_evals, labels = reduce_dimensions(model)
42
    plot_with_matplotlib(x_evals, y_evals, labels)
43