125404629 发表于 2023-10-10 15:29:05

Python-minirocket ZeroDivisionError

本帖最后由 125404629 于 2023-10-10 17:40 编辑

求助大神!
深度学习MiniRocket 报错! QQ8065559062,如能解决必有重谢

发生异常: ZeroDivisionError
division by zero
File "D:\0000可见光2\程序\MiniRocket\from sktime.transformations.panel.py", line 20, in <module>
    X_train_trans = minirocket.fit_transform(X_train)
ZeroDivisionError: division by zero

数据请联系QQ

import numpy as np
import pandas as pd
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sktime.transformations.panel.rocket import MiniRocketMultivariate


df1 = pd.read_csv("train1.csv")
df1 = np.array(df1)
X = df1[:, 1:891]# 对数据进行增维并转化为32为
Y = df1[:, 0]
print(X.shape, Y.shape)
X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.8, random_state=42)

# Apply MINIROCKET transformation (feature extraction) to train and test data
minirocket = MiniRocketMultivariate(num_kernels=10, random_state=42)
#minirocket = MiniRocket()
X_train_trans = minirocket.fit_transform(X_train)
X_val_trans= minirocket.fit_transform(X_val)
print(X_train_trans.shape, X_val_trans.shape)
print(2)
# Train a linear classifier (Logistic Regression) on the transformed training data
classifier = LogisticRegression(max_iter=1000, random_state=42)
classifier.fit(X_train_trans, y_train)
print(3)
y_train_pre = classifier.predict_proba(X_train_trans)
accuracy_cal = accuracy_score(y_train, y_train_pre)
print(f'Calibration Accuracy: {accuracy_cal * 100:.2f}%')
print(4)

isdkz 发表于 2023-10-10 15:29:45

这个报错是因为在MiniRocketMultivariate.fit_transform()方法中,有一个除法运算,分母为0,导致了ZeroDivisionError异常。可以将num_kernels的值调整为一个较小的数,比如2,来避免这个问题。同时,建议在X_train和X_val上分别进行fit_transform(),因为这两个数据集可能有不同的特征分布。

修改后的代码如下:

import numpy as np
import pandas as pd
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sktime.transformations.panel.rocket import MiniRocketMultivariate

df1 = pd.read_csv("train1.csv")
df1 = np.array(df1)
X = df1[:, 1:891]
Y = df1[:, 0]
print(X.shape, Y.shape)
X_train, X_val, y_train, y_val = train_test_split(X, Y, test_size=0.8, random_state=42)

# Apply MINIROCKET transformation (feature extraction) to train and test data
minirocket = MiniRocketMultivariate(num_kernels=2, random_state=42)
X_train_trans = minirocket.fit_transform(X_train)
X_val_trans= minirocket.fit_transform(X_val)
print(X_train_trans.shape, X_val_trans.shape)

# Train a linear classifier (Logistic Regression) on the transformed training data
classifier = LogisticRegression(max_iter=1000, random_state=42)
classifier.fit(X_train_trans, y_train)
y_train_pre = classifier.predict_proba(X_train_trans)
accuracy_cal = accuracy_score(y_train, y_train_pre.argmax(axis=1))
print(f'Calibration Accuracy: {accuracy_cal * 100:.2f}%')


希望能够帮到你!

Mike_python小 发表于 2023-10-10 15:30:35

根据您提供的代码和错误信息,问题可能出现在以下几个方面:

1. 零除错误(ZeroDivisionError)一般是由于代码中存在除数为零的情况导致的。请检查您的代码中是否存在该情况。具体到您的代码,可以查看 MiniRocketMultivariate 的实现细节,看是否在内部进行了除法计算,并检查除数是否可能为零。如果可能为零,请确保在计算之前进行适当的条件判断,避免除数为零的情况发生。

2. 数据处理错误:在您的代码中,您从文件中读取数据并将其转换为数组。请确保读取的数据没有缺失值或异常值,并且数据类型正确。这可能会导致后续计算出现错误。

3. 数据维度不匹配:您在训练集和验证集上使用了不同的数据转换器 minirocket.fit_transform。请确认两者的数据维度是否一致。如果维度不匹配,可能会导致转换时出现错误。

综上所述,您可以按照以下步骤来解决问题:

1. 检查代码中是否存在除数为零的情况,并进行适当的条件判断。
2. 确保读取的数据没有缺失值或异常值,并且数据类型正确。
3. 检查训练集和验证集的数据维度是否一致。

希望能对您有所帮助!如有进一步问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}

125404629 发表于 2023-10-10 15:37:09

isdkz 发表于 2023-10-10 15:29
这个报错是因为在MiniRocketMultivariate.fit_transform()方法中,有一个除法运算,分母为0,导致了ZeroDiv ...

还是错误,数据为pan.baidu.com/s/1QEl9KaUjf9nOQo8JexmWPw?pwd=1111

125404629 发表于 2023-10-10 15:37:53

Mike_python小 发表于 2023-10-10 15:30
根据您提供的代码和错误信息,问题可能出现在以下几个方面:

1. 零除错误(ZeroDivisionError)一般是由于 ...

还没有解决,数据为pan.baidu.com/s/1QEl9KaUjf9nOQo8JexmWPw?pwd=1111

125404629 发表于 2023-10-11 09:14:35

{:10_269:}
页: [1]
查看完整版本: Python-minirocket ZeroDivisionError