My program prints a variable predictions when executed, which is a 2-D numpy array. I am attempting to write the output to a text file. I ensured the shape is 2D by using predictions.shape and checked the type to be <class numpy.ndarray> by using type(predictions)
The error I am receiving is:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
predictor()
File "/Users/owner/Desktop/algo/predict.py", line 146, in predictor
predictions.savetxt("predictions.txt", "a", delimiter=',', fmt='%d', header='', footer='')
AttributeError: 'numpy.ndarray' object has no attribute 'savetxt'
This is returned after I call the function in the python module.
Im sure Ive imported numpy correctly, am I using the savetxt() function incorrect?
I tried searching on Stack but couldn't find anything, Thank you.
Yes, the savetext method is not a method on the array, but in the numpy package. You can use it by calling the method and passing in the array:
numpy.savetxt("predictions.txt", predictions, delimiter=',', fmt='%d', header='', footer='')
or np.savetext(..) if you did import numpy as np
Related
I am trying to open a netcdf file using rioarray:
import rioxarray
import xarray
import raster
xds = rioxarray.open_rasterio(file, crs='+proj=latlong', masked=True)
but:
type(xds)
list
and xds has none of the attributes or methods of an xarray.
xds_lonlat = xds.rio.reproject("epsg:4326")
AttributeError Traceback (most recent call last)
in
----> 1 xds_lonlat = xds.rio.reproject("epsg:4326")
AttributeError: 'list' object has no attribute 'rio'
clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)
AttributeError Traceback (most recent call last)
in
----> 1 clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)
AttributeError: 'list' object has no attribute 'rio'
Can anyone advise?
I recently encountered this when I was opening a netCDF (with rioxarray) that had multiple variables. Since it returns a list, you would not expect it to have any of the rioxarray attributes or methods.
The documentation for the function is here: https://corteva.github.io/rioxarray/stable/rioxarray.html
One of the return types is List[xarray.Dataset], so I think this behavior is expected.
My guess is that you want one of the entries in the list like xds=xds[0], though it's hard to know without having more information about the file that you are opening.
I have got a simple lgbm dataset:
import lightgbm as lgbm
dataset = lgbm.Dataset(data=X, label=y, feature_name=X.columns.tolist())
Where X is a pandas df, and y a pandas series. I want to access a specific column of X in my custom objective function. But when I try:
data = dataset.get_field('data')
I get this error message:
Traceback (most recent call last):
File "<ipython-input-71-34d27860b9e3>", line 1, in <module>
data = dataset.get_field('data')
File "/Users/***/anaconda3/envs/py3k/lib/python3.6/site-packages/lightgbm/basic.py", line 1007, in get_field
ctypes.byref(out_type)))
File "/Users/***/anaconda3/envs/py3k/lib/python3.6/site-packages/lightgbm/basic.py", line 48, in _safe_call
raise LightGBMError(_LIB.LGBM_GetLastError())
LightGBMError: b'Field not found'
Whereas this works well:
y = dataset.get_field('label')
Thank you!
It doesn't seem to be possible.
The data seems to be the core of a dataset, whereas the rest of lgb.Dataset constructor arguments are handled as additional features. You can see all of them other than the data end up in lgb.Dataset.set_field function as can be tracked in the _lazy_init function. Filed setting in C back-end is handled by SetXXXField functions as handled by the LGBM_DatasetSetField function. You will see that those calls do not appear elsewhere in c_api.cpp
I'm new to Python (and am using Spyder) and am trying to create some histograms of when the top movies from IMDB were created. I have imported the matplotlib, numpy and pandas, as well as the .txt file, but when I run the following lines of code:
plt.hist(data.year, bins=np.arange(1950, 2013), color='#cccccc')
I receive an error message:
Traceback (most recent call last):
File "stdin", line 1, in <module>
AttributeError: 'module' object has no attribute 'hist'
What am I doing wrong?
Your question provide very poor information and insight in your code. More data..
Meanwhile check if you actually import your modules correctly, should have:
import matplotlib.pyplot as plt
in order to use hist function
Can anyone suggest how to use PCA built in function in pyscripter. As a start i imported pca
from matplotlib.mlab import PCA
and then code area,
results = PCA(arr1)
print("-----------------------PCA-----------------------")
print(results)
where arr1 is the dataset(numerical) read from a file.But I am getting Attrubute error as:
Traceback (most recent call last):
File "C:\Users\pooja\Documents\project\traingsom.py", line 282, in <module>
results = PCA(arr1)
File "C:\Python27\lib\site-packages\matplotlib\mlab.py", line 845, in __init__
n, m = a.shape
AttributeError: 'list' object has no attribute 'shape'
Please help..
In the source code of mlab, it says '*a*: a numobservations x numdims array'.
You should use it like this:
import numpy as np
from matplotlib.mlab import PCA
dataMatrix = np.array(aListOfLists) # Convert a list-of-lists into a numpy array. aListOfLists is the data points in a regular list-of-lists type matrix.
myPCA = PCA(dataMatrix) # make a new PCA object from a numpy array object
Hope it helps. :)
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.