我用测井中的地质数据创建了一个DataFrame,然后创建了一个新列,根据其不同属性用名称来标记每一行。这意味着:每行现在都有一个摇滚名称。
我的问题:我已经用我拥有的所有数据训练了我的第一个DataFrame,现在我想预测一个具有与第一个相同的列(属性)的新DataFrame的标签(岩石名称)。但是我不知道该怎么做。到目前为止,这是我的代码:
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
<module> 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)
该错误表明KNN期望数组的维度小于或等于2。但是,在脚本中,您的属性(如gammaray
是numpy
数组。
编写[[gammaray, neutron, density, swat, vshale]]
时,在您的knn.predict
调用中,双括号会增加2个维度,因此最终会得到3D数组。
尝试像这样调用predict
方法:
rock_name_pred = knn.predict([gammaray, neutron, density, swat, vshale])
或者您可以直接在数据框上调用predict
方法,就像fit
方法一样:
rock_name_pred = knn.predict(well2)