我使用iloc
传递行索引并使用n
指定位置。相反,如何修改代码以从class_zero
传递行,并打印每个预测。
import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
X, y = make_classification(n_samples=1000,
n_features=6,
n_informative=3,
n_classes=2,
random_state=0,
shuffle=False)
# Creating a dataFrame
df = pd.DataFrame({'Feature 1':X[:,0],
'Feature 2':X[:,1],
'Feature 3':X[:,2],
'Feature 4':X[:,3],
'Feature 5':X[:,4],
'Feature 6':X[:,5],
'Class':y})
y_train = df['Class']
X_train = df.drop('Class', axis=1)
class_zero = df.loc[df['Class'] == 0]
n = 5 #instead of specifying 5 which is where class_zero = 0, I want to pass directly the class_zero from the list I created
#and print for each one
rf = RandomForestClassifier()
rf.fit(X_train, y_train)
instances = X_train.iloc[n].values.reshape(1, -1)
predictValue = rf.predict(instances)
actualValue = y_train.iloc[n]
print('##')
print(n)
print(predictValue)
print(actualValue)
print('##')
答案 0 :(得分:1)
您可以使用class == 0的行索引作为iloc()
像这样更改class_zero:
class_zero = df.index[df['Class'] == 0].tolist()
你正在重塑这个错误。保持这样:
instances = X_train.iloc[class_zero].values
编辑评论:
for n in class_zero:
instances = X_train.iloc[n].values.reshape(1,-1)
predictValue = rf.predict(instances)
actualValue = y_train.iloc[n]
print('##')
print(n)
print(predictValue)
print(actualValue)
print('##')