资源简介
用随机森林实现泰坦尼克号数据集的分类预测,包含参数调试过程和分类结果评估,并绘制ROC曲线。
代码片段和文件信息
# !/usr/bin/env python
# _*_ coding: utf-8 _*_
# _*_ coding: utf-8 _*_
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
from sklearn import metrics
import time
np.seterr(divide=‘ignore‘ invalid=‘ignore‘)
train = pd.read_csv(“titanic/train.csv“ dtype={“Age“: np.float64})
# 数据清洗
def harmonize_data(titanic):
# 填充空数据 和 把string数据转成integer表示
# 对于年龄字段发生缺失,我们用所有年龄的均值替代
titanic[“Age“] = titanic[“Age“].fillna(titanic[“Age“].median())
# 性别男: 用0替代
titanic.loc[titanic[“Sex“] == “male“ “Sex“] = 0
# 性别女: 用1替代
titanic.loc[titanic[“Sex“] == “female“ “Sex“] = 1
titanic[“Embarked“] = titanic[“Embarked“].fillna(“S“)
titanic.loc[titanic[“Embarked“] == “S“ “Embarked“] = 0
titanic.loc[titanic[“Embarked“] == “C“ “Embarked“] = 1
titanic.loc[titanic[“Embarked“] == “Q“ “Embarked“] = 2
titanic[“Fare“] = titanic[“Fare“].fillna(titanic[“Fare“].median())
return titanic
train_data = harmonize_data(train)
test = pd.read_csv(“titanic/test.csv“ dtype={“Age“: np.float64})
test_data = harmonize_data(test)
# 列出对生存结果有影响的字段
predictors = [“Pclass“ “Sex“ “Age“ “SibSp“ “Parch“ “Fare“ “Embarked“]
# 不调参
x = train[predictors]
y = train_data[‘Survived‘]
x_test = test[predictors]
y_test = test_data[‘Survived‘]
# 拆分训练集和测试集(这里由于给了另外给了测试集,此处无需拆分)
# feature_train feature_test target_train target_test = train_test_split(x y test_size=0.3 random_state=10)
rf = RandomForestClassifier(oob_score=True random_state=10 n_jobs=-1)
# start = time.perf_counter()
- 上一篇:基于Python的SVM解决异或问题
- 下一篇:利用爬虫获取IP的地理位置
相关资源
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
- 量化交易(附python常见函数的使用方
- python 名字用字排行
评论
共有 条评论