私はパンダが初めてです。私は最適化したいコードを書いたが、どうしたらよいかわからない。私は、「適用」とパンダベクトル化の両方が「iterrows」より速いという事実を認識していますが、同じ目標を達成するためにそれらをどのように使用するかはわかりません。 iterrowsは 'for'ループに似ているので私にとっては簡単なので、私はそれに慣れています。あなたが見ることができるように、2ネストされた 'iterrows' ループがパンダのコードを最適化する: 'iterrows'と他のアイデアを交換する
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error
from scipy.spatial.distance import euclidean
data = pd.read_csv(r'C:\temp\train.txt')
def group_df(df,num):
ln = len(df)
rang = np.arange(ln)
splt = np.array_split(rang,num)
lst = []
finel_lst = []
for i,x in enumerate(splt):
lst.append([i for x in range(len(x))])
for k in lst:
for j in k:
finel_lst.append(j)
df['group'] = finel_lst
return df
def KNN(dafra,folds,K,fi,target):
df = group_df(dafra,folds)
avarge_e = []
for i in range(folds):
train = pd.DataFrame(df.loc[df['group'] != i])
test = pd.DataFrame(df.loc[df['group'] == i])
test.loc[:,'pred_price'] = np.nan
test.loc[:,'rmse'] = np.nan
train.loc[:,'dis'] = np.nan
train = train.reset_index()
test = test.reset_index()
for index,row in test.iterrows():
for index2,row2 in train.iterrows():
train.loc[index2]['dis'] = euclidean(row2[fi],row[fi])
: はここに私のコードです。上部には1つの小さな 'for'ループもあります。 このコードのアイデアは、テストのすべての行と列の各行との間のユークリッド距離を割り当てることです。しかし、テストは 'for'ループによって変更されるため、最終的に元のDataFrameのすべてに追加されます。ここ
データのbegginingある:
Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape \
0 1 60 RL 65.0 8450 Pave NaN Reg
1 2 20 RL 80.0 9600 Pave NaN Reg
2 3 60 RL 68.0 11250 Pave NaN IR1
LandContour Utilities ... PoolArea PoolQC Fence MiscFeature MiscVal
\
0 Lvl AllPub ... 0 NaN NaN NaN 0
1 Lvl AllPub ... 0 NaN NaN NaN 0
2 Lvl AllPub ... 0 NaN NaN NaN 0
MoSold YrSold SaleType SaleCondition SalePrice
0 2 2008 WD Normal 208500
1 5 2007 WD Normal 181500
2 9 2008 WD Normal 223500
このコードをoptimaing [3行×81列]
任意のアイデアは歓迎されるだろう。ありがとうございました。私はあなたの問題には、次の2つの可能な解決策を示唆している
はあなたが試験データを提供することができます: は、あなたが新しいデータフレームにdfを元のインデックスに渡すことができ、元のインデックスを維持するために、 ? –
私のDataFrameの最初のfue行ですか? –
はい、アーカイブする内容は –