У меня есть pandas.DataFrame
с именем fake_num
:
fake_num=pd.DataFrame([[1,2,3,4,np.nan,np.nan,np.nan],[1.1,1.2,1.3,1.4,1.6,1.8,2.5]]).T
fake_num
Out[4]:
0 1
0 1.0 1.1
1 2.0 1.2
2 3.0 1.3
3 4.0 1.4
4 NaN 1.6
5 NaN 1.8
6 NaN 2.5
Я пытаюсь заполнить NaN
значения, используя линейную регрессию :
from sklearn.linear_model import LinearRegression
fdrop=fake_num.dropna(axis=0,how='any')
lr=LinearRegression()
lr.fit(np.array(fdrop.iloc[:,1]).reshape(-1, 1),np.array(fdrop.iloc[:,0]))
lr.predict(np.array(fake_num[np.isnan(fake_num[0])][1]).reshape(-1, 1))
Out[5]: array([ 6., 8., 15.])
Часть, которую я хочу заменить, - fake_num[np.isnan(fake_num[0])][0]
, поэтому я хочу:
Out[6]:
0 1
0 1.0 1.1
1 2.0 1.2
2 3.0 1.3
3 4.0 1.4
4 6.0 1.6
5 8.0 1.8
6 5.0 2.5
Пока я пытался:
fake_num[np.isnan(fake_num[0])][0]=lr.predict(np.array(fake_num[np.isnan(fake_num.iloc[:,0])].iloc[:,1]).reshape(-1, 1))
fake_num
__main__:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
Out[11]:
0 1
0 1.0 1.1
1 2.0 1.2
2 3.0 1.3
3 4.0 1.4
4 NaN 1.6
5 NaN 1.8
6 NaN 2.5
И
fake_num[np.isnan(fake_num.loc[:,0])].loc[:,0]=lr.predict(np.array(fake_num[np.isnan(fake_num.iloc[:,0])].iloc[:,1]).reshape(-1, 1))
fake_num
D:\Users\shan xu\Anaconda3\lib\site-packages\pandas\core\indexing.py:630: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item_labels[indexer[info_axis]]] = value
Out[12]:
0 1
0 1.0 1.1
1 2.0 1.2
2 3.0 1.3
3 4.0 1.4
4 NaN 1.6
5 NaN 1.8
И
fake_num[np.isnan(fake_num.iloc[:,0])].iloc[:,0]=lr.predict(np.array(fake_num[np.isnan(fake_num.iloc[:,0])].iloc[:,1]).reshape(-1, 1))
fake_num
D:\Users\shan xu\Anaconda3\lib\site-packages\pandas\core\indexing.py:630: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self.obj[item_labels[indexer[info_axis]]] = value
Out[12]:
0 1
0 1.0 1.1
1 2.0 1.2
2 3.0 1.3
3 4.0 1.4
4 NaN 1.6
5 NaN 1.8
что я должен сделать, чтобы заменить часть информационного кадра некоторыми значениями, чтобы определить его положение. Кстати, так как мне нужно больше деталей, какие-либо хорошие инструменты, принять значения заполнения с простыми моделями предсказаний, используя в качестве входных данных все другие не-строки и другие столбцы? Что-то вроде missforest в R.