python knn预测_使用knn预测另一个数据帧(python 3.6)中的值[关闭]

我创建了一个数据框架,其中包含来自测井的地质数据,然后我创建了一个新的列,根据每一行的不同属性为其添加一个名称。这意味着:每一行现在都有一个摇滚名字。

我的问题是:我已经用我拥有的所有数据训练了我的第一个数据帧,现在我想预测一个新数据帧的标签(岩石名称),它与第一个数据帧具有相同的列(属性)。但我不知道怎么做。这是我至今的密码:

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.neighbors import KNeighborsClassifier

data = pd.read_excel('wellA.xlsx') #size (20956,26)

well1 = pd.concat([data['GR'], data['NPHI'], data['RHOB'], data['SW'],

data['VSH'], data['rock_name']], axis=1, keys =

['GR','NPHI','RHOB','SW','VSH','rock_name'])

well1 = well1.drop(well1.index[0:15167])

well1.dropna(axis=0, inplace=True)

knn = KNeighborsClassifier(n_neighbors = 9)

d = {'Claystone': 1, 'Calcareous Claystone': 2, 'Sandy Claystone': 3,

'Limestone': 4, 'Muddy Limestone': 5, 'Muddy Sandstone': 6, 'Sandstone': 7}

well1['Label'] = well1['rock_name'].map(d) #size (5412,7)

X = well1[well1.columns[:5]] #size (5412, 5)

y = well1.rock_name #size (5412,)

X_train, X_test, y_train, y_test = train_test_split (X, y, random_state = 0)

#sizes: X_train(4059,5), X_test(1353,5) , y_train(4059,), y_test(1353,)

knn.fit(X_train, y_train)

knn.score(X_test, y_test)

data2 = pd.read_excel('wellB.xlsx') #size (29070, 12)

well2 = pd.concat([data2['GR'], data2['NPHI'], data2['RHOB'], data2['SW'],

data2['VSH']], axis=1, keys = ['GR','NPHI','RHOB','SW','VSH'])

well2.dropna(axis=0, inplace=True) #size (2124, 5)

# values of the properties

gammaray = well2['GR'].values

neutron = well2['NPHI'].values

density = well2['RHOB'].values

swat = well2['SW'].values

vshale = well2['VSH'].values

rock_name_pred = knn.predict([[gammaray, neutron, density, swat, vshale]])

然后我有以下错误:

回溯(最近一次呼叫的最后一次):

File "C:\Users\laguiar\AppData\Local\Continuum\anaconda3\lib\site-

packages\spyder\utils\site\sitecustomize.py", line 705, in runfile

execfile(filename, namespace)

File "C:\Users\laguiar\AppData\Local\Continuum\anaconda3\lib\site-

packages\spyder\utils\site\sitecustomize.py", line 102, in execfile

exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/laguiar/Desktop/Projeto Norne/exemploKNN.py", line 41, in

rock_name_pred = knn.predict([[gammaray, neutron, density, swat,

vshale]])

File "C:\Users\laguiar\AppData\Local\Continuum\anaconda3\lib\site-

packages\sklearn\neighbors\classification.py", line 143, in predict

X = check_array(X, accept_sparse='csr')

File "C:\Users\laguiar\AppData\Local\Continuum\anaconda3\lib\site-

packages\sklearn\utils\validation.py", line 451, in check_array

% (array.ndim, estimator_name))

ValueError: Found array with dim 3. Estimator expected <= 2.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值