'numpy.ndarray' object has no attribute 'append' [duplicate] - python

This question already has answers here:
Concatenate a NumPy array to another NumPy array
(12 answers)
Closed 3 years ago.
I am making a model using word2vec. After training the model i was using cosine similarity. But i am getting the following error.
I am using python 3
The code I used is as follows:
import numpy as np
from sklearn.metrics.pairwise import cosine_distances
cos_dist =[]
cos_dist =[cos_dist]
cos_dist = np.array(cos_dist).reshape(1, -1)
for vec in data[:-1]:
cos_dist.append(float(cosine_distances(vec,data[-1])))
I am getting the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call
last)
<ipython-input-14-ef6e7efe7eaa> in <module>
5 cos_dist = np.array(cos_dist).reshape(1, -1)
6 for vec in data[:-1]:
----> 7 cos_dist.append(float(cosine_distances(vec,data[-1])))
8
9
AttributeError: 'numpy.ndarray' object has no attribute 'append'

You can use np.append which doesn't work inplace:
cos_dist = np.append(cos_dist, [float(cosine_distances(vec,data[-1]))])

You can use numpy.concatenate(list1, list2) or numpy.append().
There's a similar discussion in this thread

Related

How to get a list of all tokens from Lucene 8.6.1 index using PyLucene?

I have got some direction from this question. I first make the index like below.
import lucene
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriterConfig, IndexWriter, DirectoryReader
from org.apache.lucene.store import SimpleFSDirectory
from java.nio.file import Paths
from org.apache.lucene.document import Document, Field, TextField
from org.apache.lucene.util import BytesRefIterator
index_path = "./index"
lucene.initVM()
analyzer = StandardAnalyzer()
config = IndexWriterConfig(analyzer)
if len(os.listdir(index_path))>0:
config.setOpenMode(IndexWriterConfig.OpenMode.APPEND)
store = SimpleFSDirectory(Paths.get(index_path))
writer = IndexWriter(store, config)
doc = Document()
doc.add(Field("docid", "1", TextField.TYPE_STORED))
doc.add(Field("title", "qwe rty", TextField.TYPE_STORED))
doc.add(Field("description", "uio pas", TextField.TYPE_STORED))
writer.addDocument(doc)
writer.close()
store.close()
I then try to get all the terms in the index for one field like below.
store = SimpleFSDirectory(Paths.get(index_path))
reader = DirectoryReader.open(store)
Attempt 1: trying to use the next() as used in this question which seems to be a method of BytesRefIterator implemented by TermsEnum.
for lrc in reader.leaves():
terms = lrc.reader().terms('title')
terms_enum = terms.iterator()
while terms_enum.next():
term = terms_enum.term()
print(term.utf8ToString())
However, I can't seem to be able to access that next() method.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-47-6515079843a0> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while terms_enum.next():
5 term = terms_enum.term()
6 print(term.utf8ToString())
AttributeError: 'TermsEnum' object has no attribute 'next'
Attempt 2: trying to change the while loop as suggested in the comments of this question.
while next(terms_enum):
term = terms_enum.term()
print(term.utf8ToString())
However, it seems TermsEnum is not understood to be an iterator by Python.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-48-d490ad78fb1c> in <module>
2 terms = lrc.reader().terms('title')
3 terms_enum = terms.iterator()
----> 4 while next(terms_enum):
5 term = terms_enum.term()
6 print(term.utf8ToString())
TypeError: 'TermsEnum' object is not an iterator
I am aware that my question can be answered as suggested in this question. Then I guess my question really is, how do I get all the terms in TermsEnum?
I found that the below works from here and from test_FieldEnumeration() in the test_Pylucene.py file which is in pylucene-8.6.1/test3/.
for term in BytesRefIterator.cast_(terms_enum):
print(term.utf8ToString())
Happy to accept an answer that has more explanation than this.

Store complex eigenvectors from linalg.eig into an array in Python

I'm having problems storing the out put of numpy.linalg.eig(). I want to store then into two different array. This is the way I've tried:
vec1 = np.zeros(y.shape[0],dtype=complex)
vec2 = np.zeros(y.shape[0],dtype=complex)
for i in np.arange(y.shape[0]):
val,vec= np.linalg.eig(rho_t[:,:,i])
vec1[i] = vec[0]
vec2[i] = vec[1]
The ERROR message is the following:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-389-791a7e5e4801> in <module>
3 for i in np.arange(y.shape[0]):
4 val,vec= np.linalg.eig(rho_t[:,:,i])
----> 5 vec1[i] = vec[0]
6 vec2[i] = vec[1]
7 #vec2[i] = np.array(sol[1][1])
TypeError: only length-1 arrays can be converted to Python scalars
No idea what is the problem, can somebody help me please
According to documentation: https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.eig.html
The normalized (unit “length”) eigenvectors, such that the column
v[:,i] is the eigenvector corresponding to the eigenvalue w[i].
so perhaps the solution is this:
for i in np.arange(y.shape[0]):
val,vec= np.linalg.eig(rho_t[:,:,i])
vec1[i] = vec[:,0]
vec2[i] = vec[:,1]

