arundo / adtk

A Python toolkit for rule-based/unsupervised anomaly detection in time series
https://adtk.readthedocs.io
Mozilla Public License 2.0
1.09k stars 145 forks source link

I encountered a strange error when using adtk's plot method: ValueError: Multi-dimensional indexing (e.g. obj[:, None]) is no longer supported. Convert to a numpy array before indexing instead #150

Open TonyEinstein opened 1 year ago

TonyEinstein commented 1 year ago

My code is:

` df = pd.read_csv('cleaned_data/原始值-周均价.csv') df['date'] = pd.to_datetime(df['date']) df.set_index('date',inplace=True) df = validate_series(df)

from adtk.data import validate_series from adtk.visualization import plot from adtk.transformer import RollingAggregate from adtk.transformer import DoubleRollingAggregate from adtk.detector import ThresholdAD threshold_ad = ThresholdAD(high=10, low=5) anomalies = threshold_ad.detect(df)

plot(df, anomaly=anomalies, ts_linewidth=1, ts_markersize=3, anomaly_markersize=5, anomaly_color='red', anomaly_tag="marker"); `

I encountered an error:

` ValueError Traceback (most recent call last) Cell In[25], line 18 13 threshold_ad = ThresholdAD(high=10, low=5) 14 anomalies = threshold_ad.detect(df) ---> 18 plot(df, anomaly=anomalies, ts_linewidth=1, ts_markersize=3, anomaly_markersize=5, anomaly_color='red', anomaly_tag="marker");

File ~\anaconda3\envs\d2l\lib\site-packages\pandas\core\indexers\utils.py:341, in disallow_ndim_indexing(result) 333 """ 334 Helper function to disallow multi-dimensional indexing on 1D Series/Index. 335 (...) 338 in GH#30588. 339 """ 340 if np.ndim(result) > 1: --> 341 raise ValueError( 342 "Multi-dimensional indexing (e.g. obj[:, None]) is no longer " 343 "supported. Convert to a numpy array before indexing instead." 344 )

ValueError: Multi-dimensional indexing (e.g. obj[:, None]) is no longer supported. Convert to a numpy array before indexing instead. `

My version information:

adtk==0.6.2

I have made the following solutions, but none of them can be solved:

  1. I asked chatgpt, and it told me to switch to an array, but although I knew that the plot method required df, I still tried an array, and it was really an error.

I can't solve this problem. The great open source tycoon can give me some suggestions or solutions.

ljluestc commented 4 weeks ago
import pandas as pd
from adtk.data import validate_series
from adtk.visualization import plot
from adtk.transformer import RollingAggregate
from adtk.transformer import DoubleRollingAggregate
from adtk.detector import ThresholdAD

# Load data
df = pd.read_csv('cleaned_data/原始值-周均价.csv')
df['date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)

# Validate the series
df = validate_series(df)

# Define the anomaly detector
threshold_ad = ThresholdAD(high=10, low=5)
anomalies = threshold_ad.detect(df)

# Convert Series to NumPy array
df_array = df.to_numpy()
anomalies_array = anomalies.to_numpy()

# Plot using the converted arrays
plot(df_array, anomaly=anomalies_array, ts_linewidth=1, ts_markersize=3, anomaly_markersize=5, anomaly_color='red', anomaly_tag="marker")