I want to graph taylor series in python use matplot
import matplotlib.pyplot as plt
import sympy as sy
import numpy as np
x=sy.Symbol('x')
plt.title("Comparison of EXP(x), PP1, PP2")
plt.xlim(00.1, 3+0.2)
plt.ylim(0, 20+1)
plt.xlabel("x")
plt.ylabel("y: values")
plt.grid(False)
x=np.linspace(0.0, 3.0, 100)
plt.plot(x, np.exp(x), label="exp(x)")
plt.plot(x, np.exp(x).series(x, 0, 4), label="PP1")
plt.legend(loc=2)
plt.show();
I use Jupyter Notebook. I want to graph taylor series of exp(x). I just success to graph exp(x) but failed taylor seires of exp(x). Sympy library has taylor series fuction so I tried it but I don't understand what's the problem. below is the error message and graph.
1 answer

sy.exp(x).series(x)
creates a sympy expression, not a functionyou might want to convert it to a function
from sympy.utilities.lambdify import lambdify x = sy.Symbol('x') exp_expr = sy.exp(x).series(x).removeO() exp_func = lambdify(x, exp_expr)
and plot it out
x_points = np.linspace(0.0, 3.0, 100) plt.plot(x_points, [exp_func(i) for i in x_points])
See also questions close to this topic

Selenium Python Unable to scroll down, while fetching google reviews
I am trying to fetch google reviews with the help of selenium in python. I have imported webdriver from selenium python module. Then I have initialized self.driver as follows:
self.driver = webdriver.Chrome(executable_path="./chromedriver.exe",chrome_options=webdriver.ChromeOptions())
After this I am using the following code to type the company name on google homepage whose reviews I need, for now I am trying to fetch reviews for "STANLEY BRIDGE CYCLES AND SPORTS LIMITED ":
company_name = self.driver.find_element_by_name("q") company_name.send_keys("STANLEY BRIDGE CYCLES AND SPORTS LIMITED ") time.sleep(2)
After this to click on the google search button, using the following code:
self.driver.find_element_by_name("btnK").click() time.sleep(2)
Then finally I am on the page where I can see results. Now I want to click on the View on google reviews button. For that using the following code:
self.driver.find_elements_by_link_text("View all Google reviews")[0].click() time.sleep(2)
Now I am able to get reviews, but only 10. I need at least 20 reviews for a company. For that I am trying to scroll the page down using the following code:
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(5)
Even while using the above code to scroll the down the page, I am still getting only 10 reviews. I am not getting any error though.
Need help on how to scroll down the page to get atleast 20 reviews. As of now I am able to get only 10 reviews. Based on my online search for this issue, people have mostly used: "driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")" to scroll the page down whenever required. But for me this is not working. I checked the the height of the page before and after ("driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")") is the same.

Create a new column from itereated rows of timedate data
I am attempting to create a downward velocity model for offshore drilling which uses the variables Depth (which increases every 1 foot) and DateTime data which is more intermittent and is only updated every foot of depth:
Dept DateTime 1141 5/24/2017 04:31 1142 5/24/2017 04:32 1143 5/24/2017 04:40 1144 5/24/2017 04:42 1145 5/25/2017 04:58
I am trying to get something like this:
Where Velocity iterated down dept/(DateTime gap)

one to one mapping in shell script
I am in process of migration. Migrating from old set of servers to new set of servers, where there is no logical relationship in the server names between the 2 sets. I have a script that runs on old server, takes all necessary backups and the run another script to copy the backups to new server and execute it.
I can combine both scripts(taking backup and copying to new server), if I can include a logic to map the old server to new server. Is there a way I can do this.
Old server New server King Queen Bat Ball water fire sand rock
What I am expecting is, if the script is run on server 'King', I want the script to identify that the corresponding new server is 'Queen' and copy the backups to Queen.

