1. 程式人生 > >pandas常用功能與函式介紹(結合例項,持續更新)

pandas常用功能與函式介紹(結合例項,持續更新)

  1. import numpy as np
  2. import pandas as pd
  3. from Cython.Shadow import inline
  4. import matplotlib.pyplot as plt
  5. #matplotlib inline
  6. ###################1 oridinal data##################
  7. train_df = pd.read_csv('input/train.csv', index_col=0)#資料匯入
  8. test_df = pd.read_csv('input/test.csv', index_col=0)
  9. print("type of train_df:" + str(type(train_df)))
  10. #print(train_df.columns)
  11. print("shape of train_df:" + str(train_df.shape))
  12. print("shape of test_df:" + str(test_df.shape))
  13. train_df.head()#資料檢視
  14. #print(train_df.head())
  15. ###################2 smooth label######################
  16. prices = pd.DataFrame({"price":train_df["SalePrice"], "log(price+1)":np.log1p(train_df["SalePrice"])})
  17. print("shape of prices:" + str(prices.shape))#資料建立
  18. prices.hist()#直方圖
  19. # plt.plot(alphas, test_scores)
  20. # plt.title("Alpha vs CV Error")
  21. plt.show()
  22. y_train = np.log1p(train_df.pop('SalePrice'))
  23. print("shape of y_train:" + str(y_train.shape))
  24. ###################3 take train and test data together##############
  25. all_df = pd.concat((train_df, test_df), axis=0)#資料合併
  26. print("shape of all_df:" + str(all_df.shape))
  27. ###################4 make category data to string####################
  28. print(all_df['MSSubClass'].dtypes)
  29. all_df['MSSubClass'] = all_df['MSSubClass'].astype(str)#資料格式轉換
  30. all_df['MSSubClass'].value_counts()#相同數值個數統計
  31. print(all_df['MSSubClass'].value_counts())
  32. ##################5 fill null###########################
  33. all_dummy_df = pd.get_dummies(all_df)#one-hot編碼,顏色RGB,則R編碼為[1 0 0]
  34. print(all_dummy_df.head())#下一行進行資料清洗,找到為空的屬性,並按照空的數量對屬性排序
  35. print(all_dummy_df.isnull().sum().sort_values(ascending=False).head())
  36. mean_cols = all_dummy_df.mean()#資料統計,均值
  37. print(mean_cols.head(10))
  38. all_dummy_df = all_dummy_df.fillna(mean_cols)#資料清洗,用()中的值代替空值
  39. print(all_dummy_df.isnull().sum().sum())
  40. ###############6 smooth numeric cols########################
  41. numeric_cols = all_df.columns[all_df.dtypes != 'object']#選取屬性不是object,即數值型資料
  42. print(numeric_cols)
  43. numeric_col_means = all_dummy_df.loc[:, numeric_cols].mean()#按照括號的索引選取資料,並求均值
  44. numeric_col_std = all_dummy_df.loc[:, numeric_cols].std()
  45. all_dummy_df.loc[:, numeric_cols] = (all_dummy_df.loc[:, numeric_cols] - numeric_col_means) / numeric_col_std
  46. ###############7 train model#######################
  47. dummy_train_df = all_dummy_df.loc[train_df.index]
  48. dummy_test_df = all_dummy_df.loc[test_df.index]
  49. print("shape of dummy_train_df:" + str(dummy_train_df))
  50. print("shape of dummy_test_df:" + str(dummy_test_df))
  51. from sklearn.linear_model import Ridge
  52. from sklearn.model_selection import cross_val_score
  53. X_train = dummy_train_df.values
  54. X_test = dummy_test_df.values
  55. alphas = np.logspace(-3, 2, 50)
  56. test_scores = []
  57. for alpha in alphas:
  58. clf = Ridge(alpha)
  59. test_score = np.sqrt(-cross_val_score(clf, X_train, y_train, cv=10, scoring='neg_mean_squared_error'))
  60. test_scores.append(np.mean(test_score))
  61. plt.plot(alphas, test_scores)
  62. plt.title("Alpha vs CV Error")
  63. plt.show()
  64. from sklearn.ensemble import RandomForestRegressor
  65. max_features = [.1, .3, .5, .7, .9, .99]
  66. test_scores = []
  67. for max_feat in max_features:
  68. clf = RandomForestRegressor(n_estimators=200, max_features=max_feat)
  69. test_score = np.sqrt(-cross_val_score(clf, X_train, y_train, cv=5, scoring='neg_mean_squared_error'))
  70. test_scores.append(np.mean(test_score))
  71. plt.plot(max_features, test_scores)
  72. plt.title("Max Features vs CV Error")
  73. plt.show()
  74. #########################8 stacking#####################
  75. ridge = Ridge(alpha=15)
  76. rf = RandomForestRegressor(n_estimators=200, max_features=.3)
  77. ridge.fit(X_train, y_train)
  78. rf.fit(X_train, y_train)
  79. y_ridge = np.expm1(ridge.predict(X_test))
  80. y_rf = np.expm1(rf.predict(X_test))
  81. y_final = (y_ridge + y_rf)/2
  82. ######################9 submission############################
  83. submission_df = pd.DataFrame(data = {'Id':test_df.index, 'SalePrice':y_final})
  84. print(submission_df.head())

相關推薦

pandas常用功能函式介紹結合例項持續更新

import numpy as np import pandas as pd from Cython.Shadow import inline import matplotlib.pyplot as plt #matplotlib inline ###################1 or

oracle常用命令日常整理持續更新

oracle常用命令 一、Oracle資料庫例項、使用者、目錄及session會話檢視: 1、ORACLE SID檢視設定 檢視SID、使用者名稱 $ env|grep SID 、select * from v$instance、select instance_name,h

