前提・実現したいこと
pandasのデータフレームの操作について
dropを用いる際に、論理演算子の&を使って、一つ目の条件式と二つ目の条件式を同時に満たしているデータ(外れ値)のみ削除したい。
エラーは起きないが、明らかに必要以上にデータをdropしてしまっている。
該当のソースコード
https://qiita.com/katsu1110/items/a1c3185fec39e5629bcb
上のURLの通りにプログラムしていった。
該当プログラム
Xmat = X_train
Xmat['SalePrice'] = y_train
Xmat = Xmat.drop(Xmat[(Xmat['TotalSF']>5) & (Xmat['SalePrice']<12.5)].index)
Xmat = Xmat.drop(Xmat[(Xmat['GrLivArea']>5) & (Xmat['SalePrice']<13)].index)
y_train = Xmat['SalePrice']
X_train = Xmat.drop(['SalePrice'], axis=1)
上のプログラムでデータを必要以上にdropしてしまう。
試したこと
Xmat.shapeを調べた。
drop前は(1460,31)
一回dropすると(172,32)
二回dropすると(15,32)
明らかに外れ値以外も削除している。
補足情報(FW/ツールのバージョンなど)
windows 10
anaconda,atom 最新アップデートを使用
- 各Featureとターゲットの関係を調べる
python
1import numpy as np 2import pandas as pd 3 4import matplotlib.pyplot as plt 5import seaborn as sns 6 7train = pd.read_csv(r'C:\Users\ringo\Documents\Python\House Prices\train.csv') 8test = pd.read_csv(r'C:\Users\ringo\Documents\Python\House Prices\test.csv') 9 10print(train.dtypes) 11train.shape 12test.shape 13 14from sklearn.preprocessing import LabelEncoder 15 16for i in range(train.shape[1]): 17 if train.iloc[:,i].dtypes == object: 18 lbl = LabelEncoder() 19 lbl.fit(list(train.iloc[:,i].values) + list(test.iloc[:,i].values)) 20 train.iloc[:,i] = lbl.transform(list(train.iloc[:,i].values)) 21 test.iloc[:,i] = lbl.transform(list(test.iloc[:,i].values)) 22 23import missingno as msno 24train.head(5) 25msno.matrix(df=train, figsize=(20,14), color=(0.5,0,0)) 26 27train_ID = train['Id'] 28test_ID = test['Id'] 29 30y_train = train['SalePrice'] 31X_train = train.drop(['Id','SalePrice'], axis=1) 32X_test = test.drop('Id', axis=1) 33 34Xmat = pd.concat([X_train, X_test]) 35Xmat = Xmat.drop(['LotFrontage','MasVnrArea','GarageYrBlt'], axis=1) 36Xmat = Xmat.fillna(Xmat.median()) 37 38Xmat['TotalSF'] = Xmat['TotalBsmtSF'] + Xmat['1stFlrSF'] + Xmat['2ndFlrSF'] 39 40X_train=Xmat[0:1460] 41X_test=Xmat[1460:2920] 42 43ax = sns.distplot(y_train) 44plt.show() 45 46y_train = np.log(y_train) 47ax = sns.distplot(y_train) 48plt.show() 49 50from sklearn.ensemble import RandomForestRegressor 51rf = RandomForestRegressor(n_estimators=80, max_features='auto') 52rf.fit(X_train, y_train) 53print('Training done using Random Forest') 54 55ranking = np.argsort(-rf.feature_importances_) 56f, ax = plt.subplots(figsize=(11, 9)) 57sns.barplot(x=rf.feature_importances_[ranking], y=X_train.columns.values[ranking], orient='h') 58ax.set_xlabel("feature importance") 59plt.tight_layout() 60plt.show() 61 62X_train = X_train.iloc[:,ranking[:30]] 63X_test = X_test.iloc[:,ranking[:30]] 64 65X_train["Interaction"] = X_train["TotalSF"]*X_train["OverallQual"] 66X_test["Interaction"] = X_test["TotalSF"]*X_test["OverallQual"] 67 68fig = plt.figure(figsize=(12,7)) 69for i in np.arange(30): 70 ax = fig.add_subplot(5,6,i+1) 71 sns.regplot(x=X_train.iloc[:,i], y=y_train) 72 73plt.tight_layout() 74plt.show() 75 76Xmat = X_train 77Xmat['SalePrice'] = y_train 78Xmat = Xmat.drop(Xmat[(Xmat['TotalSF']>5) & (Xmat['SalePrice']<12.5)].index) 79Xmat = Xmat.drop(Xmat[(Xmat['GrLivArea']>5) & (Xmat['SalePrice']<13)].index) 80y_train = Xmat['SalePrice'] 81X_train = Xmat.drop(['SalePrice'], axis=1)
回答1件
あなたの回答
tips
プレビュー