Sorting in R and Numpy
I am trying to convert some R code into numpy. I have a vector as follows:
r=[2.00000 1.64000 1.36000 1.16000 1.04000 1.00000 1.64000 1.28000 1.00000 0.80000 0.68000 0.64000 1.36000 1.00000 0.72000 0.52000 0.40000 0.36000 1.16000 0.80000 0.52000 0.32000 0.20000 0.16000 1.04000 0.68000 0.40000 0.20000 0.08000 0.04000 1.00000 0.64000 0.36000 0.16000 0.04000 0.00000]
I am trying to convert following R code
index < order(r)
into numpy by following code
index = np.argsort(r)
Here are the results
Numpy
index=array([35, 29, 34, 28, 33, 23, 27, 22, 21, 32, 17, 16, 26, 15, 20, 11, 31,25, 10, 14, 9, 19, 30, 5, 8, 13, 4, 24, 18, 3, 7, 12, 2, 6, 1, 0])
R
index= [36 30 35 29 24 34 23 28 22 18 33 17 27 16 21 12 32 11 26 15 10 20 6 9 14 31 5 25 4 19 8 3 13 2 7 1]
As you see the results are different. How can I obtain results of R in numpy

Return value of highest index in numpy 3D array
I have a 3D array in numpy that includes nans. I need to return the value with the greatest index position along the 0 axis. The answer would reduce to a 2D array.
There are a lot of questions about finding the index position of a maximum value along an axis (How to get the index of a maximum element in a numpy array along one axis), but that is different than what I need.
Example 3D array:
>>> import numpy as np >>> foo = np.asarray([[[7,4,6],[4,2,11], [7,8,9], [4,8,2]],[[1,2,3],[np.nan,5,8], [np.nan,np.nan,10], [np.nan,np.nan,7]]]) >>> foo array([[[ 7., 4., 6.], [ 4., 2., 11.], [ 7., 8., 9.], [ 4., 8., 2.]], [[ 1., 2., 3.], [ nan, 5., 8.], [ nan, nan, 10.], [ nan, nan, 7.]]])
I thought I was getting close using np.where but it returns all elements that are not nan. Not quite what I need because I want a
(4,3)
array.>>> zoo = foo[np.where(~np.isnan(foo))] >>> zoo array([ 7., 4., 6., 4., 2., 11., 7., 8., 9., 4., 8., 2., 1., 2., 3., 5., 8., 10., 7.])
The answer I need is:
>>> ans = np.asarray([[1,2,3], [4,5,8], [7,8,10], [4,8,7]]) >>> ans array([[ 1, 2, 3], [ 4, 5, 8], [ 7, 8, 10], [ 4, 8, 7]])
EDIT: I edited the foo example array to make the question more clear.

How to know which axis to do operation along an axis in numpy?
I've been working with
numpy
andpandas
for a long time but I'm still usually confused by the concept of doing an operation along an axis.For example, if I have a data of shape [200,5], and I want to find the mean with the resulting shape [1,5], I would first call
data.mean(axis=0)
, and if it doesn't work, I would trydata.mean(axis=1)
.Turns out,
axis=0
is correct in this case. But I don't have good terminology for me to remember which axis to use.Currently, I think that whatever axis I want to reduce the shape to 1, I will need to apply the operation on that axis. This works fine for Reduction operation like
mean
,sum
orstd
.But I don't know how to think when I would like to apply operations that do not reduce the shape like
divide
,add
,sort
, etc. (Fordivide
andadd
of different shapes, broadcasting is involved)So it made me curious about how the guy who created
pandas
andnumpy
think intuitively about this. It made curious about what they exactly mean when they say "sorting along the row axis".I want to understand it so clear that I know what results I'm going to expect when I call a certain axis!