各大機器學習包彙總python版持續更新

隨著機器學習的逐日升溫,各種相關開源包也是層出不群,面對如此多種類的工具包,該如何選擇,有的甚至還知之甚少或者不知呢,本文簡單彙總了一下當下使用比較多的Python版本機器學習工具包,供大家參看,還很不全不詳盡,會持續更新,也歡迎大家補充,多謝多謝!~~~ scik

unsafe未完成持續更新

unsafe本來的英文意思就是不安全的、危險的。在java中的角色同樣也是不安全的、危險的。它是在java包中的sun.msic,

iOS開發-常用第三方開源框架介紹絕對夠你用了

影象: 1.圖片瀏覽控制元件MWPhotoBrowser        實現了一個照片瀏覽器類似 iOS 自帶的相簿應用,可顯示來自手機的圖片或者是網路圖片,可自動從網路下載圖片並進行快取。可對圖片進行縮放等操作。       下載:https:

【 MATLAB 】freqz 函式介紹數字濾波器的頻率響應

freqz Frequency response of digital filter Syntax [h,w] = freqz(b,a,n) [h,w] = freqz(d,n) [h,w] = freqz(___,n,'whole') freqz(___) [

【 MATLAB 】impz函式介紹數字濾波器的脈衝響應

這篇博文將MATLAB 幫助文件上的內容簡單的貼上,便於我寫其他博文引用,以及檢視使用。 impz Impulse response of digital filter Syntax [h,t]

起名選擇器~總結類、持續更新系列

瀏覽器 自定義 輸入 class 引入 -type 一個 important css 廢話沒有,直接幹活   一、起名方式:1.元素自身的標簽名; 2.利用class屬性自定義名稱;3.利用id屬性自定義名稱。共三種,其中class使用居多。   二、選擇器:註:選擇器使用

Python 資料結構演算法——列表連結串列linked list

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

人臉識別的簡要介紹例項、python程式碼

01 介紹 你是否意識到,每當你上傳照片到Facebook上,平臺都會用人臉識別演算法來識別圖片中的人物?目前還有一些政府在用人臉識別技術來識別和抓捕罪犯。此外,最常見的應用就是通過自己的臉部解鎖手機。 計算機視覺的子領域應用得非常廣泛,並且全球很多商業活動都已經從中獲益。人臉識別模型的使用

使用者互動系統相關7.1 7.2

參考《瘋狂java講義》 與使用者互動 實際上,大部分程式都需要處理使用者動作,包括接受使用者的鍵盤輸入,滑鼠動作等。本章未涉及圖形使用者介面(GUI)程式設計,故本節主要介紹程式如何獲得使用者的鍵盤輸入。 1. 執行Java程式的引數 回憶Java程式入口——main()方法的方法

常用的網站自己平時覺得好的持續更新

1.原始碼下載 很火的一個原始碼託管網站,裡面有很多著名的原始碼,也有很多自己寫的上傳上去的,可以直接搜尋 https://github.com點選開啟連結 2.一個老外的部落格,裡面有交叉編譯好

常用運算放大器 - 選型列表比較全引數詳細

 附加晶片查詢網址入口:https://www.alldatasheetcn.com/ 29種常用的運算放大器-2018(已經打包完畢):https://download.csdn.net/download/britripe/10831441 TI - 運放 :

刁肥宅資料結構課設“布隆過濾器的實踐應用”原始碼v1.0永不上交

       程式碼很簡單,寫了一些註釋;加上註釋看就很清楚了。        檔案bloomfilter.cpp: #include "bloomfilter.h" // return a hash range from 0 to 79999 int hash(con

java筆試題的筆記手寫更新

方法 更新 ofo illegal const blank private and prot 1、 String str=new String("abc"); 緊接著這段代碼之後的往往是這個問題,那就是這行代碼究竟創建了幾個String對象呢? 2個。 2、

自己實戰整理面試題--Mysql帶答案不斷更新

mysql目前用的版本? 5.1.21;目前最高5.7.* left join,right join,inner join? left join(左連線) 返回包括左表中的所有記錄和右表中連線欄位相等的記錄  right join(右連線) 返回包括右表中的所有記錄和左

自己實戰整理面試題--Http網路相關帶答案不斷更新

*1.描述下網頁一個 Http 請求,到後端的整個請求過程: https://blog.csdn.net/w372426096/article/details/82012229 瀏覽器輸入https:www.koolearn.com這個URL,瀏覽器只知道名字是www.koolearn.

python 各個地方導航方便查詢持續更新

老男孩python全棧開發教程,武沛齊老師的知識點!:戳這裡》》》 老男孩python全棧開發教程,linhaifeng老師的知識點!:戳這裡》》》 老男孩python全棧開發教程,Eva_J老師的知識點!:戳這裡》》》 廖雪峰官方python教程!:戳這裡》》》 老男孩python全棧開發教程,My

手搓一個兔子問題分享一個C語言問題持續更新...

大家好,我是小七夜,今天就不分享C語言的基礎知識了,分享一個比較好玩的C語言經典例題:兔子問題   題目是這樣的:說有一個窮苦人這天捉到了一隻公兔子,為了能繁衍後代他又買了一隻母兔子,後來兔子開始生小兔子(一次生一個小兔子),假設兔子不會死亡,第二十次這個窮苦人能有多少隻兔子呢?   題目解析:分析題目我

自己實戰整理面試題--多執行緒帶答案不斷更新

一個執行緒兩次呼叫 start() 方法會出現什麼情況?執行緒的生命週期,狀態是如何轉移的? Java 的執行緒是不允許啟動兩次的,第二次呼叫必然會丟擲 IllegalThreadStateException,這是一種執行時異常,多次呼叫 start 被認為是程式設計錯誤。 關於執行緒生