极客前沿

AI入门:从零开始安装并跑通你的第一个模型

2026-06-08 01:32
DEV Machine Learning
查看原文

手把手教你安装Python和AI工具,跑通第一个机器学习模型。适合零基础读者。

准备你的AI环境

在开始之前,你需要安装Python(一种编程语言,用来写AI代码)。去官网 python.org 下载最新版本,安装时记得勾选“Add Python to PATH”。

然后安装Jupyter Notebook(一个写代码的网页工具,方便运行和测试)。打开命令提示符(Windows按Win+R,输入cmd),输入:pip install jupyter

安装AI库

我们需要scikit-learn(一个简单好用的机器学习库)和pandas(处理数据的工具)。在命令提示符中输入:

Tutorial Image
  • pip install scikit-learn
  • pip install pandas

如果下载慢,可以加镜像源:pip install scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple

跑通你的第一个模型

打开Jupyter Notebook,在终端输入jupyter notebook,浏览器会自动打开。新建一个Python文件,复制以下代码:

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
import pandas as pd

# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target

# 训练一个决策树模型
model = DecisionTreeClassifier()
model.fit(X, y)

# 预测一朵新花
new_flower = [[5.1, 3.5, 1.4, 0.2]]
prediction = model.predict(new_flower)
print('预测结果:', iris.target_names[prediction[0]])

按Shift+Enter运行,如果输出“预测结果: setosa”,说明你成功了!

下一步可以做什么

  • 试试修改new_flower里的数字,预测不同品种。
  • 学习更多模型,比如KNN(根据邻居分类)或线性回归(预测数值)。
  • 用真实数据(比如Excel表格)训练模型。

内容来源

DEV Machine Learning

发布时间

2026-06-08 01:32

返回 AI技术