VS Code中安装Python机器学习与数据分析相关第三方模块教程

我们需要使用pip来引入Python的第三方模块,pip是Python包管理工具,该工具提供了对Python包的查找、下载、安装与卸载等功能。

(1)更新pip:

打开VS Code后在命令行终端进行操作,本文中假设Python的安装路径为D:\Python3.10

D:\Python3.10\python.exe -m pip install --upgrade pip

(2)安装numpy

Numpy库支持数组、矩阵等运算,也是OpenCV所需要的模块之一。

pip install numpy

(3)安装matplotlib

Matplotlib库在显示图像,绘制图表方面很方便,建议大家安装一下。

pip install matplotlib

(4)安装pandas

Pandas库依赖于Numpy库,是一个开放源码、BSD 许可的库,提供高性能、易于使用的数据结构和数据分析工具。

pip install pandas

(5)安装opencv-python

OpenCV-Python是一个Python绑定库,旨在解决计算机视觉问题。

pip install opencv-python

能够成功运行以下代码表示安装成功:

import cv2
# 读一个图片并进行显示(图片路径需自己指定)
lena=cv2.imread("D:\\VS Code Projects\\Python\\1.jpg")
cv2.imshow("image",lena)
cv2.waitKey(0)

如果遇到AttributeError: module 'cv2' has no attribute 'quality'之类的报错可以将opencv-python卸了安装opencv-contrib-python

pip uninstall opencv-python
pip install opencv-contrib-python

(6)安装scipy

Scipy是一个用于数学、科学、工程领域的常用软件包,可以处理插值、积分、优化、图像处理、常微分方程数值解的求解、信号处理等问题。它用于有效计算Numpy矩阵,使Numpy和Scipy协同工作,高效解决问题。

pip install scipy

(7)安装scikit-learn

Scikit-Learn是一个开源的机器学习库,它支持有监督和无监督的学习。它还提供了用于模型拟合,数据预处理,模型选择和评估以及许多其他实用程序的各种工具。

注意:安装scikit-learn之前需要先安装numpyscipy

pip install scikit-learn

能够成功运行以下代码表示安装成功:

import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import make_blobs
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis


n_train = 20  # samples for training
n_test = 200  # samples for testing
n_averages = 50  # how often to repeat classification
n_features_max = 75  # maximum number of features
step = 4  # step size for the calculation


def generate_data(n_samples, n_features):
    """Generate random blob-ish data with noisy features.

    This returns an array of input data with shape `(n_samples, n_features)`
    and an array of `n_samples` target labels.

    Only one feature contains discriminative information, the other features
    contain only noise.
    """
    X, y = make_blobs(n_samples=n_samples, n_features=1, centers=[[-2], [2]])

    # add non-discriminative features
    if n_features > 1:
        X = np.hstack([X, np.random.randn(n_samples, n_features - 1)])
    return X, y

acc_clf1, acc_clf2 = [], []
n_features_range = range(1, n_features_max + 1, step)
for n_features in n_features_range:
    score_clf1, score_clf2 = 0, 0
    for _ in range(n_averages):
        X, y = generate_data(n_train, n_features)

        clf1 = LinearDiscriminantAnalysis(solver='lsqr', shrinkage='auto').fit(X, y)
        clf2 = LinearDiscriminantAnalysis(solver='lsqr', shrinkage=None).fit(X, y)

        X, y = generate_data(n_test, n_features)
        score_clf1 += clf1.score(X, y)
        score_clf2 += clf2.score(X, y)

    acc_clf1.append(score_clf1 / n_averages)
    acc_clf2.append(score_clf2 / n_averages)

features_samples_ratio = np.array(n_features_range) / n_train

plt.plot(features_samples_ratio, acc_clf1, linewidth=2,
         label="Linear Discriminant Analysis with shrinkage", color='navy')
plt.plot(features_samples_ratio, acc_clf2, linewidth=2,
         label="Linear Discriminant Analysis", color='gold')

plt.xlabel('n_features / n_samples')
plt.ylabel('Classification accuracy')

plt.legend(loc=1, prop={'size': 12})
plt.suptitle('Linear Discriminant Analysis vs. \
shrinkage Linear Discriminant Analysis (1 discriminative feature)')
plt.show()

(8)查看目前已安装的库:

pip list

(9)更新已安装的库:

pip install --upgrade numpy

你可能感兴趣的:(杂项,python,机器学习,数据分析,人工智能,sklearn)