I'm having trouble writing a function that solves for the flow of a vector field in Sympy. I'm very new to Sympy, although (back in my days) I have spent quite some time using Mathematica for symbolic computations.
My attempt :
>>> from sympy import *
>>> def Flow(v):
eps = Symbol(r'\varepsilon',real=True)
Fl = Function('Fl')
diff_eq = Eq(Derivative(Fl(eps),eps) , v(Fl(eps)))
return dsolve(diff_eq,
ics={Fl(0):x},
)
This definition seems to work since for a very simple example I get the right answer:
>>> def v(x): return x
>>> Flow(v)
Eq(Fl(\varepsilon), x*exp(\varepsilon))
However, on a slightly more challenging example, I get the following error message:
>>> a = Symbol('a')
>>> def v(x): return x**a
>>> Flow(v)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/mnt/c/Boulot/Volumes/Covariant_ref_elem.ipynb Cell 4' in <cell line: 3>()
1 a = Symbol('a')
2 def v(x): return x**a
----> 3 Flow(v)
/mnt/c/Boulot/Volumes/Covariant_ref_elem.ipynb Cell 3' in Flow(v)
3 Fl = Function('Fl')
4 diff_eq = Eq(Derivative(Fl(eps),eps) , v(Fl(eps)))
----> 6 return dsolve(diff_eq,
7 ics={Fl(0):x},
8 )
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/solvers/ode/ode.py:640, in dsolve(eq, func, hint, simplify, ics, xi, eta, x0, n, **kwargs)
637 else:
638 # The key 'hint' stores the hint needed to be solved for.
639 hint = hints['hint']
--> 640 return _helper_simplify(eq, hint, hints, simplify, ics=ics)
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/solvers/ode/ode.py:701, in _helper_simplify(eq, hint, match, simplify, ics, **kwargs)
699 for s in rv:
700 try:
--> 701 solved_constants = solve_ics([s], [r['func']], cons(s), ics)
702 except ValueError:
703 continue
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/solvers/ode/ode.py:793, in solve_ics(sols, funcs, constants, ics)
791 # TODO: Use solveset here
792 try:
--> 793 solved_constants = solve(subs_sols, constants, dict=True)
794 except NotImplementedError:
795 solved_constants = []
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/solvers/solvers.py:916, in solve(f, *symbols, **flags)
911 raise NotImplementedError(filldedent('''
912 Unanticipated argument of Eq when other arg
913 is True or False.
914 '''))
915 else:
--> 916 fi = fi.rewrite(Add, evaluate=False)
917 f[i] = fi
919 if fi.is_Relational:
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/core/basic.py:1890, in Basic.rewrite(self, deep, *args, **hints)
1887 return self
1888 # hereafter, empty pattern is interpreted as all pattern.
-> 1890 return self._rewrite(pattern, rule, method, **hints)
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/core/basic.py:1895, in Basic._rewrite(self, pattern, rule, method, **hints)
1893 deep = hints.pop('deep', True)
1894 if deep:
-> 1895 args = [a._rewrite(pattern, rule, method, **hints)
1896 for a in self.args]
1897 else:
1898 args = self.args
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/core/basic.py:1895, in <listcomp>(.0)
1893 deep = hints.pop('deep', True)
1894 if deep:
-> 1895 args = [a._rewrite(pattern, rule, method, **hints)
1896 for a in self.args]
1897 else:
1898 args = self.args
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/core/basic.py:1895, in Basic._rewrite(self, pattern, rule, method, **hints)
1893 deep = hints.pop('deep', True)
1894 if deep:
-> 1895 args = [a._rewrite(pattern, rule, method, **hints)
1896 for a in self.args]
1897 else:
1898 args = self.args
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/core/basic.py:1895, in <listcomp>(.0)
1893 deep = hints.pop('deep', True)
1894 if deep:
-> 1895 args = [a._rewrite(pattern, rule, method, **hints)
1896 for a in self.args]
1897 else:
1898 args = self.args
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/core/basic.py:1909, in Basic._rewrite(self, pattern, rule, method, **hints)
1907 if not args:
1908 return self
-> 1909 return self.func(*args)
File ~/anaconda3/envs/sympy/lib/python3.10/site-packages/sympy/functions/elementary/piecewise.py:35, in ExprCondPair.__new__(cls, expr, cond)
32 cond = cond.rewrite(ITE)
34 if not isinstance(cond, Boolean):
---> 35 raise TypeError(filldedent('''
36 Second argument must be a Boolean,
37 not `%s`''' % func_name(cond)))
38 return Tuple.__new__(cls, expr, cond)
TypeError:
Second argument must be a Boolean, not `Add`
If you were to tell me that the ODE is too hard for sympy, I would be a bit surprised because I solved it by hand in the matter of a few minutes, but I would understand. I however fail to undestand why an error is raised.
Questions: Is my syntax wrong? Is the ODE too complex to be solved by Sympy? Why is an error raised and what does the error mean exactly?
Thank you for your help,
Related
I have a column in form of a data-frame that contains the ratio of some numbers.
On that df col, I want to apply hurst function using df.apply() method.
I don't know if the error is with the df.apply or with the hurst_function.
Consider the code which calculates hurst exponent on a col using the df.apply method:
import hurst
def hurst_function(df_col_slice):
display(df_col_slice)
return hurst.compute_Hc(df_col_slice)
def func(df_col):
results = round(df_col.rolling(101).apply(hurst_function)[100:],1)
return results
func(df_col)
I get the error:
Input In [73], in func(df_col)
---> 32 results = round(df_col.rolling(101).apply(hurst_function)[100:],1)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:1843, in Rolling.apply(self, func, raw, engine, engine_kwargs, args, kwargs)
1822 #doc(
1823 template_header,
1824 create_section_header("Parameters"),
(...)
1841 kwargs: dict[str, Any] | None = None,
1842 ):
-> 1843 return super().apply(
1844 func,
1845 raw=raw,
1846 engine=engine,
1847 engine_kwargs=engine_kwargs,
1848 args=args,
1849 kwargs=kwargs,
1850 )
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:1315, in RollingAndExpandingMixin.apply(self, func, raw, engine, engine_kwargs, args, kwargs)
1312 else:
1313 raise ValueError("engine must be either 'numba' or 'cython'")
-> 1315 return self._apply(
1316 apply_func,
1317 numba_cache_key=numba_cache_key,
1318 numba_args=numba_args,
1319 )
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:590, in BaseWindow._apply(self, func, name, numba_cache_key, numba_args, **kwargs)
587 return result
589 if self.method == "single":
--> 590 return self._apply_blockwise(homogeneous_func, name)
591 else:
592 return self._apply_tablewise(homogeneous_func, name)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:442, in BaseWindow._apply_blockwise(self, homogeneous_func, name)
437 """
438 Apply the given function to the DataFrame broken down into homogeneous
439 sub-frames.
440 """
441 if self._selected_obj.ndim == 1:
--> 442 return self._apply_series(homogeneous_func, name)
444 obj = self._create_data(self._selected_obj)
445 if name == "count":
446 # GH 12541: Special case for count where we support date-like types
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:431, in BaseWindow._apply_series(self, homogeneous_func, name)
428 except (TypeError, NotImplementedError) as err:
429 raise DataError("No numeric types to aggregate") from err
--> 431 result = homogeneous_func(values)
432 return obj._constructor(result, index=obj.index, name=obj.name)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:582, in BaseWindow._apply.<locals>.homogeneous_func(values)
579 return func(x, start, end, min_periods, *numba_args)
581 with np.errstate(all="ignore"):
--> 582 result = calc(values)
584 if numba_cache_key is not None:
585 NUMBA_FUNC_CACHE[numba_cache_key] = func
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:579, in BaseWindow._apply.<locals>.homogeneous_func.<locals>.calc(x)
571 start, end = window_indexer.get_window_bounds(
572 num_values=len(x),
573 min_periods=min_periods,
574 center=self.center,
575 closed=self.closed,
576 )
577 self._check_window_bounds(start, end, len(x))
--> 579 return func(x, start, end, min_periods, *numba_args)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\window\rolling.py:1342, in RollingAndExpandingMixin._generate_cython_apply_func.<locals>.apply_func(values, begin, end, min_periods, raw)
1339 if not raw:
1340 # GH 45912
1341 values = Series(values, index=self._on)
-> 1342 return window_func(values, begin, end, min_periods)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\window\aggregations.pyx:1315, in pandas._libs.window.aggregations.roll_apply()
TypeError: must be real number, not tuple
What can I do to solve this?
Edit: display(df_col_slice) is giving the following output:
0 0.282043
1 0.103355
2 0.537766
3 0.491976
4 0.535050
...
96 0.022696
97 0.438995
98 -0.131486
99 0.248250
100 1.246463
Length: 101, dtype: float64
hurst.compute_Hc function returns a tuple of 3 values:
H, c, vals = compute_Hc(df_col_slice)
where H is the Hurst exponent , and c - is some constant.
But, pandas._libs.window.aggregations.roll_apply() expects its argument (function) to return a single (scalar) which is the reduced result of a rolling window.
That's why your hurst_function function need to return a certain value from vals.
I am fairly new to Python and it took me hours to figure out what I can do with no success. I am trying an example in plotting from https://jupyter.brynmawr.edu/services/public/dblank/jupyter.cs/FLAIRS-2015/TSPv3.ipynb#Python-and-iPython-Notebook:-Preliminaries. In the section on "Plotting Tours", as labeled as cell 15,16,17,18 and maybe more. The function for plot_tour and plot_lines are using map() and I read that in Python 3 it's a generator instead of a list. This made it confusing so is there any way around this to output the plot? which possibly not using map(). Thank you so much in advance.
First plot code:
def plot_tour(tour):
"Plot the cities as circles and the tour as lines between them."
plot_lines(list(tour) + [tour[0]])
def plot_lines(points, style='bo-'):
"Plot lines to connect a series of points."
plt.plot(map(X, points), map(Y, points), style)
plt.axis('scaled'); plt.axis('off')
plot_tour(alltours_tsp(Cities(8)))
This is the error im getting:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [18], in <cell line: 1>()
----> 1 plot_tour(alltours_tsp(Cities(8)))
Input In [17], in plot_tour(tour)
1 def plot_tour(tour):
2 "Plot the cities as circles and the tour as lines between them."
----> 3 plot_lines(list(tour) + [tour[0]])
Input In [17], in plot_lines(points, style)
5 def plot_lines(points, style='bo-'):
6 "Plot lines to connect a series of points."
----> 7 plt.plot(map(X, points), map(Y, points), style)
8 plt.axis('scaled'); plt.axis('off')
File ~\anaconda3\lib\site-packages\matplotlib\pyplot.py:2757, in plot(scalex, scaley, data, *args, **kwargs)
2755 #_copy_docstring_and_deprecators(Axes.plot)
2756 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2757 return gca().plot(
2758 *args, scalex=scalex, scaley=scaley,
2759 **({"data": data} if data is not None else {}), **kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py:1632, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
1390 """
1391 Plot y versus x as lines and/or markers.
1392
(...)
1629 (``'green'``) or hex strings (``'#008000'``).
1630 """
1631 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1632 lines = [*self._get_lines(*args, data=data, **kwargs)]
1633 for line in lines:
1634 self.add_line(line)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:312, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
310 this += args[0],
311 args = args[1:]
--> 312 yield from self._plot_args(this, kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:493, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs)
490 x, y = index_of(xy[-1])
492 if self.axes.xaxis is not None:
--> 493 self.axes.xaxis.update_units(x)
494 if self.axes.yaxis is not None:
495 self.axes.yaxis.update_units(y)
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1443, in Axis.update_units(self, data)
1437 def update_units(self, data):
1438 """
1439 Introspect *data* for units converter and update the
1440 axis.converter instance if necessary. Return *True*
1441 if *data* is registered for unit conversion.
1442 """
-> 1443 converter = munits.registry.get_converter(data)
1444 if converter is None:
1445 return False
File ~\anaconda3\lib\site-packages\matplotlib\units.py:206, in Registry.get_converter(self, x)
202 else:
203 # ... and avoid infinite recursion for pathological iterables for
204 # which indexing returns instances of the same iterable class.
205 if type(first) is not type(x):
--> 206 return self.get_converter(first)
207 return None
File ~\anaconda3\lib\site-packages\matplotlib\units.py:199, in Registry.get_converter(self, x)
197 pass
198 try: # If cache lookup fails, look up based on first element...
--> 199 first = cbook.safe_first_element(x)
200 except (TypeError, StopIteration):
201 pass
File ~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py:1678, in safe_first_element(obj)
1676 except TypeError:
1677 pass
-> 1678 raise RuntimeError("matplotlib does not support generators "
1679 "as input")
1680 return next(iter(obj))
RuntimeError: matplotlib does not support generators as input
Next plot code:
def plot_tsp(algorithm, cities):
"Apply a TSP algorithm to cities, plot the resulting tour, and print information."
# Find the solution and time how long it takes
t0 = time.clock()
tour = algorithm(cities)
t1 = time.clock()
assert valid_tour(tour, cities)
plot_tour(tour); plt.show()
print("{} city tour with length {:.1f} in {:.3f} secs for {}"
.format(len(tour), tour_length(tour), t1 - t0, algorithm.__name__))
def valid_tour(tour, cities):
"Is tour a valid tour for these cities?"
return set(tour) == set(cities) and len(tour) == len(cities)
plot_tsp(alltours_tsp, Cities(8))
This is the error im getting:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Input In [23], in <cell line: 1>()
----> 1 plot_tsp(alltours_tsp, Cities(8))
Input In [22], in plot_tsp(algorithm, cities)
6 t1 = time.perf_counter()
7 assert valid_tour(tour, cities)
----> 8 plot_tour(tour); plt.show()
9 print("{} city tour with length {:.1f} in {:.3f} secs for {}"
10 .format(len(tour), tour_length(tour), t1 - t0, algorithm.__name__))
Input In [17], in plot_tour(tour)
1 def plot_tour(tour):
2 "Plot the cities as circles and the tour as lines between them."
----> 3 plot_lines(list(tour) + [tour[0]])
Input In [17], in plot_lines(points, style)
5 def plot_lines(points, style='bo-'):
6 "Plot lines to connect a series of points."
----> 7 plt.plot(map(X, points), map(Y, points), style)
8 plt.axis('scaled'); plt.axis('off')
File ~\anaconda3\lib\site-packages\matplotlib\pyplot.py:2757, in plot(scalex, scaley, data, *args, **kwargs)
2755 #_copy_docstring_and_deprecators(Axes.plot)
2756 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2757 return gca().plot(
2758 *args, scalex=scalex, scaley=scaley,
2759 **({"data": data} if data is not None else {}), **kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py:1632, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
1390 """
1391 Plot y versus x as lines and/or markers.
1392
(...)
1629 (``'green'``) or hex strings (``'#008000'``).
1630 """
1631 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1632 lines = [*self._get_lines(*args, data=data, **kwargs)]
1633 for line in lines:
1634 self.add_line(line)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:312, in _process_plot_var_args.__call__(self, data, *args, **kwargs)
310 this += args[0],
311 args = args[1:]
--> 312 yield from self._plot_args(this, kwargs)
File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:493, in _process_plot_var_args._plot_args(self, tup, kwargs, return_kwargs)
490 x, y = index_of(xy[-1])
492 if self.axes.xaxis is not None:
--> 493 self.axes.xaxis.update_units(x)
494 if self.axes.yaxis is not None:
495 self.axes.yaxis.update_units(y)
File ~\anaconda3\lib\site-packages\matplotlib\axis.py:1443, in Axis.update_units(self, data)
1437 def update_units(self, data):
1438 """
1439 Introspect *data* for units converter and update the
1440 axis.converter instance if necessary. Return *True*
1441 if *data* is registered for unit conversion.
1442 """
-> 1443 converter = munits.registry.get_converter(data)
1444 if converter is None:
1445 return False
File ~\anaconda3\lib\site-packages\matplotlib\units.py:206, in Registry.get_converter(self, x)
202 else:
203 # ... and avoid infinite recursion for pathological iterables for
204 # which indexing returns instances of the same iterable class.
205 if type(first) is not type(x):
--> 206 return self.get_converter(first)
207 return None
File ~\anaconda3\lib\site-packages\matplotlib\units.py:199, in Registry.get_converter(self, x)
197 pass
198 try: # If cache lookup fails, look up based on first element...
--> 199 first = cbook.safe_first_element(x)
200 except (TypeError, StopIteration):
201 pass
File ~\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py:1678, in safe_first_element(obj)
1676 except TypeError:
1677 pass
-> 1678 raise RuntimeError("matplotlib does not support generators "
1679 "as input")
1680 return next(iter(obj))
RuntimeError: matplotlib does not support generators as input
I suspect this notebook was made for an earlier version of python and packages. That's why you're having so many problems. I recommend tracking down some specifications of what python and package versions were used in the course. However, I did get a lot of the notebook to run with these changes:
def plot_lines(points, style='bo-'):
"Plot lines to connect a series of points."
plt.plot(list(map(X, points)), list(map(Y, points)), style)
plt.axis('scaled'); plt.axis('off')
Later on, time.time() should be replaced with perf_counter().
def plot_tsp(algorithm, cities):
"Apply a TSP algorithm to cities, plot the resulting tour, and print information."
# Find the solution and time how long it takes
t0 = time.perf_counter()
tour = algorithm(cities)
t1 = time.perf_counter()
assert valid_tour(tour, cities)
plot_tour(tour); plt.show()
print("{} city tour with length {:.1f} in {:.3f} secs for {}"
.format(len(tour), tour_length(tour), t1 - t0, algorithm.__name__))
def benchmark(function, inputs):
"Run function on all the inputs; return pair of (average_time_taken, results)."
t0 = time.perf_counter()
results = map(function, inputs)
t1 = time.perf_counter()
average_time = (t1 - t0) / len(inputs)
return (average_time, results)
There are other changes that have to be made further down the notebook, however. I had problems with the benchmark function and the USA big map url request. You should really consider digging the package versions though.
I'm trying to run a fast fourier transform on a pandas dataframe that I have. I am using the Kepler exoplanet dataset, here, and a specific notebook for it, here. I recreate the code in cells 27-30 (Note that the code in cell 29 is executed elsewhere, thus both dataframes have the same shape as the original notebook), which looks as follows:
import scipy
def spectrum_getter(X):
Spectrum = scipy.fft.fft(X, n=X.size)
return np.abs(Spectrum)
x_train_OS_FT = x_train_OS.apply(spectrum_getter, axis=1)
x_test_FT = x_test.apply(spectrum_getter, axis=1)
Both x_train_OS and x_test are pandas.core.frame.DataFrame. Running this produces:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
Input In [245], in <module>
----> 1 x_train_OS_FT = x_train_OS.apply(spectrum_getter, axis=1)
2 x_test_FT = x_test.apply(spectrum_getter, axis=1)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\frame.py:8827, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs)
8816 from pandas.core.apply import frame_apply
8818 op = frame_apply(
8819 self,
8820 func=func,
(...)
8825 kwargs=kwargs,
8826 )
-> 8827 return op.apply().__finalize__(self, method="apply")
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\apply.py:727, in FrameApply.apply(self)
724 elif self.raw:
725 return self.apply_raw()
--> 727 return self.apply_standard()
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\apply.py:851, in FrameApply.apply_standard(self)
850 def apply_standard(self):
--> 851 results, res_index = self.apply_series_generator()
853 # wrap results
854 return self.wrap_results(results, res_index)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\apply.py:867, in FrameApply.apply_series_generator(self)
864 with option_context("mode.chained_assignment", None):
865 for i, v in enumerate(series_gen):
866 # ignore SettingWithCopy here in case the user mutates
--> 867 results[i] = self.f(v)
868 if isinstance(results[i], ABCSeries):
869 # If we have a view on v, we need to make a copy because
870 # series_generator will swap out the underlying data
871 results[i] = results[i].copy(deep=False)
Input In [244], in spectrum_getter(X)
3 def spectrum_getter(X):
----> 4 Spectrum = scipy.fft.fft(X, n=X.size)
5 return np.abs(Spectrum)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\scipy\fft\_backend.py:22, in _ScipyBackend.__ua_function__(method, args, kwargs)
20 if fn is None:
21 return NotImplemented
---> 22 return fn(*args, **kwargs)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\scipy\fft\_pocketfft\basic.py:17, in c2c(forward, x, n, axis, norm, overwrite_x, workers, plan)
14 if plan is not None:
15 raise NotImplementedError('Passing a precomputed plan is not yet '
16 'supported by scipy.fft functions')
---> 17 tmp = _asfarray(x)
18 overwrite_x = overwrite_x or _datacopied(tmp, x)
19 norm = _normalization(norm, forward)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\scipy\fft\_pocketfft\helper.py:97, in _asfarray(x)
95 dtype = x.dtype.newbyteorder('=')
96 # Always align input
---> 97 copy = not x.flags['ALIGNED']
98 return np.array(x, dtype=dtype, copy=copy)
File c:\users\marti\appdata\local\programs\python\python39\lib\site-packages\pandas\core\flags.py:98, in Flags.__getitem__(self, key)
96 def __getitem__(self, key):
97 if key not in self._keys:
---> 98 raise KeyError(key)
100 return getattr(self, key)
KeyError: 'ALIGNED'
I attempted to convert the dataframe to a numpy array, but ran into other issues. What am I doing wrong here?
I ran to the same error so I converted my datatype to dataframe and it solved my problem.
Hey I am pretty new to SymPy. I am trying to use SymPy's built-in plotting to plot some functions. My codes are as follows:
from sympy import Symbol, sin, Function, pi,exp
from sympy.plotting import plot
t = Symbol('t')
source = t**2
plot(source,(t,0,1))
This works perfectly. However, notice that I have also imported functions such as sin and exp above. However, I am finding anything involving these functions, SymPy will not plot. For example,
source1 = exp(t)
source2= sin(t)
plot(source1,(t,0,1))
plot(source2,(t,0,1))
Neither of these will work. The error I received is as follows, using the case with sin as an example:
TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/sympy/plotting/experimental_lambdify.py in __call__(self, args, kwargs)
194 #The result can be sympy.Float. Hence wrap it with complex type.
--> 195 result = complex(self.lambda_func(args))
196 if abs(result.imag) > 1e-7 * abs(result):
TypeError: complex() first argument must be a string or a number, not 'FunctionClass'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-45-85c4f08c25e4> in <module>
1 f0 = 15
2 source = sin(t)
----> 3 plot(exp(t),(t,0,1))
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in plot(*args, **kwargs)
1556 plots = Plot(*series, **kwargs)
1557 if show:
-> 1558 plots.show()
1559 return plots
1560
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in show(self)
189 self._backend.close()
190 self._backend = self.backend(self)
--> 191 self._backend.show()
192
193 def save(self, path):
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in show(self)
1246
1247 def show(self):
-> 1248 self.process_series()
1249 #TODO after fixing https://github.com/ipython/ipython/issues/1255
1250 # you can uncomment the next line and remove the pyplot.show() call
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in process_series(self)
1243 if isinstance(self.parent, PlotGrid):
1244 parent = self.parent.args[i]
-> 1245 self._process_series(series, ax, parent)
1246
1247 def show(self):
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in _process_series(self, series, ax, parent)
1072 # Create the collections
1073 if s.is_2Dline:
-> 1074 collection = self.LineCollection(s.get_segments())
1075 ax.add_collection(collection)
1076 elif s.is_contour:
/usr/local/lib/python3.6/dist-packages/sympy/plotting/plot.py in get_segments(self)
661 list_segments.append([p, q])
662
--> 663 f_start = f(self.start)
664 f_end = f(self.end)
665 sample(np.array([self.start, f_start]),
/usr/local/lib/python3.6/dist-packages/sympy/plotting/experimental_lambdify.py in __call__(self, args, kwargs)
234 ' problematic. We are trying a failback method'
235 ' that may still work. Please report this as a bug.')
--> 236 if abs(result.imag) > 1e-7 * abs(result):
237 return None
238 else:
AttributeError: type object 'exp' has no attribute 'imag'
This might be something trivial, but as a beginner I am having quite a hard time figuring these out. Really appreciate your help!!
I want to use ggplot2 within Jupyter Notebook. However, when I try to make an R magic cell and introduce a variable, I get an error.
Here is the code (one paragraph indicates one cell):
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import rpy2
%matplotlib inline
from rpy2.robjects import pandas2ri
pandas2ri.activate()
%load_ext rpy2.ipython
%%R
library(ggplot2)
data = pd.read_csv('train_titanic.csv')
%%R -i data -w 900 -h 480 -u px
With this last cell, I get the following error (incl traceback):
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py in py2rpy_pandasdataframe(obj)
54 try:
---> 55 od[name] = conversion.py2rpy(values)
56 except Exception as e:
~/anaconda3/envs/catenv/lib/python3.7/functools.py in wrapper(*args, **kw)
839
--> 840 return dispatch(args[0].__class__)(*args, **kw)
841
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py in py2rpy_pandasseries(obj)
125 if type(x) is not homogeneous_type:
--> 126 raise ValueError('Series can only be of one type, or None.')
127 # TODO: Could this be merged with obj.type.name == 'O' case above ?
ValueError: Series can only be of one type, or None.
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/sexp.py in from_object(cls, obj)
367 try:
--> 368 mv = memoryview(obj)
369 res = cls.from_memoryview(mv)
TypeError: memoryview: a bytes-like object is required, not 'Series'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-14-75e210679e4a> in <module>
----> 1 get_ipython().run_cell_magic('R', '-i data -w 900 -h 480 -u px', '\n\n')
~/anaconda3/envs/catenv/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2360 with self.builtin_trap:
2361 args = (magic_arg_s, cell)
-> 2362 result = fn(*args, **kwargs)
2363 return result
2364
</home/morgan/anaconda3/envs/catenv/lib/python3.7/site-packages/decorator.py:decorator-gen-130> in R(self, line, cell, local_ns)
~/anaconda3/envs/catenv/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
185 # but it's overkill for just that one bit of state.
186 def magic_deco(arg):
--> 187 call = lambda f, *a, **k: f(*a, **k)
188
189 if callable(arg):
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/ipython/rmagic.py in R(self, line, cell, local_ns)
721 raise NameError("name '%s' is not defined" % input)
722 with localconverter(converter) as cv:
--> 723 ro.r.assign(input, val)
724
725 tmpd = self.setup_graphics(args)
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
190 kwargs[r_k] = v
191 return (super(SignatureTranslatedFunction, self)
--> 192 .__call__(*args, **kwargs))
193
194
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/robjects/functions.py in __call__(self, *args, **kwargs)
111
112 def __call__(self, *args, **kwargs):
--> 113 new_args = [conversion.py2rpy(a) for a in args]
114 new_kwargs = {}
115 for k, v in kwargs.items():
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/robjects/functions.py in <listcomp>(.0)
111
112 def __call__(self, *args, **kwargs):
--> 113 new_args = [conversion.py2rpy(a) for a in args]
114 new_kwargs = {}
115 for k, v in kwargs.items():
~/anaconda3/envs/catenv/lib/python3.7/functools.py in wrapper(*args, **kw)
838 '1 positional argument')
839
--> 840 return dispatch(args[0].__class__)(*args, **kw)
841
842 funcname = getattr(func, '__name__', 'singledispatch function')
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/robjects/pandas2ri.py in py2rpy_pandasdataframe(obj)
59 'The error is: %s'
60 % (name, str(e)))
---> 61 od[name] = StrVector(values)
62
63 return DataFrame(od)
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/robjects/vectors.py in __init__(self, obj)
382
383 def __init__(self, obj):
--> 384 super().__init__(obj)
385 self._add_rops()
386
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/sexp.py in __init__(self, obj)
286 super().__init__(obj)
287 elif isinstance(obj, collections.abc.Sized):
--> 288 super().__init__(type(self).from_object(obj).__sexp__)
289 else:
290 raise TypeError('The constructor must be called '
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/sexp.py in from_object(cls, obj)
370 except (TypeError, ValueError):
371 try:
--> 372 res = cls.from_iterable(obj)
373 except ValueError:
374 msg = ('The class methods from_memoryview() and '
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/conversion.py in _(*args, **kwargs)
26 def _cdata_res_to_rinterface(function):
27 def _(*args, **kwargs):
---> 28 cdata = function(*args, **kwargs)
29 # TODO: test cdata is of the expected CType
30 return _cdata_to_rinterface(cdata)
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/sexp.py in from_iterable(cls, iterable, populate_func)
317 if populate_func is None:
318 cls._populate_r_vector(iterable,
--> 319 r_vector)
320 else:
321 populate_func(iterable, r_vector)
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/sexp.py in _populate_r_vector(cls, iterable, r_vector)
300 r_vector,
301 cls._R_SET_VECTOR_ELT,
--> 302 cls._CAST_IN)
303
304 #classmethod
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/sexp.py in _populate_r_vector(iterable, r_vector, set_elt, cast_value)
237 def _populate_r_vector(iterable, r_vector, set_elt, cast_value):
238 for i, v in enumerate(iterable):
--> 239 set_elt(r_vector, i, cast_value(v))
240
241
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/sexp.py in _as_charsxp_cdata(x)
430 return x.__sexp__._cdata
431 else:
--> 432 return conversion._str_to_charsxp(x)
433
434
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/conversion.py in _str_to_charsxp(val)
118 s = rlib.R_NaString
119 else:
--> 120 cchar = _str_to_cchar(val)
121 s = rlib.Rf_mkCharCE(cchar, _CE_UTF8)
122 return s
~/anaconda3/envs/catenv/lib/python3.7/site-packages/rpy2/rinterface_lib/conversion.py in _str_to_cchar(s, encoding)
97 def _str_to_cchar(s, encoding: str = 'utf-8'):
98 # TODO: use isStrinb and installTrChar
---> 99 b = s.encode(encoding)
100 return ffi.new('char[]', b)
101
AttributeError: 'float' object has no attribute 'encode'
So I find that it is not possible to even start an R magic cell while importing my pandas dataframe object. However, I have tried creating R vectors inside the cell, and find I can plot these using ggplot2 with no issues.
I am using Python 3.7.6, rpy2 3.1.0, jupyter-notebook 6.0.3and am using Ubuntu 18.04.2 LTS on Windows Subsystem for Linux.
The problem is most likely with one (or more) columns having more than one type - therefore it is impossible to transfer the data into an R vector (which can hold only one data type). The traceback may be overwhelming, but here is the relevant part:
ValueError: Series can only be of one type, or None.
Which column it is? Difficult to say without looking at the dataset that you load, but my general solution is to check the types in the columns:
types = data.applymap(type).apply(set)
types[types.apply(len) > 1]
Anything returned by the snippet above would be a candidate culprit. There are many different ways of dealing with the problem, depending on the exact nature of the data. Workarounds that I frequently use include:
calling data = data.infer_objects() - helps if the pandas did not catch up with a dtype change and still stores the data with (suboptimal) Python objects
filling NaN with an empty string or a string constant if you have missing values in a string column (e.g. str_columns = str_columns.fillna(''))
dates.apply(pd.to_datetime, axis=1) if you have datetime objects but the dtype is object
using df.applymap(lambda x: datetime.combine(x, datetime.min.time()) if not isinstance(x, datetime) else x) if you have a mixture of date and datetime objects
In some vary rare cases pandas stores the data differently than expected by rpy2 (following certain manipulations); then writing the dataframe down to a csv file and reading it from the disk again helps - but this is likely not what you are facing here, as you start from a newly read dataframe.
I just noticed there might be an even simpler reason for the problem. For some reason, pandas2ri requires you to call pandas2ri.activate()after importing it. This solved the problem for me.