1 Star 0 Fork 0

KyleXue/卢宏熙的作业

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
penguins.py 1.96 KB
一键复制 编辑 原始数据 按行查看 历史
KyleXue 提交于 2024-10-10 07:13 . update README.md.
# 导入所需的库
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
# 读取数据集
url = "https://raw.githubusercontent.com/MicrosoftDocs/mslearn-introduction-to-machine-learning/main/Data/ml-basics/penguins.csv"
df = pd.read_csv(url)
# 打印前五行数据
print(df.head())
# 显示包含缺失值的行
print("\nRows with missing values:")
print(df[df.isnull().any(axis=1)])
# 删除含有缺失值的行
df_clean = df.dropna()
# 用条形图可视化企鹅种类的分布
sns.countplot(x='Species', data=df_clean)
plt.title('Distribution of Penguin Species')
plt.show()
# 用箱线图可视化 FlipperLength, CulmenLength 和 CulmenDepth 的分布
plt.figure(figsize=(15,5))
plt.subplot(1, 3, 1)
sns.boxplot(x='Species', y='FlipperLength', data=df_clean)
plt.title('Flipper Length Distribution by Species')
plt.subplot(1, 3, 2)
sns.boxplot(x='Species', y='CulmenLength', data=df_clean)
plt.title('Culmen Length Distribution by Species')
plt.subplot(1, 3, 3)
sns.boxplot(x='Species', y='CulmenDepth', data=df_clean)
plt.title('Culmen Depth Distribution by Species')
plt.tight_layout()
plt.show()
# 准备训练模型:将数据分为特征和标签
X = df_clean[['CulmenLength', 'CulmenDepth', 'FlipperLength']] # 特征
y = df_clean['Species'] # 标签
# 将数据划分为训练集和测试集,30%的数据用于测试
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# 创建多分类的逻辑回归模型并训练
model = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=200)
model.fit(X_train, y_train)
# 预测测试集的标签
y_pred = model.predict(X_test)
# 计算模型的准确率
accuracy = accuracy_score(y_test, y_pred)
print(f"Model accuracy: {accuracy * 100:.2f}%")
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/KyleXue/lu-hongxis-homework.git
git@gitee.com:KyleXue/lu-hongxis-homework.git
KyleXue
lu-hongxis-homework
卢宏熙的作业
master

搜索帮助