AttributeError: type object 'PartialDependenceDisplay' has no attribute 'from_estimator' - scikit-learn

I'm trying to implement Partial Dependence Plot using the following example:
from sklearn.inspection import PartialDependenceDisplay
from sklearn.datasets import make_friedman1
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
X, y = make_friedman1()
est1 = LinearRegression().fit(X, y)
est2 = RandomForestRegressor().fit(X, y)
disp1 = PartialDependenceDisplay.from_estimator(est1, X,
[1, 2])
disp2 = PartialDependenceDisplay.from_estimator(est2, X, [1, 2],
ax=disp1.axes_)
But I get the following error:
AttributeError Traceback (most recent call last)
<ipython-input-7-e98f23dac323> in <module>
6 est1 = LinearRegression().fit(X, y)
7 est2 = RandomForestRegressor().fit(X, y)
----> 8 disp1 = PartialDependenceDisplay.from_estimator(est1, X,
9 [1, 2])
10 disp2 = PartialDependenceDisplay.from_estimator(est2, X, [1, 2],
AttributeError: type object 'PartialDependenceDisplay' has no attribute 'from_estimator'
Is there any solution for this?
Thanks

I had same issue (Windows 10, Python 3.9.7 on Anaconda).
Issue resolved with upgrade sklearn package
!pip install --upgrade scikit-learn

Related

Numpy in VS Code Ubuntu 20.04

I am new to Ubuntu and is trying to get Numpy working on VS Code. Following is my code:
import matplotlib.pyplot as plt
import numpy as np
x = [1,2,3,4]
y = [3,5,7,9]
plt.grid(True)
plt.xlabel("My X values")
plt.ylabel("My Y values")
plt.plot(x,y, 'b-^', lineWidth = 3, markersize = 7, label = "Blue Line")
plt.legend(loc = "upper center")
plt.show()
But the debugger gives me the following error:
No module named 'numpy.core._multiarray_umath'
Does anyone knows how to install that module? Thank you so much in advance.

How to use polygonPointTest function for a polygon?

I am trying to run polygonpoint test function for a polygon. My code looks something like below, does it only meant for coutours ?
import ast
import cv2
import numpy as np
point1 = (25, 50)
t = "201.94,191.31;158.20,343.59;520.55,361.55;469.79,175.70" # closed polygon
pts = [*map(ast.literal_eval, t.split(';'))]
pts = np.array(pts)
cv2.pointPolygonTest(pts, point1, False)
I am getting below error
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-454-bfdc7e835e9b> in <module>
----> 1 cv2.pointPolygonTest(pts, point1, False)
error: OpenCV(4.3.0) /io/opencv/modules/imgproc/src/geometry.cpp:103: error: (-215:Assertion failed) total >= 0 && (depth == CV_32S || depth == CV_32F) in function 'pointPolygonTest'
I am using np array of incompatible type, after converting it into np.int32, I am able to get desired output.
import ast
import cv2
import numpy as np
point1 = (25, 40)
t = "201.94,191.31;158.20,343.59;520.55,361.55;469.79,175.70" # closed polygon
pts = [*map(ast.literal_eval, t.split(';'))]
pts = np.array(pts, np.int32)
cv2.pointPolygonTest(pts, point1, False)

module 'tensorflow' has no attribute 'variable_scope' with TFANN in Colab

i start using Colab to create ANN and try out the TFANN package and receive error with the following simple sample code from https://github.com/nicholastoddsmith/pythonml#tfann:
pip install TFANN
import numpy as np
from TFANN import ANNR
A = np.random.rand(32, 4)
Y = np.random.rand(32, 1)
a = ANNR([4], [('F', 4), ('AF', 'tanh'), ('F', 1)], maxIter = 16, name = 'mlpr1')
a.fit(A, Y)
S = a.score(A, Y)
YH = a.predict(A)
The error i received is: AttributeError: module 'tensorflow' has no attribute 'variable_scope'
Can someone tell me why i received this error and how can i fix this?
Many thanks!

Python Memory 'Blank' Jupyter Notebook

Firstly I'm running on Ubuntu and using Anaconda for my kernels in Jupyter Notebook.
I keep getting the following error. I believe it suggests I'm running out of memory but I'm not sure how to solve this. Someone suggested uninstalling Python3 32 and installing Python3 64 but this broke my Ubuntu installation immediately after uninstalling python (whoops) and I had to reinstall.
---------------------------------------------------------------------------
MemoryError Traceback (most recent call last)
<ipython-input-25-8b765500013f> in <module>()
----> 1 _, S, _ = np.linalg.svd(features)
2 print("Condition number from the SVD: {0:.1f}".format(np.max(S)/np.min(S)))
3 print("Condition number from cond: {0:.1f}".format(np.linalg.cond(features)))
/home/n/anaconda/lib/python3.5/site-packages/numpy/linalg/linalg.py in svd(a, full_matrices, compute_uv)
1387
1388 signature = 'D->DdD' if isComplexType(t) else 'd->ddd'
-> 1389 u, s, vt = gufunc(a, signature=signature, extobj=extobj)
1390 u = u.astype(result_t, copy=False)
1391 s = s.astype(_realType(result_t), copy=False)
MemoryError:
I'm trying to process a dataset with a shape (91946, 171), all numerical, using the code below.
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
pd.set_option('display.max_columns',180)
dfk = pd.read_csv('data/kick.csv')dfk = pd.read_csv('data/kick.csv')
response = dfk.state
features = dfk
features.drop('state', axis=1, inplace=True)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features, response,
random_state=321)
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
logreg = LogisticRegression()
logreg.fit(X_train,y_train)
ypred_lr = logreg.predict(X_test)
print("Accuracy on the test set: {0:.3f}".format(accuracy_score(ypred_lr, y_test)))
_, S, _ = np.linalg.svd(features) #fails here
print("Condition number from the SVD: {0:.1f}".format(np.max(S)/np.min(S)))
print("Condition number from cond: {0:.1f}".format(np.linalg.cond(features)))

Creating PDF using pydot

I got the below code from Visualizing a Decision Tree - Machine Learning
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
test_idx = [0, 50 , 100]
train_target = np.delete(iris.target, test_idx)
train_data = np.delete(iris.data, test_idx , axis=0)
test_target = iris.target[test_idx]
test_data = iris.data[test_idx]
clf = tree.DecisionTreeClassifier()
clf.fit(train_data, train_target)
print(test_target)
print(clf.predict(test_data))
#viz_code
from sklearn.externals.six import StringIO
import pydot
dot_data = StringIO()
tree.export_graphviz(clf,
out_file=dot_data,
feature_names = iris.feature_names,
class_names = iris.target_names,
filled = True, rounded = True,
impurity = False)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph.write_pdf("iris.pdf")
I tried to run it in my python 3.5 but i get an error saying that graph is a list.
Traceback (most recent call last):
File "Iris.py", line 31, in <module>
graph.write_pdf("iris.pdf")
AttributeError: 'list' object has no attribute 'write_pdf'
Press any key to continue . . .
How come graph here is a list?
I think this is a duplicate, here is answered the same question link
because pydot.graph_from_dot_data return a list the solution is:
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf")
This solved the problem for me with Python 3.6.5 :: Anaconda, Inc.
Pydot will not work in Python3.
You can use Pydotplus (graph.write_pdf("iris.pdf") AttributeError: 'list' object has no attribute 'write_pdf'") for python3 instead of pydot.
Although, the code shown on youtube is for Python2. So, it will be better if you use Python2.

Resources