1

I am new to python, I would like to do a rf classification on an multispectral image which I applied the PCA.

After applying acp on different bands including NDVI I got negative values, after that my training file contains negative spectral values ​​can this be correct?

import pandas as pd 
df = pd.read_csv("training.csv")
print("Shape: ", df.shape)
print(df.columns)
print(df.head())
df_cat = pd.DataFrame ()
for i in list(df.columns):
    df_cat['{}_cat'.format(i)] = df[i].astype('category').copy()
    df_cat['{}_cat'.format(i)] = df_cat['{}_cat'.format(i)].cat.codes
    print(df_cat.head())
    
    X = df_cat.drop('class', axis = 1)
    y = df_cat['class']

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold
kf = KFold(n_splits=2, random_state = 24)
results = []

for train_index, test_index in kf.split(X):
     X_train, X_test = X[train_index], X[test_index]
     y_train, y_test = y[train_index], y[test_index]
     
     for train_index, test_index in kf.split(X):
         model = RandomForestClassifier(n_estimators = 100, random_state = 24)
     model.fit(X_train, y_train)
     y_pred = model.predict(X_test)
     results.append (f1_score(y_test, y_pred))
     print("Accuracy: ", np.mean(results))

error:

key=key, axis=self.obj._get_axis_name(axis)))

KeyError:

u"None of

here is my training sample:

| Class | ID | X | Y |  Map Y  |  Map Y   |   Lat   |   Lon   |   B1   |   B2   |  B3   |
|-------|----|---|---|---------|----------|---------|---------|--------|--------|-------|
|      1|   1|973|967|713458.25|3953393.25|35.701378|-0.640678| 99.8778|-27.7799|18.7059|
|      1|   2|974|967|713468.25|3953393.25|35.701376|-0.640568| -8.6351|-79.4471|46.5504|
|      1|   3|975|967|713478.25|3953393.25|35.701374|-0.640457| 37.6848|-59.7302|36.0935|
|      1|   4|972|967|713448.25|3953393.25|35.701380|-0.640788|153.5402| -8.8041| 8.3142|
|      1|   5|972|968|713448.25|3953383.25|35.701290|-0.640791|-28.4907|-71.9933|43.7319|
|      1|   6|973|968|713458.25|3953383.25|35.701288|-0.640681|-20.4926|-72.5833|43.9850|
|      1|   7|974|968|713468.25|3953383.25|35.701286|-0.640570| 51.7772|-52.6125|32.2387|

training sample

1
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works. – Ian Turton Oct 6 '20 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.