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 Overflow
🌐
Reddit
reddit.com › r/learnpython › error: 'numpy.ndarray' object has no attribute 'iloc'
r/learnpython on Reddit: ERROR: 'numpy.ndarray' object has no attribute 'iloc'
April 17, 2020 -

Hi, 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.

Discussions

model_selection/iterative_stratification.py throws AttributeError: 'numpy.ndarray' object has no attribute 'iloc'
The method iterative_stratification throws the error AttributeError: 'numpy.ndarray' object has no attribute 'iloc'. In this method, X and y are used in the following function: trai... More on github.com
🌐 github.com
2
March 19, 2023
scikit learn - AttributeError: 'numpy.ndarray' object has no attribute 'columns' - Data Science Stack Exchange
import numpy as np import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.feature_selection import SelectFromModel... More on datascience.stackexchange.com
🌐 datascience.stackexchange.com
June 21, 2019
python 3.x - AttributeError: ‘numpy.ndarray’ object has no attribute ‘iloc’ - Stack Overflow
Why do the errors come up and how do I work around it? If I take away '.iloc' another error comes up. import numpy as np import pandas as pd for tr_idx, val_idx in kf.split(X_train_new, y_train_n... More on stackoverflow.com
🌐 stackoverflow.com
jupyter notebook - ERROR: 'numpy.ndarray' object has no attribute 'iloc' - Stack Overflow
I am trying to run my K-fold cross-validation and this happened from sklearn import model_selection kFold = model_selection.KFold(n_splits=5, shuffle=True) #use the split function of kfold to s... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Itsourcecode
itsourcecode.com › home › attributeerror numpy ndarray object has no attribute iloc [solved]
attributeerror numpy ndarray object has no attribute iloc [Solved]
April 4, 2023 - Since ndarray does not have the iloc attribute, you can use indexing to access elements in the array. For example, if you want to access the element at row 1 and column 2 of an array arr, you can use arr[1, 2] instead of arr.iloc[1, 2]. ... ...
🌐
GitHub
github.com › scikit-multilearn › scikit-multilearn › issues › 286
model_selection/iterative_stratification.py throws AttributeError: 'numpy.ndarray' object has no attribute 'iloc' · Issue #286 · scikit-multilearn/scikit-multilearn
March 19, 2023 - train_indexes, test_indexes are numpy arrays, not pandas dataframes. Thus, .iloc in the following code block (lines 105-106) results in the error: X_train, y_train = X.iloc[train_indexes, :], y.iloc[train_indexes, :] X_test, y_test = X.iloc[test_indexes, :], y.iloc[test_indexes, :] Removing '.iloc' from this code fixes the error. It looks like the four instances of '.iloc' should be removed from these two lines of code.
Author   gdurante2019
🌐
Kaggle
kaggle.com › questions-and-answers › 273907
Why am getting error in iloc function while trying to define x ...
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
Stack Overflow
stackoverflow.com › questions › 61264931 › error-numpy-ndarray-object-has-no-attribute-iloc
jupyter notebook - ERROR: 'numpy.ndarray' object has no attribute 'iloc' - Stack Overflow
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 lResult
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 70765390 › dbscan-python-attributeerror-numpy-ndarray-object-has-no-attribute-iloc
DBSCAN python, AttributeError: 'numpy.ndarray' object has no attribute 'iloc' - Stack Overflow
A seen below, I am trying to call out column on Daily Consumption and Month yet I have this error on iloc usage. Please help. ... It is giving me this eror: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-44-86d3fcae3613> in <module>() ----> 1 df = df.to_numpy()[: , [0,5]] AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy'
🌐
Stack Overflow
stackoverflow.com › questions › 59810808 › attributeerror-numpy-ndarray-object-has-no-attribute-iloc-while-splitting-d
python - AttributeError: 'numpy.ndarray' object has no attribute 'iloc' while splitting dataset into X and y variables - Stack Overflow
January 19, 2020 - 7 'numpy.ndarray' object has no attribute 'read' 0 pandas python: np.array_split(df, x) throws an error: 'DataFrame' object has no attribute 'size' 2 numpy split error - too many indices for array · 0 TypeError: only length-1 arrays can be converted to Python scalars in a dataset loading · 1 Dataframe -- AttributeError: 'NoneType' object has no attribute 'iloc' 21 'numpy.ndarray' object has no attribute 'index' 17 AttributeError: 'numpy.ndarray' object has no attribute 'iloc' 1 TypeError: __init__() got multiple values for argument 'n_splits' 2 TypeError: 'numpy.ndarray' object is not callable within for loop Python ·
🌐
Stack Overflow
stackoverflow.com › questions › 73575704 › numpy-ndarray-object-has-no-attribute-loc
python - Numpy.ndarray' object has no attribute 'loc' - Stack Overflow
label_encoder = LabelEncoder() x= merged_data_df.iloc[:, 1:14] y = label_encoder.fit_transform(merged_data_df['common name']) print(x.shape, y.shape)
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-fix-numpy-ndarray-object-has-no-attribute-index
How to Fix: ‘numpy.ndarray’ object has no attribute ‘index’ - GeeksforGeeks
November 28, 2021 - ‘numpy.ndarray’ object has no attribute ‘index’ is an attribute error which indicates that there is no index method or attribute available to use in Numpy array.
🌐
GitHub
github.com › pandas-dev › pandas › issues › 55604
BUG: using iloc/loc to set a nullable int type on a non-nullable int column fails · Issue #55604 · pandas-dev/pandas
October 20, 2023 - import pandas as pd import numpy as np df = pd.DataFrame.from_dict({'a': np.array([10], dtype='i8')}) # This doesn't work (raises AttributeError): df.iloc[:, 0] = df['a'].astype('Int64') # Neither does this: df.loc[:, 'a'] = df['a'].astype('Int64') # If you instead do this, it's fine: #df['a'] = df['a'].astype('Int64') assert df['a'].dtype == 'Int64' The .iloc line raises an AttributeError ('Series' object has no attribute '_hasna').
Author   batterseapower
🌐
Statology
statology.org › home › how to fix: ‘numpy.ndarray’ object has no attribute ‘index’
How to Fix: 'numpy.ndarray' object has no attribute 'index'
September 17, 2021 - This error occurs when you attempt to use the index() function on a NumPy array, which does not have an index attribute available to use.
🌐
GitHub
github.com › scikit-multilearn › scikit-multilearn › issues
Issues · scikit-multilearn/scikit-multilearn
A scikit-learn based module for multi-label et. al. classification - scikit-multilearn/scikit-multilearn
Author   scikit-multilearn
🌐
freeCodeCamp
forum.freecodecamp.org › python
Medical Data Visualizer (AttributeError: numpy.ndarray) - Python - The freeCodeCamp Forum
October 10, 2020 - I am having this error and can’t find the solution. Does anyone knows how to fix it? Thanks in advance. EE.['0.0', '0.0', '-0.0', '0.0', '-0.1', '0.5', '0.0', '0.1', '0.1', '0.3', '0.0', '0.0', '0.0', '0.0', '0.0', '0.…