Value Error with numpy when installed TensorFlow - python

I am running into this error when i import this
from gensim.models import KeyedVectors
the error is
File "c:\Users\frase\eg1.py", line 11, in <module>
from gensim.models import KeyedVectors
File "C:\Users\frase\AppData\Local\Programs\Python\Python39\lib\site-packages\gensim\__init__.py", line 11, in <module>
from gensim import parsing, corpora, matutils, interfaces, models, similarities, utils # noqa:F401
File "C:\Users\frase\AppData\Local\Programs\Python\Python39\lib\site-packages\gensim\corpora\__init__.py", line 6, in <module>
from .indexedcorpus import IndexedCorpus # noqa:F401 must appear before the other classes
File "C:\Users\frase\AppData\Local\Programs\Python\Python39\lib\site-packages\gensim\corpora\indexedcorpus.py", line 14, in <module>
from gensim import interfaces, utils
File "C:\Users\frase\AppData\Local\Programs\Python\Python39\lib\site-packages\gensim\interfaces.py", line 19, in <module>
from gensim import utils, matutils
File "C:\Users\frase\AppData\Local\Programs\Python\Python39\lib\site-packages\gensim\matutils.py", line 1024, in <module>
from gensim._matutils import logsumexp, mean_absolute_difference, dirichlet_expectation
File "gensim/_matutils.pyx", line 1, in init gensim._matutils
#!/usr/bin/env cython
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject
I have used KeyedVectors before to load in a model, but this started to occur when i installed 'TensorFlow'.
I have looked up this issue, and reduced the version of Numpy to 1.19.2 (which is max for Tensorflow), but that did not work. I have also tried to simply uninstall and reinstall and that also has not worked.
Any reason why this is happening?
Example of code used for (NOTE: I can't test it so there might be errors):
model = KeyedVectors.load_word2vec_format(
'\Word2vec\GoogleNews-vectors-negative300.bin.gz', binary=True)
# create the embedding matrix
embedding_matrix = np.zeros((vocab_size, 300)) # 300 is dim size
for word, vector in model.key_to_vectors():
if(word in tokenizer.word_index):
idx = tokenizer.word_index[word]
embedding_matrix[idx] = np.array(vector, dtype=np.float32)

Solutions.
Downgrade python environment to 3.8/3.7
uninstall Numpy and upgrade to Latest NumPy version
Reference- ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject

Related

ValueError: source code string cannot contain null bytes - Error after scipy.io import

After updating scipy, numpy and pandas to the newest versions, I receive the following error whenever I attempt to run my code on a Windows 10 machine with Python 3.7.4:
Traceback (most recent call last):
...
File "Path\To\MyClass.py", line 3, in <module>
import scipy.io as sio
File "Path\To\Anaconda\lib\site-packages\scipy\__init__.py", line 68, in <module>
from ._lib.deprecation import _deprecated
File "Path\To\Anaconda\lib\site-packages\scipy\_lib\__init__.py", line 12, in <module>
from scipy._lib._testutils import PytestTester
ValueError: source code string cannot contain null bytes
This is how the last file looks like:
"""
Module containing private utility functions
===========================================
The ``scipy._lib`` namespace is empty (for now). Tests for all
utilities in submodules of ``_lib`` can be run with::
from scipy import _lib
_lib.test()
"""
from scipy._lib._testutils import PytestTester
test = PytestTester(__name__)
del PytestTester
Am I running into a bug or is my setup broken?
I managed to solve this issue by reinstalling Anaconda. I still don't know the source of the problem though.

MetPy Geocolor Satellite Tutorial Breakage

I am encountering an error in MetPy when following the geocolor satellite imagery tutorial. Specifically, the section entitled "Plot with Cartopy Geostationary Projection". This breakage occurred roughly two weeks ago and functionality has yet to return. Consider the following code:
from xarray import open_dataset
import metpy
data_dir = '.'
color_file = 'OR_ABI-L1b-RadC-M3C01_G16_s20180152002235_e20180152005008_c20180152005054.nc'
c = open_dataset('/'.join([data_dir,color_file]))
dat = c.metpy.parse_cf('Rad')
This block is functionally similar to that provided in the MetPy geocolor satellite tutorial. It worked fine until recently. Now the following error occurs:
Traceback (most recent call last):
File "<stdin>", line 1, in module
File "/usr/local/anaconda3/lib/python3.7/site-packages/metpy/xarray.py", line 191, in parse_cf
from .plots.mapping import CFProjection
File "/usr/local/anaconda3/lib/python3.7/site-packages/metpy/plots/__init__.py", line 13, in module
from .skewt import * # noqa: F403
File "/usr/local/anaconda3/lib/python3.7/site-packages/metpy/plots/skewt.py", line 28, in module
from ..calc import dewpoint, dry_lapse, moist_lapse, vapor_pressure
File "/usr/local/anaconda3/lib/python3.7/site-packages/metpy/calc/__init__.py", line 7, in module
from .cross_sections import * # noqa: F403
File "/usr/local/anaconda3/lib/python3.7/site-packages/metpy/calc/cross_sections.py", line 14, in module
from .tools import first_derivative
File "/usr/local/anaconda3/lib/python3.7/site-packages/metpy/calc/tools.py", line 101, in module
def find_intersections(x, a, b, direction='all'):
File "/usr/local/anaconda3/lib/python3.7/site-packages/pint/registry_helpers.py", line 248, in decorator
% (func.__name__, count_params, len(args))
TypeError: find_intersections takes 4 parameters, but 3 units were passed
What seems to be the problem here? Is there a workaround available?
I think it's an incompatibility between your installed versions of MetPy and Pint. Try making sure you're running the latest versions of those two with:
conda update metpy pint
I should note that MetPy 0.12.0 (currently the latest) is incompatible with xarray 0.15.1. As of this writing, if the above command updates xarray, you'll need to roll it back slightly with:
conda install xarray=0.15.0
We're working on a bugfix release to address this.

NLTK TypeError: '<' not supported between instances of 'str' and 'int'

I have a piece of code that was working fine a while back.
import nltk
import pickle
from nltk.tokenize import word_tokenize
from nltk.classify import ClassifierI
from statistics import mode
I have nltk properly installed, and I wasn't getting this problem at all yesterday. To the best of knowledge, nothing has changed.
Here is the complete stack trace,
Traceback (most recent call last):
File "C:\Users\student\Desktop\asd.py", line 1, in <module>
import nltk
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\nltk\__init__.py", line 128, in <module>
from nltk.chunk import *
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\nltk\chunk\__init__.py", line 157, in <module>
from nltk.chunk.api import ChunkParserI
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\nltk\chunk\api.py", line 13, in <module>
from nltk.parse import ParserI
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\nltk\parse\__init__.py", line 79, in <module>
from nltk.parse.transitionparser import TransitionParser
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\nltk\parse\transitionparser.py", line 21, in <module>
from sklearn.datasets import load_svmlight_file
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\__init__.py", line 57, in <module>
from .base import clone
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\base.py", line 12, in <module>
from .utils.fixes import signature
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\__init__.py", line 11, in <module>
from .validation import (as_float_array,
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\validation.py", line 18, in <module>
from ..utils.fixes import signature
File "C:\Users\student\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\fixes.py", line 406, in <module>
if np_version < (1, 12, 0):
TypeError: '<' not supported between instances of 'str' and 'int'
Looks like you have somehow ended up with numpy version 1.11.2rc1. Scikit's fixes.py converts this to the triple (1, 11, '2rc1'), with the inevitable result.
Since the current version is 1.12.1, I think the solution is obvious.
My best guess is that np_version (the value referenced at the bottom of the stack trace) has become corrupted somehow, and is now a tuple of strings, or some other datatype that is not a tuple of ints, which is what the code is comparing it against in the line if np_version < (1, 12, 0):. I would suggest reinstalling nltk, though that might not be a definite fix. It's possible that a version mismatch has occured in which a newer version uses a tuple of strings to store the version number. If this is so, it might be a good idea to try to install an older version of nltk, though it's possible a reinstall will fix the problem. Hope this helps!

initialization of multiarray raised unreported exception python

I am a new programmer who is picking up python. I recently am trying to learn about importing csv files using numpy.
Here is my code:
import numpy as np
x = np.loadtxt("abcd.py", delimiter = True, unpack = True)
print(x)
The idle returns me with:
>> True
>> Traceback (most recent call last):
>> File "C:/Python34/Scripts/a.py", line 1, in <module>
import numpy as np
>> File "C:\Python34\lib\site-packages\numpy\__init__.py", line 180, in <module>
from . import add_newdocs
>> File "C:\Python34\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
>> File "C:\Python34\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
>> File "C:\Python34\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
>> File "C:\Python34\lib\site-packages\numpy\core\__init__.py", line 14, in <module>
from . import multiarray
>> SystemError: initialization of multiarray raised unreported exception
Why do I get the this system error and how can I remedy it?
I have experienced this problem too. This is cuased by a file named "datetime.py" in the same folder (exactly the same problem confronted by Bruce). Actually "datetime" is an existing python module. However, I do not know why running my own script, e.g. plot.py will invoke my datetime.py file (I have seen the output produced by my datetime.py, and there will be an auto-generated datetime.cpython-36.pyc in the __pycache__ folder).
Although I am not clear about how the error is triggered, after I rename my datetime.py file to other names, I can run the plot.py immediately. Therefore, I suggest you check if there are some files whose name collides with the system modules. (P.S. I use the Visual Studio Code to run python.)
As there is an error at the import line, your installation of numpy is broken in some way. My guess is that you have installed numpy for python2 but are using python3. You should remove numpy and attempt a complete re-install, taking care to pick the correct version.
There are a few oddities in the code:
You are apparently reading a python file, abcd.py, not a csv file. Typically you want to have your data in a csv file.
The delimiter is a string, not a boolean, typically delimiter="," (Documentation)
import numpy as np
x = np.loadtxt("abcd.csv", delimiter = ",", unpack = True)

Loading svmlight style file when there are less features in the train set than in the test set

I have updated my scikit-learn version to the latest, 0.15.2 (more specifically, I have created a new anaconda environment). It seems that, in this version, a new ValueError has been defined in the method load_svmlight_files() in sklearn/datasets/svmlight_format.py (line 238) :
elif n_features < n_f:
raise ValueError("n_features was set to {},"
" but input file contains {} features"
.format(n_features, n_f))
My problem is, I load a model and then, I want to load my test data, so I use the shape of the coef_ attribute of this model when loading my test data (using the "n_features" attribute of the load_svmlight_file() method). But if the model has less features than the test data, then the loading fails. What is the good way to handle this setting ? I'm not sure when this exception has been added but it seems to be absent in the 0.14.1 release. As an additional question, why this exception has been added ?
>>> from sklearn.externals import joblib
>>> from sklearn.datasets import load_svmlight_file
>>> clf = joblib.load('mymodel')
>>> print clf.coef_.shape
(11, 9862)
>>> X,y = load_svmlight_file('test_data', n_features=clf.coef_.shape[1] )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../anaconda/envs/test/lib/python2.7/site-packages/sklearn/datasets/svmlight_format.py", line 113, in load_svmlight_file
zero_based, query_id))
File ".../anaconda/envs/test/lib/python2.7/site-packages/sklearn/datasets/svmlight_format.py", line 248, in load_svmlight_files
.format(n_features, n_f))
ValueError: n_features was set to 9862, but input file contains 34912 features

Categories

Resources