人工智慧的比賽是很考驗實際實踐能力的,我覺得與其想系統學完理論在打比賽,不如直接在比賽中學習。
比賽是kaggle上的house prices。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import pandas_profiling as ppf
import warnings##忽略警告
warnings.filterwarnings('ignore')
%matplotlib inline # 圖直接在頁面顯示,而不是彈出乙個視窗
plt.style.use('ggplot') # 使用自帶的樣式進行美化
%matplotlib inline是jupyter notebook裡的命令, 意思是將那些用matplotlib繪製的圖顯示在頁面裡而不是彈出乙個視窗。在pycharm裡這句話會報錯。
from sklearn.base import baseestimator, transformermixin, regressormixin, clone
from sklearn.preprocessing import labelencoder#標籤編碼
from sklearn.preprocessing import robustscaler, standardscaler#去除異常值與資料標準化
from sklearn.pipeline import pipeline, make_pipeline#構建管道
from scipy.stats import skew#偏度
from sklearn.preprocessing import imputer
ppf.profilereport(train) # 用ppf包得出訓練集的報告
plt.figure(figsize=(10,8))
sns.boxplot(train.yearbuilt, train.saleprice)##箱型圖是看異常值的,離群點
# 畫乙個散點圖
plt.figure(figsize=(12,6))
plt.xlabel("grlivarea", fontsize=13)
plt.ylabel("saleprice", fontsize=13)
plt.ylim(0,800000)
# 根據圖刪除離散值
train.drop(train[(train["grlivarea"]>4000)&(train["saleprice"]<300000)].index,inplace=true)#pandas 裡面的條件索引
# 把訓練集和測試集合在一起處理,為了方便。
full = pd.concat([train,test],ignore_index=true)
# id列沒什麼用刪除了
full.drop("id",axis=1,inplace=true)
# full.info()#檢視資料的乙個資訊
miss = full.isnull().sum()#統計出空值的個數
miss[miss>0].sort_values(ascending=true)#由低到高排好序
# 對字元型別的進行填充 用none填充空值
cols1 = ["poolqc" , "miscfeature", "alley", "fence", "fireplacequ", "garagequal", "garagecond", "garagefinish", "garageyrblt", "garagetype", "bsmtexposure", "bsmtcond", "bsmtqual", "bsmtfintype2", "bsmtfintype1", "masvnrtype"]
for col in cols1:
full[col].fillna("none",inplace=true)
# 對數值型別的進行填充 用0填充
cols=["masvnrarea", "bsmtunfsf", "totalbsmtsf", "garagecars", "bsmtfinsf2", "bsmtfinsf1", "garagearea"]
for col in cols:
full[col].fillna(0, inplace=true)
# 對lotfrontage的空值進行填充(用這一列的均值)
full["lotfrontage"].fillna(np.mean(full["lotfrontage"]),inplace=true)
# 對這些列進行眾數填充
cols2 = ["mszoning", "bsmtfullbath", "bsmthalfbath", "utilities", "functional", "electrical", "kitchenqual", "saletype","exterior1st", "exterior2nd"]
for col in cols2:
full[col].fillna(full[col].mode()[0], inplace=true)
# 將一些數字特徵轉換為類別特徵。最好使用labelencoder和get_dummies來實現這些功能。
for col in cols2:
full[col]=full[col].astype(str) ##astype來進行資料轉換成字串型別
lab = labelencoder()
full["alley"] = lab.fit_transform(full.alley)
full["poolqc"] = lab.fit_transform(full.poolqc)
full["miscfeature"] = lab.fit_transform(full.miscfeature)
full["fence"] = lab.fit_transform(full.fence)
full["fireplacequ"] = lab.fit_transform(full.fireplacequ)
full["garagequal"] = lab.fit_transform(full.garagequal)
full["garagecond"] = lab.fit_transform(full.garagecond)
full["garagefinish"] = lab.fit_transform(full.garagefinish)
full["garageyrblt"] = full["garageyrblt"].astype(str)
full["garageyrblt"] = lab.fit_transform(full.garageyrblt)
full["garagetype"] = lab.fit_transform(full.garagetype)
full["bsmtexposure"] = lab.fit_transform(full.bsmtexposure)
full["bsmtcond"] = lab.fit_transform(full.bsmtcond)
full["bsmtqual"] = lab.fit_transform(full.bsmtqual)
full["bsmtfintype2"] = lab.fit_transform(full.bsmtfintype2)
full["bsmtfintype1"] = lab.fit_transform(full.bsmtfintype1)
full["masvnrtype"] = lab.fit_transform(full.masvnrtype)
full["bsmtfintype1"] = lab.fit_transform(full.bsmtfintype1)
n_train=train.shape[0]#訓練集的行數
x = pipeline_data[:n_train]#取出處理之後的訓練集
test_x = pipeline_data[n_train:]#取出n_train後的資料作為測試集
y= train.saleprice
x_scaled = standardscaler().fit(x).transform(x)#做轉換
y_log = np.log(train.saleprice)##這裡要注意的是,更符合正態分佈
#得到測試集
test_x_scaled = standardscaler().fit_transform(test_x)
from sklearn.tree import decisiontreeregressor#匯入模型
model = decisiontreeregressor()
model1 =model.fit(x_scaled,y_log)
predict = np.exp(model1.predict(test_x_scaled))##np.exp是對上面的對數變換之後的反變換
result=pd.dataframe()
result.to_csv("submission1.csv",index=false)
天池訓練營 python第一天
task1 1 運算子 整除 取餘 冪 1 2 位運算子 按位取反 按位與 有0則為0,同1則為1 按位或 有1則為1,否則為0 按位異或 相同則為0,否則為1 左移 右移 1 3 三位運算子 e x,y 4,5 small x if xin 存在 not in 不存在 is 是 not is 不是...
metasploit魔鬼訓練營第一章實踐作業
1.搜尋samba服務usermap script安全漏洞相關資訊 根據下面的連線,搜尋到如下內容 the time line is as follows may 7,2007 initial defect disclosure to the security samba.org email ali...
metsploit魔鬼訓練營第一章作業
實驗一 通過攻擊samba服務漏洞控制linux靶機 目標ip 192.168.232.134 實驗環境 kali linux kali msfconsole msf use exploit multi samba usermap script msf exploit usermap script ...