Python中常见错误汇总(持续更新中)

Problem1 ModuleNotFoundError: No module named 'sklearn.datasets.samples_generator’

from sklearn.datasets.samples_generator import make_blobs

ModuleNotFoundError: No module named 'sklearn.datasets.samples_generator’

原因

samples_generator模块在新版本scikit-learn中已被移除。

samples_generator模块中相应的类/函数直接从sklearn.datasets中导入即可。

Problem2 ‘sklearn.externals’ (C:\anaconda\lib\site-packages\sklearn\externals_init_.py

python储存模型时报错,cannot import name ‘joblib’ from ‘sklearn.externals’ (C:\anaconda\lib\site-packages\sklearn\externals_init_.py)

原因:

使用的anaconda 而不是自己下载的python

解决:

删除:from sklearn.externals import joblib
更改为:import joblib

Problem3 AttributeError: module ‘tensorflow’ has no attribute ‘Session’

AttributeError: module ‘tensorflow’ has no attribute ‘Session’

解释:

这个错误表明你正在尝试从tensorflow模块中访问一个不存在的属性Session。在TensorFlow 2.x版本中,tensorflow.Session已被移除,因为TensorFlow 2.x版本引入了新的API,其中最主要的变化是使用tf.function来构建图结构,并使用Eager Execution来执行操作,这样可以让编程变得更加直观和简单。

解决方法:

如果你的代码是为TensorFlow 1.x编写的,并且你想在TensorFlow 2.x环境中运行它,你需要做一些修改。以下是几种解决方法:

  1. 如果你只是想使用TensorFlow 1.x的行为,可以通过以下代码在TensorFlow 2.x中启用TensorFlow 1.x兼容模式:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

然后你可以使用tf.Session创建一个会话。

  1. 如果你想迁移到TensorFlow 2.x的API,你需要将所有tf.Session的用法替换为tf.compat.v1.Session,并将图构建的代码迁移到tf.function装饰器中。

  2. 另一个选择是安装TensorFlow 1.x版本,可以通过pip安装一个特定版本的TensorFlow:

pip install tensorflow==1.15

确保选择与你的代码兼容的版本。

Problem4 波士顿数据集导入错误

ImportError: load_boston has been removed from scikit-learn since version 1.2. 波士顿数据集导入错误

该问题是由于在sklearn 1.2之后版本中被移除造成的。

不过可以通过以下网址之一获得这个数据集:

http://lib.stat.cmu.edu/datasets/boston

https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data

解决

import numpy as np
import pandas as pd
data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep=r"\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]

导入数据集的方法:

boston = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :3]])
print(boston.shape)

输出(506, 14)

或者,卸载现有的scikit-learn:

pip uninstall scikit-learn

之后,使用下面的命令下载比1.2版本的scikit-learn低的版本:

pip install scikit-learn==1.1.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

Problem5 AttributeError: module ‘sklearn.metrics’ has no attribute ‘calinski_harabaz_score’

AttributeError: module ‘sklearn.metrics’ has no attribute ‘calinski_harabaz_score’

metrics.calinski_harabaz_score(X, y_pred)

改为

metrics.calinski_harabasz_score(X, y_pred)

官方示例

Problem6 AttributeError: module ‘matplotlib.pyplot’ has no attribute ‘df_cm1’

AttributeError: module ‘matplotlib.pyplot’ has no attribute ‘df_cm1’

# 计算混淆矩阵  
cm = confusion_matrix(y_true, y_pred)  
  
# 将混淆矩阵转换为 DataFrame  
df_cm1 = pd.DataFrame(cm, index=['类别0', '类别1'], columns=['类别0', '类别1'])  # 假设你有两个类别  
  
# 绘制热图  
plt.figure(figsize=(10, 7))  
sns.heatmap(df_cm1, annot=True, cmap='Blues')  
  

Problem 7 ValueError: Number of rows must be a positive integer, not 5.0

ValueError: Number of rows must be a positive integer, not 5.0
经过查看

ax1 = p.add_subplot(num/10,10,fignum+1)

改为

ax1 = p.add_subplot(int(num/10),10,fignum+1)

只需要给num/10加一个int函数,转为整型即可

本文由笔者整理而成,内容收集于网络,整理不易,转载注明出处,持续更新中ing

相关推荐

  1. Python常见错误汇总持续更新

    2024-04-20 20:38:09       13 阅读
  2. python】一些常用命令汇总持续更新……)

    2024-04-20 20:38:09       23 阅读
  3. HTTP常见状态码(持续更新~~)

    2024-04-20 20:38:09       28 阅读
  4. 前端知识汇编持续更新

    2024-04-20 20:38:09       41 阅读
  5. 【ADB】常见命令汇总持续更新

    2024-04-20 20:38:09       13 阅读
  6. mapper.xml特殊SQL语句汇总持续更新

    2024-04-20 20:38:09       11 阅读
  7. 2024前端面试题汇总持续更新

    2024-04-20 20:38:09       11 阅读
  8. Python基础----数据容器(持续更新)

    2024-04-20 20:38:09       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-20 20:38:09       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-20 20:38:09       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-20 20:38:09       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-20 20:38:09       20 阅读

热门阅读

  1. ● Queryable State实现原理与配置方法

    2024-04-20 20:38:09       16 阅读
  2. python-selenium +Chrome driver环境配置

    2024-04-20 20:38:09       14 阅读
  3. 比特币减半后适合挖矿吗

    2024-04-20 20:38:09       12 阅读
  4. 【SpringBoot】springboot的启动初步理解

    2024-04-20 20:38:09       13 阅读
  5. 李沐44_物体检测算法R_CNN_SSD_YOLO

    2024-04-20 20:38:09       11 阅读
  6. 【Matlab】Sobol灵敏度分析

    2024-04-20 20:38:09       13 阅读