问题描述
这是一个线性回归算法的代码。主要引入的库有:
## 加载库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
## 加载statsmodels 的api
import statsmodels.api as sm
import statsmodels.formula.api as smf
运行报错截图

存在的问题
- 报错显示乱码,不清楚中文部分内容。
Traceback (most recent call last):
File "c:\Users\Administrator\Desktop\stasmodels.py", line 7, in <module>
import statsmodels.api as sm
- 报错信息显示出错的行在第7行
解决办法
1、消除输出控制台乱码
以 windows 系统为例,添加系统变量:
PYTHONIOENCODING=utf-8
 配置完成后,重启vs code,再次运行程序,查看结果:显示错误信息乱码变成中文。 
2、消除找不到import statsmodels.api as sm所指的程序问题
经过一番尝试,是由于scipy或者statsmodels包版本不一致导致的,需要重新分别卸载 然后重新安装最新版的就好了。 卸载和安装最新版的scipy命令:
pip uninstall scipy
pip install scipy
卸载和安装最新版的statsmodels命令:
pip uninstall statsmodels
pip install statsmodels
运行成功后的结果
 
设置python导入其他库包文件的镜像地址为国内镜像的方法
- 在C盘的users文件夹下创建一个文件夹pip
- 在该pip文件夹下创建一个文本,改名为pip,后缀名改为.ini
- pip.ini中填写下列脚本并保存:
[global]
index-url=https://mirrors.aliyun.com/pypi/simple

设置VS code的python环境为anaconda的python环境。
anaconda默认安装后的python环境地址为:
C:\ProgramData\Anaconda3
在VS code设置面板(打开快捷键为:ctrl+, )中搜索:python path,找到python path选项将上面的地址填入:C:\ProgramData\Anaconda3 。效果如图:

|