scikit learn - AttributeError: 'numpy.ndarray' object has no attribute 'fit' - Data Science Stack Exchange
Medical Data Visualizer AttributeError: 'numpy.ndarray' object has no attribute
Error creating numpy v-stack, 'AttributeError: 'numpy.ndarray' object has no attribute 'np'
Python Coding help- keep recieving error message"AttributeError: 'numpy.ndarray' object has no attribute 'MESSAGE_A'"
Videos
It would be helpful if you could post the full stack trace, so that we can see which line your error occurs at. In general, the more information you can provide in a question, the better.
In this case, it looks like your full_model_pipeline may somehow become a numpy array. Since you have a one-element pipeline, you could try changing
full_model_pipeline = Pipeline(steps =[
('full_pipeline',full_pipeline),
('model',LinearRegression())
])
full_model_pipeline.fit(X_train,y_train)
to
model = LinearRegression()
model.fit(X_train, y_train)
I believe you need to add () where you add scaler to the pipeline: ('std_scaler',StandardScaler) --> ('std_scaler',StandardScaler())
Hi,
I'm trying to create a numpy v-stack and creating 3 np.array's for it, by filling them with a loop:
I get the error: 'AttributeError: 'numpy.ndarray' object has no attribute 'np' . I think I'm using the wrong notation to append to the empty arrays:
neighbor_id = [id_ for id_ in range(1, n_obs) if id_ != user_id]
neighbor_id_arr = np.array(neighbor_id)
similarity = np.array([])
num_interactions = np.array([])
# get similarity and num_interactions
for id_ in neighbor_id:
similarity.np.append(np.dot(user_item.loc[user_id],user_item.loc[id_])) #The issue is here, I think
num_interactions.np.append(user_interactions.loc[id_])
c = numpy.vstack((neighbor_id_arr, similarity,num_interactions))
Thanks!
James
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!!
If you debug your program by simply printing ax, you'll quickly find out that ax is a two-dimensional array: one dimension for the rows, one for the columns.
Thus, you need two indices to index ax to retrieve the actual AxesSubplot instance, like:
ax[1,1].plot(...)
If you want to iterate through the subplots in the way you do it now, by flattening ax first:
ax = ax.flatten()
and now ax is a one dimensional array. I don't know if rows or columns are stepped through first, but if it's the wrong around, use the transpose:
ax = ax.T.flatten()
Of course, by now it makes more sense to simply create each subplot on the fly, because that already has an index, and the other two numbers are fixed:
for x < plots_tot:
ax = plt.subplot(nrows, ncols, x+1)
Note: you have x <= plots_tot, but with x starting at 0, you'll get an IndexError next with your current code (after flattening your array). Matplotlib is (unfortunately) 1-indexed for subplots. I prefer using a 0-indexed variable (Python style), and just add +1 for the subplot index (like above).
The problem here is with how matplotlib handles subplots. Just do the following:
fig, axes = plt.subplots(nrows=1, ncols=2)
for axis in axes:
print(type(axis))
you will get a matplotlib object which is actually a 1D array which can be traversed using single index i.e. axis[0], axis[1]...and so on. But if you do
fig, axes = plt.subplots(nrows=2, ncols=2)
for axis in axes:
print(type(axis))
you will get a numpy ndarray object which is actually a 2D array which can be traversed only using 2 indices i.e. axis[0, 0], axis[1, 0]...and so on. So be mindful how you incorporate your for loop to traverse through axes object.
The problem is not with Pandas. This is due to NumPy. I had a similar issue and this is what I did.
Ran python -c "import numpy as np; print(np.__file__); print(np.ndarray)". The expect output will contain the location of your installed NumPy package and <class 'numpy.ndarray'> for the second print statement. In my case, it seemed that NumPy package was located in ~/.local.
I tried reinstalling NumPy in conda, but the location was still the same. So, as per this solution, I deleted ~/.local. Then the error disappeared.
For me none of the above solutions fixed the issue. The only way it worked was I cleaned up all my python environments.
- Removed manually installed python versions at various locations such as usr/local/bin
- cleaned up PATH
- Removed all but python 3.11 from brew.
- Ran brew cleanup and resolved all warnings/issues.
- Re-installed command line tools since I had been using the system installed python version for various projects.
- Created a fresh virtual environment using brew's python3.11 as the base interpreter. Post this both numpy and pandas were not present and were installed fresh. No issues anymore.
if you need to flatten you can directly use flatten() on numpy.ndarray object
X = X.flatten()
Link to doc: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.flatten.html
DO NOT VOTE UP RESPONSE MAIN WORK DONE BY @wayne above:
Did you run print(type(X)) on X prior to that line that gave an error? Given you got an error of AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy', X appears to already be numpy object. Also, I don't think the to_numpy comes from numpy. Pandas has one. Polars has one. This drops us in the end of the problem and so we cannot provide much help without more code upstream.
solution
reload data set.
df = pd.read_csv(file_name, header=0)
[str(i) for i in ([["CPU_frequency"]])]
code above resolves.