Error when importing modules - python

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

Related

partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)

I am trying to import pandas modules, I imported california housing datasets and I want to get frame info but I get this error:
partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)
what am I missing?
this is my code:
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn import linear_model
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
chousing = fetch_california_housing(as_frame=True)
chousing.frame.info()
try :
import pandas as pd
or simply :
import pandas

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

Pandas/Numpy Errors in Eclipse

I am working in python and eclipse for the first time and i am having troubles. This is the code I have now
from pandas import pandas as pd
from numpy import numpy as np
update = pd.read_csv("PPList.txt")
My error says that the read_csv is an undefined from import
My previous searches indicates that i need to change from numpy import numpy as np to just import numpy *
When I do that, the * has an error saying that it's expecting a comma or colon.
The imports should be
import pandas as pd
import numpy as np
Or
from pandas import *
from numpy import *

How to import single function from packages with the same name

What should I do about this? It seems python is calling the first function.
from numpy.random import multivariate_normal
from scipy.stats import multivariate_normal
The usual convention is:
import numpy as np
np.random.multivariate_normal
Then there won't be such collisions.

abort trap when using matplotlib.pyplot after import caffe in python

My problem is that it occurs an abort trap when using matplotlib.pyplot after import caffe however there is no problem if using matplotlib.pyplot before import caffe.I run this with python2.7 on macos10.12.4.
Here is my wrong code:
#!/usr/bin/python
#encoding=utf-8
import numpy as np
import matplotlib.pyplot as plt
import caffe
plt.plot([0,1],[0,2])
plt.show()
The output is:
src/tcmalloc.cc:284] Attempt to free invalid pointer 0x7feb5f158af0
Abort trap: 6
There will be no problem like this:
#!/usr/bin/python
#encoding=utf-8
import numpy as np
import matplotlib.pyplot as plt
plt.plot([0,1],[0,2])
plt.show()
import caffe
But i need to use plt after 'import caffe'.
How to fix it?

Categories

Resources