AttributeError: 'tuple' object has no attribute 'shape' - python

So I have been writing a code to standardize the elements of a matrix and the function I used is as follows:
def preprocess(Data):
if stdn ==True:
st=np.empty((Data.shape[0],Data.shape[1]))
for i in xrange(0,Data.shape[0]):
st[i,0]=Data[i,0]
for i in xrange(1,Data.shape[1]):
st[:,i]=((Data[:,i]-np.min(Data[:,i]))/(np.ptp(Data[:,i])))
np.random.shuffle(st)
return st
else:
return Data
It works very well outside the class but when used inside of it it gives me this error:
AttributeError: 'tuple' object has no attribute 'shape'
Any idea on how I can fix it??
P.S. This is a KNN classification code

According to the error you posted, Data is of type tuple and there is no attribute shape defined for data. You could try casting Data when you call your preprocess function, e.g.:
preprocess(numpy.array(Data))

Related

DaskLGBMClassifier.fit() error: 'Future' object has no attribute 'get_params'

I'm trying LGBM's Dask API and when I fit DaskLGBMClassifier I get the following error:
'Future' object has no attribute 'get_params'
I tried to deug it working on the original code. The variable model that you can see in the error reference that Colab should be an instance of the class LGBMModel, which seems to have the method get_params().
Why does it say that get_params is an attribute? What is a 'Future' object?
this is the image of the error image of error , this the model=_train, this the _train function, this the LGBMModel adn finally the get param function.

Unable to get length of QList despite docs telling me i can

I'm working on a small plugin for QGIS 3.12+ and regardless of what i try i'm unable to perform the simple task of retrieving the number of items in a QList that's returned by QListWidget.selectedItems()
I'm not well versed in python, but have read through the docs and tried each of the below with all of them failing.
What am i missing?
#Returns items as i can loop though them with: for item in selected:
selected = self.dlg.qListWidgetLayers.selectedItems()
#TypeError: count() takes exactly one argument (0 given)
#Docs claim there's a count that doesn't need the parameter...
intTotal = selected.count()
#AttributeError: 'list' object has no attribute 'size'
intTotal = selected.size()
#AttributeError: 'list' object has no attribute 'length'
intTotal = selected.length()
#AttributeError: 'list' object has no attribute 'len'
intTotal = selected.len() #<- This attempt is incorrect, should actually be len(selected)

AttributeError: 'Str' Object Has No Attribute 'Mean_validation_score' in python

This error occurs in my code: AttributeError: 'str' object has no attribute 'mean_validation_score'. What can I do to resolve it?
GridMean = [result.mean_validation_score for result in
gridA.cv_results_]
print(GridMean)
plt.plot(k_values, GridMean)
plt.xlabel('Value of "K" for KNN')
plt.ylabel('CrossValidated Accuracy')
"mean_validation_score" is depricated now it's "mean_test_score". Use "mean_test_score".
For your confirmation you can check-it-out
gridA.cv_results_.keys()
After running the above comment you can see there is no "mean_validation_score".
I presume you are working with sklearn.model_selection.GridSearchCV, with gridA being an instance of GridSearchCV.
I am not sure when the method mean_validation_score was deprecated for GridSearchCV objects, but for sklearn 0.22 you can get the scores calling them with the key 'mean_test_score' being gridA.cv_results a dictionary. Like this:
GridMean = gridA.cv_results_['mean_test_score']

AttributeError: 'numpy.ndarray' object has no attribute 'nipy_spectral'

I am dealing with error
"AttributeError: 'numpy.ndarray' object has no attribute 'nipy_spectral'"
while running silhouette analysis.
The original code was taken from here. I have added the line that shows the error:
color = cm.nipy_spectral(float(i) / n_clusters)
The problem was that you were assigning cm to some other array. Correct way is not to assign cm to any other array or change the code to
matplotlib.cm.nipy_spectral(float(i) / n_clusters)
Change it at all the places where cm is used.
Here is the snippet of the input data and the result of running print(type(cm)).
Screenshot

'dict' object is not callable error - when I'm not using any 'dict's in networkx

I'm using Python to work with networkx and draw some graphs.
I ran into a problem raising:
TypeError: 'dict' object is not callable
on this line of code:
set_node_color(num, list(Graph.node()))
I searched to find that this error is raised when I'm using a variable name dict.
The problem is, I'm not using any variables with the name dict, nor am I using any dictionary types anywhere in the code.
In case it's necessary, printing the type of Graph gives <class 'networkx.classes.digraph.Digraph'>.
I also tried printing the type for Graph.node() only to receive the same error, telling me 'dict' object is not callable.
So I suspect Graph.node() to be a dict type variable, but using (Graph.node()).items() raises the same TypeError.
Any help or advices would be nice. Thanks.
Maybe Graph.node is a dict object, so Graph.node() is not callable.

Categories