When I successfully ran the simulation by using odeint solver. But when I tried to use solve_ivp solver, it gives me an error as "'float' object is not subscriptable". Can someone help me to solve with this issue?. Highly appreciate if you can show the corrected codings to solve the problem as I am bit new to advance coding.
def tank(h,t):
c1=0.13
c2=0.20
Ac=2.0
qin=0.5
h0=h[0]
h1=h[1]
qout1=c1*h0**0.5
qout2=c2*h1**0.5
dhdt1=(qin-qout1)/Ac
dhdt2=(qout1-qout2)/Ac
dhdt=[dhdt1,dhdt2]
return dhdt
t0=0
tfinal=100
tspan=np.arange(0,tfinal+1)
y0=np.array([0,0])
# t=np.linspace(0,10,21)
# y=odeint(tank,y0,t,)
y=solve_ivp(tank,(t0,tfinal),y0,t_eval=tspan)
print(y)
plt.plot(t,y[:,0],'b-')
plt.plot(t,y[:,1],'r--')
plt.show()
I am getting the following error:
AttributeError: 'DataFrame' object has no attribute 'as_matrix'
The code that produces the error:
def plot_fruit_knn(X, y, n_neighbors, weights):
X_mat = X[['height', 'width']].as_matrix()
y_mat = y.as_matrix()
Can anyone help me spot the problem?
as_matrix() is depreceated you can use Dataframe.to_numpy() instead
y_mat = y.to_numpy()
I had the same issue, and I used .to_numpy()
Use
y_mat = y.to_numpy()
instead
I'm trying to compare certain pixel values in my pyautogui script, but it crashes with following error message after either multiple successful runs, or sometimes just straight on the first call:
Traceback (most recent call last):
File "F:\Koodit\Python\HeroWars NNet\Assets\autodataGet.py", line 219, in <module>
battle = observeBattle()
File "F:\Koodit\Python\HeroWars NNet\Assets\autodataGet.py", line 180, in observeBattle
statii = getHeroBattlePixels()
File "F:\Koodit\Python\HeroWars NNet\Assets\autodataGet.py", line 32, in getHeroBattlePixels
colormatch = pyautogui.pixelMatchesColor(location[0], location[1], alive, tolerance=5)
File "E:\Program Files\Python\lib\site-packages\pyscreeze\__init__.py", line 557, in pixelMatchesColor
pix = pixel(x, y)
File "E:\Program Files\Python\lib\site-packages\pyscreeze\__init__.py", line 582, in pixel
return (r, g, b)
File "E:\Program Files\Python\lib\contextlib.py", line 120, in __exit__
next(self.gen)
File "E:\Program Files\Python\lib\site-packages\pyscreeze\__init__.py", line 111, in __win32_openDC
raise WindowsError("windll.user32.ReleaseDC failed : return 0")
OSError: windll.user32.ReleaseDC failed : return 0
My code (this is called multiple times, sometimes it crashes on first run, sometimes it runs nicely for around 100 calls before failing, also, my screen is 4K, so the resolutions get big):
def getSomePixelStatuses():
someLocations= [
[1200, 990],
[1300, 990],
[1400, 990],
[1500, 990],
[1602, 990],
[1768, 990],
[1868, 990],
[1968, 990],
[2068, 990],
[2169, 990]
]
status = []
someValue= (92, 13, 12)
for location in someLocations:
colormatch = pyautogui.pixelMatchesColor(location[0], location[1], someValue, tolerance=5)
status.append(colormatch)
return status
I have no idea how to mitigate this problem. It would seem that pyautogui uses pyscreeze to read pixel values on screen, and most probable candidate for the place where error occurs is the pyscreeze pixel function:
def pixel(x, y):
"""
TODO
"""
if sys.platform == 'win32':
# On Windows, calling GetDC() and GetPixel() is twice as fast as using our screenshot() function.
with __win32_openDC(0) as hdc: # handle will be released automatically
color = windll.gdi32.GetPixel(hdc, x, y)
if color < 0:
raise WindowsError("windll.gdi32.GetPixel failed : return {}".format(color))
# color is in the format 0xbbggrr https://msdn.microsoft.com/en-us/library/windows/desktop/dd183449(v=vs.85).aspx
bbggrr = "{:0>6x}".format(color) # bbggrr => 'bbggrr' (hex)
b, g, r = (int(bbggrr[i:i+2], 16) for i in range(0, 6, 2))
return (r, g, b)
else:
# Need to select only the first three values of the color in
# case the returned pixel has an alpha channel
return RGB(*(screenshot().getpixel((x, y))[:3]))
I installed these libraries just yesterday, and I'm running python 3.8 on windows 10, and pyscreeze is version 0.1.25 so in theory everything should be up to date, but somehow something ends up crashing. Is there a way to mitigate this, either modifying my code, or even the library itself, or is my environment not suitable for this operation?
Well I know it's not particularly helpful; but for me, this error was fixed simply by running my code on 3.7 instead of 3.8. There shouldn't be any changes you have to make to your code, however (unless you were using walrus!)
On Windows, this can be done with the -3.7 command line flag, as long as 3.7 is installed
PyScreeze and PyAutoGUI maintainer here. This is an issue that has been fixed in PyScreeze 0.1.28, so you just need to update it by running pip install -U pyscreeze.
For more context, here's the GitHub issue where it was reported: https://github.com/asweigart/pyscreeze/pull/73
It's a bug. You were on the right track, as the problem is indeed in this line of the pixel() function:
with __win32_openDC(0) as hdc
That function uses cyptes.windll which doesn't seem to do well with the negative values sometimes returned from windll.user32.GetDC(), which subsequently creates an exception when windll.user32.ReleaseDC() is called.
The folks at pillow helped track this down and propose a fix.
issue filed at pyautogui
issue filed at pillow which led to the solution
pending PR at pyscreeze to address
I can use pixel function on Python 3.8 like this:
try:
a = pixel(100,100)
> except:
> a = pixel(100,100)
I don't have any clue why this works, but it works.
I had this error too and i fixed it. Just use try and except.
While true:
try:
x,y = pyautogui.position()
print(pyautogui.pixel(x,y))
except:
print("Cannot get pixel for the moment")
Given that you might be taking pixels multiple times, or you can do so, try and except works wonders to solve any pyscreeze for pyautogui issue. Honestly i dont know whats up with pyscreeze, but this works for me. Cheers
When I try to solve this first ode by using Sympy as it shows below:
import sympy
y = sympy.Function('y')
t = sympy.Symbol('t')
ode = sympy.Eq(y(t).diff(t),(1/y(t))*sympy.sin(t))
sol = sympy.dsolve(ode,y(t))
csol=sol.subs([(t,0),(y(0),-4)]) # the I.C. is y(0) = 1
ode_sol= sol.subs([(csol.rhs,csol.lhs)])
print(sympy.pprint(ode_sol))
It gives me this error:
Traceback (most recent call last):
File "C:/Users/Mohammed Alotaibi/AppData/Local/Programs/Python/Python35/ODE2.py", line 26, in <module>
csol=sol.subs([(t,0),(y(0),-4)]) # the I.C. is y(0) = 1
AttributeError: 'list' object has no attribute 'subs'
Your problem is that this ODE does not have a unique solution. Thus it returns a list of solution, which you can find out from the error message and by printing sol.
Do the evaluation in a loop,
for psol in sol:
csol = psol.subs([(t,0),(y(0),-4)]);
ode_sol = psol.subs([(csol.rhs,csol.lhs)]);
print(sympy.pprint(ode_sol))
to find the next error, that substituting does not solve for the constant. What works is to define C1=sympy.Symbol("C1") and using
ode_sol= psol.subs([(C1, sympy.solve(csol)[0])]);
but this still feels hacky. Or better to avoid error messages for the unsolvability of the second case:
C1=sympy.Symbol("C1");
for psol in sol:
csol = psol.subs([(t,0),(y(0),-4)]);
for cc1 in sympy.solve(csol):
ode_sol= psol.subs([(C1, cc1)]);
print(sympy.pprint(ode_sol))
This has completely baffled me on a number of configurations. I keep reading the documentation, and I just don't get it. Here is my registration code:
ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();
ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
.WithCtorArg("busyDelay").EqualTo(TimeSpan.FromMilliseconds(20))
.WithCtorArg("overallTimeout").EqualTo(TimeSpan.FromSeconds(60));
ForRequestedType<WorkItemQueue>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());
As it is, it says there's no default instance for IWorkItemProcessor (which is correct). Switching the last line to this:
ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());
...Makes a stack overflow exception.
How do you chain classes together that both implement an interface, and take in that same interface in their constructor?
This works, but I can't explain why. From what I know, the first version should work just as well.
ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();
var retryProcessor = ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
.CtorDependency<TimeSpan>("busyDelay").Is(x => x.Object(TimeSpan.FromMilliseconds(20)))
.CtorDependency<TimeSpan>("overallTimeout").Is(x => x.Object(TimeSpan.FromSeconds(60)));
ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
.CtorDependency<IWorkItemProcessor>("workItemProcessor").Is(x => x.Instance(retryProcessor));