np is not defined after importing numpy - python

Why am I having this error
import numpy as np
np.array([1,2,3,4])
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2304/609253908.py in
----> 1 np.array([1,2,3,4])
NameError: name 'np' is not defined

Your error code says that np.array([1, 2, 3, 4]) is in line 1 so you are trying to use it before importing, you need to import numpy first, as shown in your question.

You can just go with numpy
import numpy
numpy.array([1,2,3,4])
Have you installed or updated numpy?
Try this once in your terminal.
>>> pip install numpy

Related

I get error , cannot import from file helper

Please help, I get the error below running jupyter notebook.
import numpy as np
import pandas as pd
from helper import boston_dataframe
np.set_printoptions(precision=3, suppress=True)
Error:
ImportError Traceback (most recent call last)
<ipython-input-3-a6117bd64450> in <module>
1 import numpy as np
2 import pandas as pd
----> 3 from helper import boston_dataframe
4
5
ImportError: cannot import name 'boston_dataframe' from 'helper' (/Users/irina/opt/anaconda3/lib/python3.8/site-packages/helper/__init__.py)
Since you are not giving the where you get the notebook, I have to guess that you get it from this course Supervised Learning: Regression provided IBM.
In the zip folder in week 1, it provides helper.py.
What you need to do it is to change the directory to where this file is. Change IPython/Jupyter notebook working directory
Alternatively, you can load boston data from sklearn then load it to Pandas Dataframe
Advices for you:
Learn how to use Jupyter notebook
Learn how Python import work
Learn how to provide information in a question so that no one need to guess

why does numpy int64 not work with timedelta64

I was wondering why I get the below error when using numpy.int64 with numpy.timedelta64
ValueError: Could not convert object to NumPy timedelta
For example:
In [10]: np.timedelta64(np.int64(2),'D')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-10-06ccd44f066c> in <module>()
----> 1 np.timedelta64(np.int64(2),'D')
ValueError: Could not convert object to NumPy timedelta
The reason for my question: I am using scipy.stats.mode and pass the result into numpy.timedelta64 in the interim I do the below int as a fix but interested to find out why such behaviour occurs:
from scipy.stats import mode
import nump as np
np.timedelta64(int(mode([2,2,3,4,1,1,5,5])[0][0]),'D')
Out[15]: numpy.timedelta64(1,'D')
Might be a version issue ?
numpy version = 1.10.4
python version = 2.7.11 (64 bit)

'module' object has no attribute 'corrplot'

import seaborn as sns
import matplotlib.pyplot as plt
sns.corrplot(rets,annot=False,diag_names=False)
I get this error after I call the function above...don't know whats going on
AttributeError Traceback (most recent call last)
<ipython-input-32-33914bef0513> in <module>()
----> 1 sns.corrplot(rets,annot=False,diag_names=False)
AttributeError: 'module' object has no attribute 'corrplot'
The corrplot function was deprecated in seaborn version v0.6: https://seaborn.github.io/whatsnew.html#other-additions-and-changes:
The corrplot() and underlying symmatplot() functions have been deprecated in favor of heatmap(), which is much more flexible and robust. These two functions are still available in version 0.6, but they will be removed in a future version.
Note that the function actually still exists in the seaborn codebase, but you have to directly import it from seaborn.linearmodels and you will get a warning that it is subject to removal in a future release.
Import using the command for corrplot and symmatplot
from seaborn.linearmodels import corrplot,symmatplot
corrplot or symmatplot has been deprecated in favor of heatmap .
#Example usage
>>> import numpy as np; np.random.seed(0)
>>> import seaborn as sns; sns.set()
>>> data = np.random.rand(10, 12)
>>> ax = sns.heatmap(data)
Check this for more about heatmap: http://seaborn.pydata.org/generated/seaborn.heatmap.html#seaborn.heatmap

Error when importing modules

I'm getting the following error when importing modules in python. I'm using jupyter notebook (python 2). I've searched through the internet but still can't quite figure out why. Any help would be so much appreciated.
Here's the code:
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-e4e9959b303a> in <module>()
----> 1 import numpy as np
2 from pandas import Series,DataFrame
3 import pandas as pd
/Users/...filepath.../Python/data_analysis/numpy.pyc in <module>()
17
18 import numpy as np
---> 19 from numpy.random import randn
20
21
ImportError: No module named random
I've tried adding import random to the above code (before the other modules) and it still gives the same error. Could this be due to the version of gfortran on my system? I have version 4.9.2
Since I dont have complete code, just tried using import statements.
If we use np as per #John
import numpy as np
from np.random import randn
I am getting
from np.random import randn
ImportError: No module named np.random
I am not getting any error if I import randn from numpy.random
import numpy as np
from numpy.random import randn
print "randn1= ", randn()
from numpy.random import rand
print "rand1= ", rand()
Its working for me with output as below,
randn1= 0.147667079884
rand1= 0.243935746205
You can also try to use np.random.randn() and np.random.rand() directly.
import numpy as np
print "randn2= ", np.random.randn()
print "rand2= ", np.random.rand()
I get :
randn2= -0.22571513741
rand2= 0.486507681046

AttributeError with numpy in IDLE but not in Python Shell

The following code executed from an IDLE window produces an error shown below.
import numpy as np
testarray = np.array([1,2,3], int)
Here is the error...
Traceback (most recent call last):
File "C:\Test\numpy.py", line 1, in <module>
import numpy as np
File "C:\Test\numpy.py", line 2, in <module>
testarray = np.array([1,2,3], int)
AttributeError: 'module' object has no attribute 'array'
>>>
If I do the same thing in the Shell, it works just fine...
>>> import numpy as np
>>> testarray = np.array([1,2,3], int)
>>> testarray
array([1, 2, 3])
>>>
This has been holding me up all day... anyone know how fix it? Perhaps I'm doing something wrong.
Note: If I just execute the code above without the testarray, no error gets returned.
You named a file numpy.py. Python sees that in the module search path and thinks it's the implementation of numpy. Pick a different name.

Categories

Resources