【数据分析实战】kaggle项目:bike sharing demand

一、 导入数据

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
%config InlineBackend.figure_format = 'svg'

bike_df0 = pd.read_csv('data/bike/train.csv')
bike_df0.info()
"""
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10886 entries, 0 to 10885
Data columns (total 12 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   datetime    10886 non-null  object 
 1   season      10886 non-null  int64  
 2   holiday     10886 non-null  int64  
 3   workingday  10886 non-null  int64  
 4   weather     10886 non-null  int64  
 5   temp        10886 non-null  float64
 6   atemp       10886 non-null  float64
 7   humidity    10886 non-null  int64  
 8   windspeed   10886 non-null  float64
 9   casual      10886 non-null  int64  
 10  registered  10886 non-null  int64  
 11  count       10886 non-null  int64  
dtypes: float64(3), int64(8), object(1)
memory usage: 1020.7+ KB
"""

bike_df0.describe([0.01, 0.99])

在这里插入图片描述

二、 特征工程


2.1 类型转换

bike_df1 = bike_df0.copy()

def transform_datetime(df):
    # 将datetime处理成datetime类型
    df.datetime = pd.to_datetime(df.datetime)
    # 分别得到year、hour特征
    df['year'] = df.datetime.dt.year
    df['hour'] = df.datetime.dt.hour
    # 原来的datetime抛弃
    df.drop(columns=['datetime'], inplace=True)

transform_datetime(bike_df1)

2.2 特征筛选

# 查看相关系数
plt.figure(figsize=(12, 8))
sns.heatmap(bike_df1.corr(), vmin=-1, cmap=plt.cm.coolwarm, annot=True)

在这里插入图片描述

# 丢弃atemp列、holiday列、windspeed列和count列
bike_df1.drop(columns=['atemp', 'holiday', 'windspeed', 'count'], inplace=True)

2.3 异常值处理

# 画图查看hour列、temp列、humidity列与count的关系
def show_img(df, col_name, value_name):
    temp = pd.pivot_table(df, index=[col_name], values=[value_name], aggfunc='mean')
    plt.figure(figsize=(6,4))
    sns.lineplot(
        x=col_name, 
        y=value_name,
        data=temp
    )
    plt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sprite.Nym

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值