task2 数据分析
2.1 学习目标
-
学习如何对数据集整体概况进行分析,包括数据集的基本情况(缺失值、异常值)
-
学习了解变量之间的相互关系、变量与预测值之间的存在关系。
-
完成相应学习打卡任务
2.2 内容介绍
- 数据总体了解
- 读取数据集并了解数据集的大小,原始特征维度;
- 通过info了解数据类型;
- 粗略查看数据集中各特征的基本统计量
- 缺失值和唯一值
- 查看数据缺失值情况
- 查看唯一值情况
- 数据特性和特征分布
- 三类渔船轨迹的可视化
- 坐标序列可视化
- 三类渔船速度和方向序列可视化
- 三类渔船速度和方向的数据分布
- 总结
2.3 代码示例
01 载入库及包
#coding:utf-8
#导入warnings包,利用过滤器来实现忽略警告语句。
import warnings
warnings.filterwarnings('ignore')
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
from tqdm import tqdm
import multiprocessing as mp
import os
import pickle
import random
# 把读取所有数据的函数放在单独的python文件中,是为了解决多线程问题
import read_all_data
# 这里面的数据位置需要修改一下
df = pd.read_csv('data/DF.csv')
02 定义加载和存储数据的函数
class Load_Save_Data():
def __init__(self,file_name=None):
self.filename = file_name
def load_data(self,Path=None):
if Path is None:
assert self.filename is not None,"Invalid Path...."
else:
self.filename = Path
with open(self.filename,"wb") as f:
data = pickle.load(f)
return data
def save_data(self,data,path):
if path is None:
assert self.filename is not None,"Invalid path...."
else:
self.filename = path
with open(self.filename,"wb") as f:
pickle.dump(data,f)
03 读取数据
# 定义读取数据的函数
def read_data(Path,Kind=""):
"""
:param Path:待读取数据的存放路径
:param Kind:'train' of 'test'
"""
# 替换成数据存放的路径
filenames = os.listdir(Path)
print("\n@Read Data From"+Path+".........................")
with mp.Pool(processes=mp.cpu_count()) as pool:
data_total = list(tqdm(pool.map(read_all_data.read_train_file if Kind == "train" else
read_all_data.read_test_file,filenames),total=len(filenames)))
print("\n@End Read total Data............................")
load_save = Load_Save_Data()
if Kind == "train":
load_save.save_data(data_total,"./data_tmp/total_data.pkl")
return data_total
# 训练数据读取
# 存放数据的绝对路径
train_path = "/Users/mac/Documents/study/dataWhale/wisdomOcean/data/hy_round1_train_20200102/"
data_train = read_data(train_path,Kind="train")
data_train = pd.concat(data_train)
# 测试数据读
# 存放数据的绝对路径
test_path = "/Users/mac/Documents/study/dataWhale/wisdomOcean/data/hy_round1_testA_20200102/"
data_test = read_data(test_path,Kind="test")
data_test = pd.concat(data_test)
04 总体数据了解
# 样本个数
data_test.shape
# (782378, 6)
# 测试数据个数
data_train.shape
# (2699638, 7)
data_train.info()
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 渔船ID 2699638 non-null int64 # 渔船的唯一识别,结果文件以此ID为标识
1 x 2699638 non-null float64 # 渔船的x坐标
2 y 2699638 non-null float64 # 渔船的y坐标
3 速度 2699638 non-null float64 # 单位节
4 方向 2699638 non-null int64 # 单位度
5 time 2699638 non-null object # 上报时刻,单位月日
6 type 2699638 non-null object # 渔船label, 作业类型
dtypes: float64(3), int64(2), object(2)
memory usage: 164.8+ MB
data_train.describe([0.01