数据挖掘比赛流程(新手向)--基于kaggle房价预测

1.评价指标

本次kaggle房价预测采用大多数回归问题常用的RMSE指标
示例代码

def rmse_cv(model,X,y):
    rmse = np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv=5))
    return rmse

2.数据可视化

plt.scatter()  #绘制连续型特征
sns.displot()  #绘制连续型特征
sns.barplot() #绘制离散型特征
sns.boxplot() #绘制连续型特征,箱图多用于比较。在箱子很扁或有很多异常值时尝试log变换
sns.pointplot() #绘制连续型特征
sns.violinplot() #综合了箱式图和密度图
sns.heatmap(df.corr(),vmax = 8,square = Ture)  #绘制相关性图

3.数据预处理

3.1缺失值处理:

b = df[(df[]>100)&(df[]<40)]
df = df.drop(b.index) #过滤删除离群点
a = df.isnull().sum()   
a[a>0].sort_values #筛选过滤缺失值
df.seclect_dtypes(exclude = ["object"]) #通过数据类型进行筛选
full["列名''].fillna(full["列名''].mode()[0],inplace = True) #众数填充
full["列名''].fillna(full["列名''].median(),inplace = True) #中位数填充
data1[].groupby(data1['列名'],data1[列名']).transform(lambda x : x.fillna(x.median)) #对重要的特征分组填充
#还可以用随机森林等方法填充

3.2数据分组

对比较重要的特征通过键进行分组

data1[].groupby(data1['列名'],data1[列名']).mean()#按照中位数分组
分组键的类型:
1.DataFrame的列名的值
2.分组轴向上的值和分组名称相匹配的字典或Series
3.轴索引或索引中的单个标签上调用的函数

3.3定性数据的处理

定量数据:变量可以以某种方式排序,比如Age,一般通过cut()或qcut()进行离散化

pd.qcut(数据,分组数,labels = [用来指示分组名称])#按照频率分桶
pd.cut(数据,分组数,labels = [用来指示分组名称])#按照均值分桶
df["列名"] = np.log1p(df["列名"]) #标准正态化
df["列名"] = boxcox1p(df["列名"],lam = 0.05) # box-cox化,需要from scipy.special import boxcox1p

from sklearn.preprocessing import Binarizer
data = Binarizer(threshold = epsilon).fit_transform(data) #定量数据二值化(大于epsilon为1,小于等于epsilon为0):

定性数据:变量描述了物体的某一(不能被数字所描述的)方面,比如省份,一般进行LabelEncoder或OneHot

4.建模(以lasso为例)

clf1 = LassoCV(alphas = [1, 0.1, 0.001, 0.0005,0.0003,0.0002, 5e-4])
clf1.fit(X, y)
lasso_preds = np.expm1(clf1.predict(test_X_scaled )) # exp(x) - 1  <---->log1p(x)==log(1+x)
score1 = rmse_cv(clf1,X, y) # rmse_cv为1中提到的评价指标
print("\nLasso score: {:.4f} ({:.4f})\n".format(score1.mean(), score1.std()))


猜你喜欢

转载自blog.csdn.net/guaixi/article/details/90648143