As the comments suggest, .iloc is a Pandas dataframe method.
To filter a numpy array you just need: array[indices]
In your case:
x_train,x_val=train[train_indices],train[val_indices]
y_train,y_val=y[train_indices],y[val_indices]
Answer from Franco Piccolo on Stack OverflowHi, I am trying to run my K-fold cross-validation and this happened
'numpy.ndarray' object has no attribute 'iloc'
from sklearn import model_selection
kFold = model_selection.KFold(n_splits=5, shuffle=True)
#use the split function of kfold to split the housing data set
for trainIndex, testIndex in kFold.split(df):
print("Fold: ",i)
print(trainIndex.shape)
print(trainIndex)
i += 1
lRegPara = [0.01, 0.05, 0.1, 0.25, 0.5, 0.75, 1]
final_results = []
i=0
for trainIndex, testIndex in kFold.split(df):
# split the train test further
trainX, validX, trainY, validY = train_test_split(np.array(X.iloc[trainIndex]),
np.array(Y.iloc[trainIndex]),
test_size=0.20, random_state=99)
# optimise the linear regression
lResults = []
for regPara in lRegPara:
polyLassoReg = Lasso(alpha=regPara, normalize=True)
polyFitTrainX = polyreg.fit_transform(trainX)
polyLassoReg.fit(polyFitTrainX, trainY)
polyFitValidX = polyreg.fit_transform(validX)
predictKY = polyLassoReg.predict(polyFitValidX)
mse = mean_squared_error(predictKY, validY)
lResults.append(mse)
final_results.append(lResults)
plt.plot(lRegPara, lResults)Why? I have been getting this error 'numpy.ndarray' object has no attribute 'iloc'. I have search everywhere but there are no similar problem. I tried function 'loc' in numpy and the result still the same.
model_selection/iterative_stratification.py throws AttributeError: 'numpy.ndarray' object has no attribute 'iloc'
scikit learn - AttributeError: 'numpy.ndarray' object has no attribute 'columns' - Data Science Stack Exchange
python 3.x - AttributeError: ‘numpy.ndarray’ object has no attribute ‘iloc’ - Stack Overflow
jupyter notebook - ERROR: 'numpy.ndarray' object has no attribute 'iloc' - Stack Overflow
The problem is that train_test_split(X, y, ...) returns numpy arrays and not pandas dataframes. Numpy arrays have no attribute named columns
If you want to see what features SelectFromModel kept, you need to substitute X_train (which is a numpy.array) with X which is a pandas.DataFrame.
selected_feat= X.columns[(sel.get_support())]
This will return a list of the columns kept by the feature selector.
If you wanted to see how many features were kept you can just run this:
sel.get_support().sum() # by default this will count 'True' as 1 and 'False' as 0
because this :
X = df.iloc[:,:24481].values
y = df.iloc[:, -1].values
you should remove .values or make extra X_col, y_col like that
X_col = df.iloc[:,:24481]
y_col = df.iloc[:, -1]
The problem is from: traindata = traindata.drop(traindata.columns[j], axis=1, inplace=True). You can check the value of traindata right after it by adding one line of code print(traindata), you will see it returns 'None'.
you can change to:
traindata.drop(traindata.columns[j], axis=1, inplace=True)
However, you may receive new error (IndexError: single positional indexer is out-of-bounds) because you keep dropping columns.
the problem is in this line of code :
traindata = traindata.drop(traindata.columns[j], axis=1, inplace=True)
the function drop changes the type of traindata, because you're affecting its return value.
you should not affect the result because it's applied to the dataframe you should replace that line of code with:
traindata.drop(traindata.columns[j], axis=1, inplace=True)
https://imgur.com/gallery/yAdAjdx
Hello,
Linked is the screenshot of the two error messages I keep recieving as well as the code leading up to it. I'm trying to run an uplift on a classification tree. Any help is appreciated, I am still fairly new to python. Thank you!!