Chuẩn bị bài nộp đầu tiên

Chinh phục cuộc thi Kaggle bằng Python

Yauhen Babakhin

Kaggle Grandmaster

Bài nộp là gì

 

 

quy trình cuộc thi kaggle

Chinh phục cuộc thi Kaggle bằng Python

Dự đoán giá cước taxi New York

# Read train data
taxi_train = pd.read_csv('taxi_train.csv')
taxi_train.columns.to_list()
['key',
 'fare_amount',
 'pickup_datetime',
 'pickup_longitude',
 'pickup_latitude',
 'dropoff_longitude',
 'dropoff_latitude',
 'passenger_count']
Chinh phục cuộc thi Kaggle bằng Python

Loại bài toán

import matplotlib.pyplot as plt

# Plot a histogram
taxi_train.fare_amount.hist(bins=30, alpha=0.5)
plt.show()

biểu đồ tần suất fare_amount

Chinh phục cuộc thi Kaggle bằng Python

Xây dựng mô hình

from sklearn.linear_model import LinearRegression

# Create a LinearRegression object
lr = LinearRegression()
# Fit the model on the train data
lr.fit(X=taxi_train[['pickup_longitude', 'pickup_latitude', 'dropoff_longitude',
                     'dropoff_latitude', 'passenger_count']],
       y=taxi_train['fare_amount'])
Chinh phục cuộc thi Kaggle bằng Python

Dự đoán trên tập kiểm tra

# Select features
features = ['pickup_longitude', 'pickup_latitude',
            'dropoff_longitude', 'dropoff_latitude',
            'passenger_count']
# Make predictions on the test data
taxi_test['fare_amount'] = lr.predict(taxi_test[features])
Chinh phục cuộc thi Kaggle bằng Python

Chuẩn bị bài nộp

# Read a sample submission file
taxi_sample_sub = pd.read_csv('taxi_sample_submission.csv')
taxi_sample_sub.head(1)
                              key     fare_amount
0     2015-01-27 13:08:24.0000002     11.35
# Prepare a submission file
taxi_submission = taxi_test[['key', 'fare_amount']]

# Save the submission file as .csv
taxi_submission.to_csv('first_sub.csv', index=False)
Chinh phục cuộc thi Kaggle bằng Python

Let's practice!

Chinh phục cuộc thi Kaggle bằng Python

Preparing Video For Download...