python 数据挖掘最佳入门案列,来自Kaggle分享

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 数据挖掘最佳入门案列,来自Kaggle分享相关的知识,希望对你有一定的参考价值。

# This script shows you how to make a submission using a few
# useful Python libraries.
# It gets a public leaderboard score of 0.76077.
# Maybe you can tweak it and do better...?

import pandas as pd
import xgboost as xgb
from sklearn.preprocessing import LabelEncoder
import numpy as np

# Load the data
train_df = pd.read_csv('../input/train.csv', header=0)
test_df = pd.read_csv('../input/test.csv', header=0)

# We'll impute missing values using the median for numeric columns and the most
# common value for string columns.
# This is based on some nice code by 'sveitser' at http://stackoverflow.com/a/25562948
from sklearn.base import TransformerMixin
class DataFrameImputer(TransformerMixin):
    def fit(self, X, y=None):
        self.fill = pd.Series([X[c].value_counts().index[0]
            if X[c].dtype == np.dtype('O') else X[c].median() for c in X],
            index=X.columns)
        return self
    def transform(self, X, y=None):
        return X.fillna(self.fill)

feature_columns_to_use = ['Pclass','Sex','Age','Fare','Parch']
nonnumeric_columns = ['Sex']

# Join the features from train and test together before imputing missing values,
# in case their distribution is slightly different
big_X = train_df[feature_columns_to_use].append(test_df[feature_columns_to_use])
big_X_imputed = DataFrameImputer().fit_transform(big_X)

# 极简标签数据转换 
# 例: LabelEncoder().fit_transform(['a','b','b']) ->> array([0, 1, 1], dtype=int64)
# XGBoost目前不会自动将标签转换为整数值,需要预处理
# 详细: http://scikit-learn.org/stable/modules/preprocessing.html#preprocessing
le = LabelEncoder()
for feature in nonnumeric_columns:
    big_X_imputed[feature] = le.fit_transform(big_X_imputed[feature])

# 准备输入数据
train_X = big_X_imputed[0:train_df.shape[0]].as_matrix()
test_X = big_X_imputed[train_df.shape[0]::].as_matrix()
train_y = train_df['Survived']

# You can experiment with many other options here, using the same .fit() and .predict()
# methods; see http://scikit-learn.org
# This example uses the current build of XGBoost, from https://github.com/dmlc/xgboost
gbm = xgb.XGBClassifier(max_depth=3, n_estimators=300, learning_rate=0.05).fit(train_X, train_y)
predictions = gbm.predict(test_X)

# Kaggle needs the submission to have a certain format;
# see https://www.kaggle.com/c/titanic-gettingStarted/download/gendermodel.csv
# for an example of what it's supposed to look like.
submission = pd.DataFrame({ 'PassengerId': test_df['PassengerId'],
                            'Survived': predictions })
submission.to_csv("submission.csv", index=False)

以上是关于python 数据挖掘最佳入门案列,来自Kaggle分享的主要内容,如果未能解决你的问题,请参考以下文章

Python从简单案列中揭示常用内置函数以及数据类型

Redux入门

想学数据分析,SPSS/R语言/Python/SQL中哪个才是小白最佳入门工具?

不容错过,零基础入门 Python学习路线最佳实践

毫无头绪的自学Python,你可能连门槛都摸不到!最佳学习路线

gRPC最佳入门教程,Golang/Python/PHP多语言讲解