How to plot columns of two separate DataFrames in one plot ( subplots representing each column)
I have two DataFrames like below. They are exactly the same in terms of column names and structure. The difference is one dataframe is prediction and one is observed data. I need to plot a figure with subplots where each subplot title is the column names, Xaxis is the index values and Yaxis is the values in the table. I need two lines drawn in each graph, 1 from the dataframe with prediction, one with observed. Below is what I have been trying to do but its not working.
08FB006 08FC001 08FC005 08GD004 08GD005  0 1.005910075 0.988765247 0.00500000 0.984376392 5.099999889 1 1.052696367 1.075232414 0.00535313 1.076066586 5.292135227 2 1.101749034 1.169026145 0.005731682 1.176162168 5.491832766 3 1.153183046 1.270744221 0.006137526 1.285419625 5.699405829 4 1.207119522 1.381030962 0.006572672 1.404662066 5.915181534 5 1.263686077 1.500580632 0.007039282 1.534784937 6.139501445 6 1.323017192 1.630141078 0.007539681 1.676762214 6.372722261 7 1.38525461 1.770517606 0.008076372 1.831653101 6.615216537 8 1.450547748 1.922577115 0.008652045 2.000609283 6.867373442 9 1.519054146 2.087252499 0.009269598 2.184882781 7.129599561 10 1.590939931 2.265547339 0.009932148 2.385834436 7.402319731
The sample code, I have 81 stations/columns
import matplotlib.pyplot as plt %matplotlib inline fig, axes = plt.subplots(nrows=81, ncols=1) newdf1.plot(ax=axes[0,0]) newdf1_pred.plot(ax=axes[0,1])

"ValueError: RGBA values should be within 01 range" after upgrading matplotlib
I have upgraded to matplotlib 3.0.2 and the script below I used to use for my 3d plots throws this error now:
ValueError: RGBA values should be within 01 range
. Tested with the 2.0.2 version and it works there... Tried to google for similar issues but couldn't find a workaround so asking this smart community for help...test = pd.DataFrame({'cluster': ["0", "1", "2"], 'x': [2, 3, 1], 'y': [10, 5, 2], 'z': [10, 5, 2]}) fig = plt.figure(figsize=(7,7)) ax = Axes3D(fig) x=test['x'] y=test['y'] z=test['z'] clusters = test['cluster'] ax.scatter(x, y, z, c=clusters, marker='x', cmap='tab20b', depthshade=False) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show()

Produce pie chart subplots for unique results in groupby?
What is the best way to produce pie charts for unique values in a Dataframe?
I have a DataFrame that shows number of services by county. I would like to produce a group of pie charts for every county that shows the count of services in that county. I've tried a variety of different approaches without much success.
Here's my data
print(mdfgroup)
County Service ServiceCt 0 Alamance Literacy 1 1 Alamance Technical 1 2 Alamance Vocational 4 3 Chatham Literacy 3 4 Chatham Technical 2 5 Chatham Vocational 1 6 Durham Literacy 1 7 Durham Technical 1 8 Durham Vocational 1 9 Orange Literacy 1 10 Wake Literacy 2 11 Wake Technical 2
So there would be one chart for Alamance with slices for literacy, technical, vocational; a chart for Chatham, Durham, etc. Slice size would be based on ServiceCt.
I've been experimenting with a lot of different approaches but I'm not sure what the most efficient would be. I tried but below it doesn't really break it down by county and it's not resulting in any graphs.
for i, row in enumerate(mdfgroup.itertuples(),1): plt.figure() plt.pie(row.ServiceCt,labels=row.Service, startangle=90,frame=True, explode=0.2,radius=3) plt.show()
This throws an error:
TypeError: len() of unsized object
and then produces a blank plot box
(I can't embed an image yet so here's the link) Blank Plot Box
Ideally I'd like them to all be subplots, but at this stage I'd take a series of individual plots. The other examples I've found don't deal with unique values for a key (County).

