KDE – Is there something wrong in scipy or numpy? Or is it something I am doing?

Question:

I am simply trying to follow an example: https://medium.com/swlh/how-to-analyze-volume-profiles-with-python-3166bb10ff24

I am only on the second step and I am getting errors. Here is my code:

# Load data
df = botc.ib.data_saver.get_df(SYMBOL.lower())

# Separate for vol prof
volume = np.asarray(df['Volume'])
close = np.asarray(df['Close'])

print("Close:")
print(close)
print("VOLUME:")
print(volume)

# Plot volume profile based on close
px.histogram(df, x="Volume", y="Close", nbins=150, orientation='h').show()

# Kernel Density Estimator
kde_factor = 0.05
num_samples = 500
kde = stats.gaussian_kde(close, weights=volume, bw_method=kde_factor)
xr = np.linspace(close.min(), close.max(), num_samples)
kdy = kde(xr)
ticks_per_sample = (xr.max() - xr.min()) / num_samples

def get_dist_plot(c, v, kx, ky):
    fig = go.Figure()
    fig.add_trace(go.Histogram(name="Vol Profile", x=c, y=v, nbinsx=150,
                               histfunc='sum', histnorm='probability density'))
    fig.add_trace(go.Scatter(name="KDE", x=kx, y=ky, mode='lines'))
    return fig

get_dist_plot(close, volume, xr, kdy).show()

And here are the errors:

Traceback (most recent call last):
  File "C:/Users/Jagel/PycharmProjects/VolumeBotv1-1-1/main.py", line 80, in <module>
    start_bot()
  File "C:/Users/Jagel/PycharmProjects/VolumeBotv1-1-1/main.py", line 64, in start_bot
    kde = stats.gaussian_kde(close, weights=volume, bw_method=kde_factor)
  File "M:PROGRAMSAnacondaaenvsMLStockBot2libsite-packagesscipystats_kde.py", line 207, in __init__
    self.set_bandwidth(bw_method=bw_method)
  File "M:PROGRAMSAnacondaaenvsMLStockBot2libsite-packagesscipystats_kde.py", line 555, in set_bandwidth
    self._compute_covariance()
  File "M:PROGRAMSAnacondaaenvsMLStockBot2libsite-packagesscipystats_kde.py", line 564, in _compute_covariance
    self._data_covariance = atleast_2d(cov(self.dataset, rowvar=1,
  File "<__array_function__ internals>", line 180, in cov
  File "M:PROGRAMSAnacondaaenvsMLStockBot2libsite-packagesnumpylibfunction_base.py", line 2680, in cov
    avg, w_sum = average(X, axis=1, weights=w, returned=True)
  File "<__array_function__ internals>", line 180, in average
  File "M:PROGRAMSAnacondaaenvsMLStockBot2libsite-packagesnumpylibfunction_base.py", line 550, in average
    avg = np.multiply(a, wgt,
TypeError: can't multiply sequence by non-int of type 'float'

I have looked all over the internet for over an hour and haven’t been able to solve this. Sorry if it is simple, but I’m starting to get quite angry, so any help is very much appreciated.
Other things I have tried: using different bw_methods, convert to numpy array first.

Asked By: Jacob

||

Answers:

I don’t know about your data, but in your bug, I can reproduce the error as follows:

>>> [5] * 0.1

TypeError                                 Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_18536/2403475853.py in <module>
----> 1 [5] * 0.1

TypeError: can't multiply sequence by non-int of type 'float'

So, you can check about your data, I think in a certain row of the column there is array data

Answered By: Toàn Nguyễn Văn
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.