How to format these strings in python [duplicate]

This question already has answers here:
use format with tex fraction expression in matplotlib python
(2 answers)
Interference between string formatting and LaTeX functions
(1 answer)
How to display a matrix in the Matplotlib annotations
(1 answer)
Closed 4 years ago.
I have some problem about .format() when I use it in matplotlib plotting: Suppose, I have a matrix A = np.column_stack([[0, 1], [-1, 0]]) and I have to display this matrix in my plot. Also this matrix may be changed as per my need. So I use the following code but it gives error. Can you please tell me how to do this task. Also I faced a similar problem when I try to display a vector like, $2\hat{i}+5 \hat{j}$:
I try the following for the matrix:
A = np.column_stack([[0, 1], [-1, 0]])
matrix = r'$\left( \begin{array}{ll} {a11} & {a12} \\ {a21} & {a22} \end{array} \right)$'.format(a11=A[0][0], a12=A[0][1], a21=A[1][0], a22=A[1][1])
plt.title(matrix)
Output:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-89-12cfe5180092> in <module>()
12
13 A = np.column_stack([[0, 1], [-1, 0]]) #Matrix
---> 14 title = r'$\left( \begin{array}{ll} {a11} & {a12} \\ {a21} & {a22} \end{array} \right)$'.format(a11=A[0][0], a12=A[0][1], a21=A[1][0], a22=A[1][1])
15
16 #Plotting:
KeyError: 'array'
I try the following for the vector:
x = [2,3]
plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1]))
Output:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-92-31281b09369d> in <module>()
32
33
---> 34 plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1]))
35
36 #plt.title(title, y = 1.03)
KeyError: 'i'
I've used your second example to provide an answer for simplicity to explain the issue.
x = [2,3]
plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1]))
This returns a KeyError: 'i' because the array variable specified in the string as {i} has not been set in your .format() statement. It was also fail if you just add i to your .format() statement as j has not been specified either.
See the following for a working example:
x = [2,3]
plt.text(2,3,'${a}\hat{i}+{b}\hat{j}$'.format(a= x[0], b=x[1], i="SOMETHING", j="SOMETHING"))
SUMMARY: Everything inside a {} needs to be set in your .format() statement.

New, unexpected operand error with # operator with ndarray or matrix operands

I have been able to use # to do matrix multiplication before, but for some reason, it is not working anymore. I'm running Python 3.5.4, and in either IPython 6.2.1 or notebook 5.0.0, this unexpected error comes up:
In [1]: from numpy import arange
In [2]: A = arange(5)
In [3]: B = arange(15).reshape((5,3))
In [4]: A # B
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-455d622f3b50> in <module>()
----> 1 A # B
TypeError: unsupported operand type(s) for #: 'numpy.ndarray' and 'numpy.ndarray'
Using the matrix type doesn't help:
In [5]: from numpy import matrix
In [6]: A = matrix(arange(5))
In [7]: B = matrix(arange(15).reshape((5,3)))
In [8]: A # B
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-455d622f3b50> in <module>()
----> 1 A # B
TypeError: unsupported operand type(s) for #: 'matrix' and 'matrix'
I'm at a total loss here. Is the # operator deprecated? Am I missing something obvious? This came up while I was revising a notebook that worked fine about a year ago. Is there some other information that would help diagnose whatever is going on?
As far as I can tell, it turns out to have been an issue with having the right version of numpy, per cxw's comment. I have updated numpy, and everything seems to be working fine now.

basic python query. NameError in __init__ method

NameError: name 'the_shape' is not defined
I get the following error when I try to import my KMeans module containing the class named KMeansClass. The KMeans.py module has the following structure:
import numpy as np
import scipy
class KMeansClass:
#takes in an npArray like object
def __init__(self,dataset,the_shape=5):
self.dataset=dataset
self.mu = np.empty(shape=the_shape)
and in ipython when I try
import KMeans
I get the NameError: name 'the_shape' is not defined
I am really new to python OOP and don't know why this is happening as all I'm doing is passing arguments to init and assigning those arguments to instance variables.
Any help would be appreciated.
Thanks in advance!
Full Traceback:
NameError Traceback (most recent call last)
<ipython-input-2-44169aae5584> in <module>()
----> 1 import kmeans
/Users/path to file/kmeans.py in <module>()
1 import numpy as np
2 import scipy
----> 3 class KMeansClass:
4 #takes in an npArray like object
5 def __init__(self,dataset,the_shape=5):
/Users/path_to_file/kmeans.py in KMeansClass()
5 def __init__(self,dataset,the_shape=5):
6 self.dataset=dataset
----> 7 self.mu = np.empty(shape=the_shape)
8
9
NameError: name 'the_shape' is not defined

Categories