ubuntu

Ubuntu Python机器学习

小樊
57
2025-10-01 19:04:43
栏目: 编程语言

Installing Python and pip
Most Ubuntu systems come with Python 3 preinstalled, but you should verify the version and install the latest updates. Run python3 --version to check; if Python 3 isn’t installed, use these commands:

sudo apt update
sudo apt install python3 python3-pip

This ensures you have Python 3 and pip (Python’s package manager) ready for machine learning workflows.

Setting Up a Virtual Environment (Recommended)
Virtual environments isolate project dependencies, preventing conflicts between libraries across different projects. Install the venv module (bundled with Python 3) and create a new environment:

sudo apt install python3-venv  # Install venv if not already available
python3 -m venv myenv          # Create a virtual environment named "myenv"
source myenv/bin/activate      # Activate the environment (your terminal prompt will change to show the environment name)

Deactivate the environment anytime with deactivate.

Installing Core Machine Learning Libraries
For basic machine learning (e.g., linear regression, decision trees), install essential libraries like NumPy (numerical computing), Pandas (data manipulation), and scikit-learn (algorithm implementation):

pip install numpy pandas scikit-learn matplotlib

Installing Deep Learning Frameworks (Optional)
For deep learning tasks (e.g., image recognition, natural language processing), install TensorFlow or PyTorch. These frameworks support GPU acceleration (via CUDA/cuDNN) for faster training.

Verify installations by importing the libraries in a Python shell and checking their versions (e.g., import tensorflow as tf; print(tf.__version__)).

Writing and Running Machine Learning Code
Use a text editor (e.g., VS Code, Sublime Text) or an IDE (e.g., PyCharm) to write scripts. Below are two common examples:

Using Jupyter Notebook (Optional but Recommended)
Jupyter Notebook is an interactive environment for data exploration and prototyping. Install it via pip:

pip install notebook

Start the Notebook server:

jupyter notebook

This opens a browser window where you can create .ipynb files (notebooks) to write code, add text, and visualize results interactively.

Key Tips for Success

0
看了该问题的人还看了