日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

kaggle(04)---avazu_ctr_predictor(baseline)

發布時間:2023/12/13 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 kaggle(04)---avazu_ctr_predictor(baseline) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

比賽的目的:

  • 通過分析網上的系統日志和用戶行為信息,來預測某些網頁上項目的點擊率。
  • 是一個二分類的問題,只需要預測出用戶是否點擊即可
  • 最好能夠輸出某個概率,比如:用戶點擊某個廣告的概率。
    比賽官網

文件信息:

train - Training set. 10 days of click-through data, ordered chronologically. Non-clicks and clicks are subsampled according to different strategies.

test - Test set. 1 day of ads to for testing your model predictions.

sampleSubmission.csv - Sample submission file in the correct format, corresponds to the All-0.5 Benchmark.

屬性信息:

  • id: ad identifier
  • click: 0/1 for non-click/click
  • hour: format is YYMMDDHH, so 14091123 means 23:00 on Sept. 11, 2014 UTC.
  • C1 – anonymized categorical variable
  • banner_pos
  • site_id
  • site_domain
  • site_category
  • app_id
  • app_domain
  • app_category
  • device_id
  • device_ip
  • device_model
  • device_type
  • device_conn_type
  • C14-C21 – anonymized categorical variables

初步分析:

  • 這是一個點擊率預測的問題,是一個二分類的問題
  • 通過初步查看給出的屬性,主要分為用戶,網站,廣告和時間四種類型的屬性
  • 時間應該是一個重要的屬性,可以好好分析,因為每個人在不同時間喜歡看不同的東西
  • 網站類型也是一個和用戶相關性比較大的屬性
  • 設備類型可以反映出用戶的一個經濟范圍和消費水平
  • 等等!肯定還有很多相關性在這些屬性中,我們應該設身處地的思考這些問題。

Load Data

import pandas as pd# Initial setup train_filename = "train_small.csv" #由于原始數據量比較多,所以這里先導入一個經過下采樣的樣本 test_filename = "test.csv" submission_filename = "submit.csv"training_set = pd.read_csv(train_filename)

Explore Data

training_set.shape (99999, 24) #我們首先看看數據的樣子 training_set.head(10) idclickhourC1banner_possite_idsite_domainsite_categoryapp_idapp_domain...device_typedevice_conn_typeC14C15C16C17C18C19C20C210123456789
1.000009e+18014102100100501fbe01fef384576728905ebdecad23867801e8d9...1215706320501722035-179
1.000017e+19014102100100501fbe01fef384576728905ebdecad23867801e8d9...101570432050172203510008479
1.000037e+19014102100100501fbe01fef384576728905ebdecad23867801e8d9...101570432050172203510008479
1.000064e+19014102100100501fbe01fef384576728905ebdecad23867801e8d9...101570632050172203510008479
1.000068e+1901410210010051fe8cc4489166c1610569f928ecad23867801e8d9...1018993320502161035-1157
1.000072e+1901410210010050d6137915bb1ef334f028772becad23867801e8d9...10169203205018990431100077117
1.000072e+19014102100100508fda644b25d4cfcdf028772becad23867801e8d9...1020362320502333039-1157
1.000092e+1901410210010051e151e2457e091613f028772becad23867801e8d9...1020632320502374339-123
1.000095e+19114102100100501fbe01fef384576728905ebdecad23867801e8d9...1215707320501722035-179
1.000126e+190141021001002084c7ba46c4e18dd650e219e0ecad23867801e8d9...0021689320502496316710019123

10 rows × 24 columns

  • 目前主要有22個屬性,其中有很多是類別的屬性。
  • 訓練集總共有99999個樣本,還行,不多也不少。
training_set.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 99999 entries, 0 to 99998 Data columns (total 24 columns): id 99999 non-null float64 click 99999 non-null int64 hour 99999 non-null int64 C1 99999 non-null int64 banner_pos 99999 non-null int64 site_id 99999 non-null object site_domain 99999 non-null object site_category 99999 non-null object app_id 99999 non-null object app_domain 99999 non-null object app_category 99999 non-null object device_id 99999 non-null object device_ip 99999 non-null object device_model 99999 non-null object device_type 99999 non-null int64 device_conn_type 99999 non-null int64 C14 99999 non-null int64 C15 99999 non-null int64 C16 99999 non-null int64 C17 99999 non-null int64 C18 99999 non-null int64 C19 99999 non-null int64 C20 99999 non-null int64 C21 99999 non-null int64 dtypes: float64(1), int64(14), object(9) memory usage: 18.3+ MB
  • 因為是處理好的,所以數據比較完整,沒有缺失值,這為我們省去很多的工作
  • 數據中很多屬性是類別的,需要進行編碼處理
  • 數值型的數據取值都是int64,但是還是需要看看數據范圍是否一致,不然還要歸一化處理。
  • 接下來看一下數值型的數據的一個分布情況
