Python的pandas库。
pandas官网:http://pandas.pydata.org/python
本文使用pandas库对上一篇文章:链家房源数据爬取(Scrapy)http://www.javashuo.com/article/p-knxamhts-mc.html 获取的数据进行清洗和预处理。web
原始数据部份内容以下:
网络
数据清洗过程包括:app
数据清洗代码:机器学习
# 读文件 df = pd.read_csv(r'lianjia.csv', sep=',') # 打印原始基本信息 print("original data rowCount: %d" % (df.shape[0])) print("original data colCount: %d" % (df.shape[1])) print(df.dtypes) # 去除Nan数据和重复数据 df.dropna(inplace=True) df.drop_duplicates(keep="first", inplace=True) # 去除未知、暂无数据、车位、别墅 df = df[df["build_year"] != "未知"] df = df[df["gross_area"] != "暂无数据"] df = df[df["house_orientation"] != "暂无数据"] df = df[df["usable_area"] != "暂无数据"] df = df[df["with_elevator"] != "暂无数据"] df = df[df["year_of_property"] != "未知"] df = df[(df["household_style"] != "车位") & (df["household_style"] != "别墅")] df = df[df["deal_time"].str.len() <= 10] # 打印清洗后的基本信息 print("cleaned data rowCount: %d" % (df.shape[0])) print("cleaned data colCount: %d" % (df.shape[0])) # 输出清洗完成的数据 df.to_csv("lianjia_cleaned.csv", index=False)
数据清洗后内容以下:
注意:表格中is_two_five标签表示房源是否满五年或两年,这一列没有去掉“暂无数据”,由于该列“暂无数据”表示房源没有满五年也没有满两年,该特征值将在后续机器学习中应用到,顾保留。svg
数据清洗后进行数据预处理,数据预处理涉及到pandas的一些数据操做:学习
具体代码以下:ui
读文件 df = pd.read_csv(r'lianjia_cleaned.csv', sep=',') # 打印原始基本信息 print("original data rowCount: %d" % (df.shape[0])) print("original data colCount: %d" % (df.shape[1])) print(df.dtypes) # 去除无用数据列 df.drop("house_usage", axis=1, inplace=True) df.drop("year_of_property", axis=1, inplace=True) df.drop(["deal_time"], axis=1, inplace=True) # 特征数值化 df.replace({"with_elevator": "无"}, 0, inplace=True) df.replace({"with_elevator": "有"}, 1, inplace=True) df.replace({"is_two_five": "暂无数据"}, 0, inplace=True) df.replace({"is_two_five": "满两年"}, 2, inplace=True) df.replace({"is_two_five": "满五年"}, 5, inplace=True) df.loc[:, "usable_area"] = df["usable_area"].str.strip("㎡") df.loc[:, "gross_area"] = df["gross_area"].str.strip("㎡") # 房屋朝向的数值化 df_house_orientation_group = df.groupby(by='house_orientation') house_orientation_list = list(df_house_orientation_group.groups.keys()) house_orientation_dict = {"东南": 2, "南": 3, "西南": 1, "西": -1, "西北": -1.5, "北": -2, "东北": 0, "东": 1} # 将li中的房屋朝向,经过索引dic的方式转换为权重值val def split_house_orientation_list(*li, **dic): lis = [] for it in li: v = 0 for k in it.split(): if k in dic.keys(): v += dic[k] # 累加全部朝向的组合分数 lis.append(v) return lis # 房屋朝向权重列表 house_orientation_data = split_house_orientation_list(*house_orientation_list, **house_orientation_dict) # 房屋朝向替换为权重值val for item, val in zip(df_house_orientation_group.groups.keys(), house_orientation_data): df.replace({"house_orientation": item}, val, inplace=True) # 房屋户型 df[['bedroom', 'living_room', 'kitchen', 'toilet']] = df["household_style"].str.extract('(\d+)室(\d+)厅(\d+)厨(\d+)卫', expand=False) df.drop("household_style", axis=1, inplace=True) # 楼层 df["floor_number"] = df["floor_number"].str.extract('共(\d+)层', expand=False) # 建筑年限转为楼龄 df["build_year"] = df["build_year"].map(lambda x: 2019 - x) df.rename(columns={'build_year': 'house_age'}, inplace=True) print("processed data rowCount: %d" % (df.shape[0])) print("processed data colCount: %d" % (df.shape[1])) # 写文件 df.to_csv("lianjia_processed.csv", index=False)
预处理后的数据:
经过数据清洗和数据预处理产生供机器学习使用的房源数据,咱们也就产生了房源的特征
,机器学习将会用到这些特征。spa
本文使用pandas库对上一篇文章:链家房源数据爬取(Scrapy)http://www.javashuo.com/article/p-knxamhts-mc.html 获取的数据进行清洗和预处理。
获得的数据特征,后续将现对特征进行划分,经过3层神经网络,多层神经网络,深度神经网络对这些特征进行机器学习。.net