背景知识要求
Python的pandas库。
pandas官网:http://pandas.pydata.org/
摘要
本文使用pandas库对上一篇文章:链家房源数据爬取(Scrapy)https://blog.csdn.net/qq_39206674/article/details/90114296 获取的数据进行清洗和预处理。
正文
数据清洗
原始数据部分内容如下:
数据清洗过程包括:
- 去除Nan数据和重复数据
- 去除未知和有缺失的数据
- 去除经过分析无用的数据(以房源为例:车位、别墅数据为无用数据)
数据清洗代码:
# 读文件
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标签表示房源是否满五年或两年,这一列没有去掉“暂无数据”,因为该列“暂无数据”表示房源没有满五年也没有满两年,该特征值将在后续机器学习中应用到,顾保留。
数据预处理
数据清洗后进行数据预处理,数据预处理涉及到pandas的一些数据操作:
- 去除无用数据列,既去掉了无用的特征。
- 特征数值化,既将一些非数值元素转换为数值元素。
- 数值换算,如将建筑年代2012-3-4转换为房龄7年。
- 将一列拆分为多列,如户型"4室2厅1厨1卫",拆分为4 2 1 1四列数据。
- 将一列数据做加权运算,如房屋朝向"南加权为3,北加权为-2 …"通过加权计算对房屋的评分
具体代码如下:
读文件
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)
预处理后的数据:
通过数据清洗和数据预处理产生供机器学习使用的房源数据,我们也就产生了房源的特征
,机器学习将会用到这些特征。
结论
本文使用pandas库对上一篇文章:链家房源数据爬取(Scrapy)https://blog.csdn.net/qq_39206674/article/details/90114296 获取的数据进行清洗和预处理。
得到的数据特征,后续将现对特征进行划分,通过3层神经网络,多层神经网络,深度神经网络对这些特征进行机器学习。
参考
来源:CSDN
作者:拾贝的孩子
链接:https://blog.csdn.net/qq_39206674/article/details/90114819