#查看訓練集 training_set.describe() idclickhourC1banner_posdevice_typedevice_conn_typeC14C15C16C17C18C19C20C21countmeanstdmin25%50%75%max
9.999900e+0499999.00000099999.099999.00000099999.00000099999.00000099999.00000099999.00000099999.00000099999.00000099999.00000099999.00000099999.00000099999.00000099999.000000
9.500834e+180.17490214102100.01005.0344400.1983021.0557410.19927217682.106071318.33394356.8189881964.0290900.789328131.73544737874.60636688.555386
5.669435e+180.3798850.01.0887050.4026410.5839860.6352713237.72695611.93199836.924283394.9611291.223747244.07781648546.36929945.482979
3.237563e+130.00000014102100.01001.0000000.0000000.0000000.000000375.000000120.00000020.000000112.0000000.00000033.000000-1.00000013.000000
4.183306e+180.00000014102100.01005.0000000.0000001.0000000.00000015704.000000320.00000050.0000001722.0000000.00000035.000000-1.00000061.000000
1.074496e+190.00000014102100.01005.0000000.0000001.0000000.00000017654.000000320.00000050.0000001993.0000000.00000035.000000-1.00000079.000000
1.457544e+190.00000014102100.01005.0000000.0000001.0000000.00000020362.000000320.00000050.0000002306.0000002.00000039.000000100083.000000156.000000
1.844670e+191.00000014102100.01010.0000005.0000005.0000005.00000021705.000000728.000000480.0000002497.0000003.0000001835.000000100248.000000157.000000
  • 數值型數據取值范圍相差較大,后面需要對其進行歸一化處理。
# id: ad identifier # click: 0/1 for non-click/click # hour: format is YYMMDDHH, so 14091123 means 23:00 on Sept. 11, 2014 UTC. # C1 -- anonymized categorical variable # banner_pos # site_id # site_domain # site_category # app_id # app_domain # app_category # device_id # device_ip # device_model # device_type # device_conn_type # C14-C21 -- anonymized categorical variables from sklearn.externals import joblib from sklearn.cross_validation import train_test_split from sklearn.linear_model import LogisticRegression from sklearn import metricsfrom utils import load_df E:\Anaconda2\soft\lib\site-packages\sklearn\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20."This module will be removed in 0.20.", DeprecationWarning) # 結果衡量 def print_metrics(true_values, predicted_values):print "Accuracy: ", metrics.accuracy_score(true_values, predicted_values)print "AUC: ", metrics.roc_auc_score(true_values, predicted_values)print "Confusion Matrix: ", + metrics.confusion_matrix(true_values, predicted_values)print metrics.classification_report(true_values, predicted_values)# 擬合分類器 def classify(classifier_class, train_input, train_targets):classifier_object = classifier_class()classifier_object.fit(train_input, train_targets)return classifier_object# 模型存儲 def save_model(clf):joblib.dump(clf, 'classifier.pkl') train_data = load_df('train_small.csv').values train_data.shape #數據量還是99999個 (99999L, 14L) train_data[:,:] array([[ 0, 14102100, 1005, ..., 35, -1, 79],[ 0, 14102100, 1005, ..., 35, 100084, 79],[ 0, 14102100, 1005, ..., 35, 100084, 79],...,[ 0, 14102100, 1005, ..., 35, -1, 79],[ 1, 14102100, 1005, ..., 35, -1, 79],[ 0, 14102100, 1005, ..., 35, -1, 79]],dtype=int64)

先訓練一個baseline看看,說起baseline當然選用工業界認同的baseline模型LR

# 訓練和存儲模型 X_train, X_test, y_train, y_test = train_test_split(train_data[0::, 1::], train_data[0::, 0],test_size=0.3, random_state=0)classifier = classify(LogisticRegression, X_train, y_train) #使用LR模型 predictions = classifier.predict(X_test) print_metrics(y_test, predictions) #通過多種評價指標對分類的模型進行評判 save_model(classifier) #保存模型 Accuracy: 0.8233 AUC: 0.5 Confusion Matrix: [[24699 0][ 5301 0]]precision recall f1-score support0 0.82 1.00 0.90 246991 0.00 0.00 0.00 5301avg / total 0.68 0.82 0.74 30000E:\Anaconda2\soft\lib\site-packages\sklearn\metrics\classification.py:1135: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.'precision', 'predicted', average, warn_for)

從baseline的結果,我們可以得出如下幾點結論:

  • 將結果全部預測為沒有點擊后的準確率可以達到82.33%,這顯然是不對的
  • 從混淆矩陣可以看出原本為點擊的結果全部預測為了不點擊,猜想的原因可能是樣布不均衡問題導致的。因為畢竟廣告點擊的較少,數據中大部分的數據的標簽都是沒有點擊的,這會導致模型偏向于去預測不點擊
  • 從實驗結果可以發現準確率有時候非常不準,對于模型的狀態預判。
#樣本中未點擊的樣本數占總體樣本的83%多,這和我們分析的原因是一樣的,樣本非常不均衡。 training_set[training_set["click"] == 0].count()[0] * 1.0 / training_set.shape[0] 0.8250982509825098 # 按照指定的格式生成結果 def create_submission(ids, predictions, filename='submission.csv'):submissions = np.concatenate((ids.reshape(len(ids), 1), predictions.reshape(len(predictions), 1)), axis=1)df = DataFrame(submissions)df.to_csv(filename, header=['id', 'click'], index=False) import numpy as np from pandas import DataFrameclassifier = joblib.load('classifier.pkl') test_data_df = load_df('test.csv', training=False) ids = test_data_df.values[0:, 0] predictions = classifier.predict(test_data_df.values[0:, 1:]) create_submission(ids, predictions)

總結

以上是生活随笔為你收集整理的kaggle(04)---avazu_ctr_predictor(baseline)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。