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
'numpy.ndarray' object has no attribute 'numpy'.
python - Axes from plt.subplots() is a "numpy.ndarray" object and has no attribute "plot" - Stack Overflow
Python Coding help- keep recieving error message"AttributeError: 'numpy.ndarray' object has no attribute 'MESSAGE_A'"
Medical Data Visualizer AttributeError: 'numpy.ndarray' object has no attribute
Videos
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.
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!!
The solution:
The given dataset was already an array, so I didn’t need to call .values.
The problem lies in the following line:
df = StandardScaler().fit_transform(df)
It returns a NumPy array (see the documentation), which does not have a drop function. You would have to convert it into a pd.DataFrame first!
new_df = pd.DataFrame(StandardScaler().fit_transform(df), columns=df.columns, index=df.index)
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.