如何解决为什么错误:“无法从 'sklearn.metrics' 导入名称 'balanced_accuracy'” 比它应该的更复杂?
我正在尝试在 kaggle 上使用 jupyter notebooks 对 COVID-19 不平衡数据集进行多类分类项目,我的代码是:
import random
import gc
import numpy as np
from numpy import asarray
import itertools
train_dir='/kaggle/input/pandemic2/Training/Training'
test_dir='/kaggle/input/pandemic2/Testing/Testing'
train_covid=['/kaggle/input/pandemic2/Training/Training/{}'.format(i) for i in os.listdir(train_dir) if 'COVID' in I]
train_normal=['/kaggle/input/pandemic2/Training/Training/{}'.format(i) for i in os.listdir(train_dir) if 'normal' in I]
train_pneumonia=['/kaggle/input/pandemic2/Training/Training/{}'.format(i) for i in os.listdir(train_dir) if 'MERS' or
'SARS'or
'Bacterial' or
'Chlamydia' or
'Influenza' or
'Klebsiella' or
'Legionella' or
'Mycoplasma' or
'Pneumocystis' or
'Streptococcus' or
'Varicella' in I]
test_imgs=['/kaggle/input/pandemic2/Testing/Testing/{}'.format(i) for i in os.listdir(test_dir)]
train_imgs=train_covid[:] + train_normal[:] + train_pneumonia[:]
random.shuffle(train_imgs)
del train_covid
del train_normal
del train_pneumonia
gc.collect()
nrows=150
ncolumns=150
channels= 3
def read_and_process_image (list_of_images):
x=[]
y=[]
for image in list_of_images:
x.append(cv2.resize(cv2.imread(image,cv2.IMREAD_COLOR),(nrows,ncolumns),interpolation=cv2.INTER_CUBIC))
if 'normal' in image:
y.append(0)
elif 'COVID' in image:
y.append(1)
else:
y.append(2)
return x,y
x,y= read_and_process_image(train_imgs)
del train_imgs
gc.collect()
x=np.array(x)
print(x.shape)
y=np.array(y)
print(y.shape)
import sklearn
from keras import layers
from keras import models
from keras import optimizers
from sklearn.model_selection import StratifiedKFold
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array,load_img
from sklearn.model_selection import cross_val_score
from sklearn.metrics import balanced_accuracy
from keras.applications import InceptionresnetV2
conv_base= InceptionresnetV2(weights='imagenet',include_top=False,input_shape=(150,150,3))
model=models.Sequential()
model.add(conv_base)
model.add(layers.Flatten())
model.add(layers.Dense(256,activation='relu'))
model.add(layers.Dense(3,activation='softmax'))
from keras import optimizers
model.compile(loss='categorical_crossentropy',optimizer=optimizers.Adam(lr=1e-4),metrics= ['categorical_accuracy'])
train_datagen=ImageDataGenerator(rescale=1./255,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True )
val_datagen=ImageDataGenerator(rescale=1./255)
skf = StratifiedKFold(n_splits=5,shuffle=True,random_state=1)
scores = cross_val_score(model,x,y,cv=5,scoring= "balanced_accuracy")
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,stratify=y,test_size=0.20,random_state=2)
x_train,x_val,y_val = train_test_split(x_train,stratify=y_train,train_size=0.8,random_state=2)
from keras.utils import to_categorical
y_train=to_categorical(y_train,3)
print('Shape of training lables is:',y_train.shape)
y_val=to_categorical(y_val,3)
print('Shape of validation lables is:',y_val.shape)
y_test=to_categorical(y_test,3)
print('Shape of test labels is:',y_test.shape)
for index,(train_indices,val_indices) in enumerate(skf.split(x,y)):
print ("Training on fold " + str(index+1) + "/" + str(n_splits))
# Generate batches from indices
xtrain,xval = x[train_indices],x[val_indices]
ytrain,yval = y[train_indices],y[val_indices]
ntrain=len(x_train)
nval=len(x_val)
batch_size=32
train_generator=train_datagen.flow(x_train,batch_size=batch_size)
val_generator=val_datagen.flow(x_val,y_val,batch_size=batch_size)
print ("Training new iteration on " + str(xtrain.shape[0]) + " training samples," + str(xval.shape[0]) + " validation samples,this may take while...")
history=model.fit(train_generator,steps_per_epoch=ntrain//batch_size,epochs=80,validation_data=(val_generator),validation_steps=nval//batch_size,verbose=2)
print('\nBalanced Accuracy:',mterics.balanced_accurcay*100,'%')
Traceback (most recent call last)
<ipython-input-7-45c4c9070141> in <module>
6 from keras.preprocessing.image import img_to_array,load_img
7 from sklearn.model_selection import cross_val_score
----> 8 from sklearn.metrics import balanced_accuracy
9
10 #Download the model
ImportError: cannot import name 'balanced_accuracy' from 'sklearn.metrics' (/opt/conda/lib/python3.7/site-packages/sklearn/metrics/__init__.py)
我尝试了很多解决方案,例如 1、2、3 和 4,但是 this solution 将我引向了另一个复杂的问题,因为每当我运行命令 conda activate myenv
它给了我错误:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell,run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
通过 understanding it thoroughly 解决此问题并尝试此 thread 中提供的内容导致我收到以下错误消息:
/bin/bash: -c: line 0: Syntax error near unexpected token `newline' /bin/bash: -c: line 0: `/opt/conda/bin/conda init <bash>'.
然后,当我被卡住并感到被困时,我尝试按照 conda official documentation 创建一个包含所有需要的包的虚拟环境,但我遇到了上述相同的错误,指出 我的 shell 不是正确配置以激活新的 virtualenv !!
再一次,我通过执行 conda list revisions
conda install --revision=0
并更新它来恢复到我的基本环境的第一个版本,但错误仍然存在并且仍然阻止我使用 balance_accuracy 和其他有用的指标。
我还尝试创建一个新的 jupyter notebook,并通过更新我的软件包从头开始,但它们已经更新到最新版本!!
我相信我以错误的顺序运行这组配置命令,因为我的 jupyter 内核和环境现在一团糟。
如果有人能够指导我采用最佳实践为深度学习任务设置环境,我们将不胜感激。
顺便说一句:建议操作 bashrc
文件的解决方案让我有点困惑,我不完全理解它是如何工作的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。