sympy calculate nth recurrence without using rsolve
I'm working on writing an application which has to solve some recurrence relations but there are some relation which can't be analytically solved by using the
rsolve
method ofsympy
. It just returnsNone
. Is there a way I can force sympy to solve them numerically?I have something like this:
from sympy import * ctx = { "f": Function("f"), "x": var("x",integer=True) } initial_conditions = { 0: 1, 1: 1, 2: 1, 3: 1 } f = sympify("2*f(x1)+11*f(x2)+12*f(x3)36*f(x4) +41**(x4)+3 f(x)", ctx) # calculate f(10) here without creating a closed from # The code below will not work rsolve returns None solve_for = sympify("f(x)", ctx) solved = rsolve(f, solve_for, initial_conditions)
I hope someone can help me!

plotting sympy function using numpy and matplotlib
Here's what i'm trying to do:
# the standard broiler plate for jupyter %matplotlib inline from matplotlib.pyplot import * from sympy import * import numpy as np init_printing() t = symbols('t') x1 = 2*cos(3*pi*t + pi/4) x2 = x1.diff(t) display(x1) display(x2) pi2 = 2*np.pi vt = np.arange(0, pi2, 0.1) vx1 = np.zeros(len(tv)) vx2 = np.zeros(len(tv)) for n in range(0,len(vt)): vx1[n] = N(x1.subs(t,vt[n])) vx2[n] = N(x2.subs(t,vt[n])) title('Signal') plot(vt, vx1) show() title('Derivative of signal') plot(vt, vx2) show()
The above seems like it should work... however, I'm getting unexpected error message:
 TypeError Traceback (most recent call last) <ipythoninput41eaccf9c2bc6b> in <module>() 14 15 title('Signal') > 16 plot(vt, vx1) 17 show() 18 title('Instantanious Frequency') C:\Python35\lib\sitepackages\sympy\plotting\plot.py in plot(*args, **kwargs) 1289 series = [] 1290 plot_expr = check_arguments(args, 1, 1) > 1291 series = [LineOver1DRangeSeries(*arg, **kwargs) for arg in plot_expr] 1292 1293 plots = Plot(*series, **kwargs) TypeError: 'NoneType' object is not iterable

Solving symbolic linear inequalities
In sympy, I can solve systems of multivariatae linear equalities, for example:
print(linsolve([x+y  3*a, xy  a], x,y))
shows that the solution is x = 2*a, y = a.
Is there a way to solve the analogous inequality, i.e., something like:
linsolve_inequality([x+y  3*a, xy  a], x,y)
should show that x > 2*a?

How to get inputs from GUI and output answer in GUI?
I have created a Exponential Taylor Series Calculator in Java but I don't know how to use GUI in order to get the user input as well as output the answer in GUI. I am a beginner level programmer and I really need help on this.
import java.util.Scanner; public class Exponential { public static void main(String[] args) { Scanner input = new Scanner(System.in); double x; int n; System.out.println("Enter value of x:"); x=input.nextDouble(); System.out.println("Enter value of N:"); n=input.nextInt(); double Exponential= calc(x,n); System.out.print("The sum for value of "+x); System.out.print(" between 0 and "+n); System.out.println(" is: "+Exponential); if (x>0) System.out.println("The sum is increasing"); if (x<0) System.out.println("The sum is decreasing"); if (x==0) System.out.println("The sum is nor increasing/decreasing"); } public static long factorial(long number) { if (number==0) return 1; if (number==1) { return 1; } return number*factorial(number1); } public static double calc(double x, int n){ if (n==0) return 1; else { return calc (x,n1)+Math.pow(x,n)/factorial(n); } } }

Problem with calculating the Taylor series
How to expend the function y=lnax in the powers of (x2)? Desperately needs solution for this problem

Puiseux Series Expansion
How does one compute a Puiseux series? For example the series
\sqrt{t(t1)} = \sqrt{t}\frac{t^{\frac{3}{2}}}{2}\frac{t^{\frac{5}{2}}}{8}
about t=0.
And the series
\sqrt{tan{t}} = \sqrt{t} + \frac{x^{\frac{5}{2}}}{6}+\frac{19 x^{\frac{9}{2}}}{360}
about t = 0.