极客前沿

AI 入门:从零搭建你的第一个智能应用

2026-07-14 01:32
DEV Machine Learning
查看原文

手把手教你安装 Python、搭建环境、跑通第一个 AI 模型,零基础也能上手。

准备你的电脑环境

AI 开发的第一步是安装 Python(一种编程语言,AI 最常用的工具)。去 python.org 下载最新版(建议 3.10 或以上),安装时记得勾选“Add Python to PATH”(这样你才能在任何地方使用它)。

安装必要的 AI 库

打开终端(Windows 用 cmd 或 PowerShell,Mac/Linux 用终端),输入下面命令安装最常用的 AI 库:

  • pip install numpy pandas matplotlib(数据处理和画图)
  • pip install scikit-learn(经典机器学习算法)
  • pip install tensorflowpip install torch(深度学习框架,二选一)

坑提醒:如果安装慢,可以临时用国内镜像,如 pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow

Tutorial Image

跑通你的第一个 AI 程序

创建一个新文件 hello_ai.py,复制以下代码:

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

# 加载鸢尾花数据集(经典入门数据)
iris = datasets.load_iris()
X, y = iris.data, iris.target

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 创建并训练 KNN 分类器
clf = KNeighborsClassifier(n_neighbors=3)
clf.fit(X_train, y_train)

# 预测并计算准确率
y_pred = clf.predict(X_test)
print("准确率:", accuracy_score(y_test, y_pred))

在终端运行 python hello_ai.py,如果看到类似“准确率:0.9666”的输出,恭喜你,第一个 AI 模型跑通了!

下一步可以做什么

  • 尝试修改 n_neighbors 的值(比如 1 或 5),看准确率怎么变。
  • 换成自己的数据:把 iris 换成 CSV 文件,用 pandas 读取。
  • 学习更多模型:从 scikit-learn 官方文档的“分类”和“回归”示例开始。

内容来源

DEV Machine Learning

发布时间

2026-07-14 01:32

返回 AI技术