There's two issues here.
- By taking
.valuesyou actually access the underlyingnumpyarray; you no longer have apandas.Series.numpyarrays do not have anapplymethod. - You are trying to use
applyfor a simple multiplication, which will be orders of magnitude slower than using a vectorized approach.
See below:
import pandas as pd
import numpy as np
df = pd.DataFrame({'a': np.arange(1000, dtype=np.float64)})
print(type(df['a']))
# Gives pandas.core.series.Series
print(type(df['a'].values))
# Gives numpy.ndarray
# The vectorized approach
df['a'] = df['a'] * 1.3
Answer from roganjosh on Stack OverflowThere's two issues here.
- By taking
.valuesyou actually access the underlyingnumpyarray; you no longer have apandas.Series.numpyarrays do not have anapplymethod. - You are trying to use
applyfor a simple multiplication, which will be orders of magnitude slower than using a vectorized approach.
See below:
import pandas as pd
import numpy as np
df = pd.DataFrame({'a': np.arange(1000, dtype=np.float64)})
print(type(df['a']))
# Gives pandas.core.series.Series
print(type(df['a'].values))
# Gives numpy.ndarray
# The vectorized approach
df['a'] = df['a'] * 1.3
numpy.ndarray does not have the 'apply' attribute. https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
Error creating numpy v-stack, 'AttributeError: 'numpy.ndarray' object has no attribute 'np'
'numpy.ndarray' object has no attribute 'numpy'.
scikit learn - AttributeError: 'numpy.ndarray' object has no attribute 'fit' - Data Science Stack Exchange
python - How to fix this error :AttributeError: 'numpy.ndarray' object has no attribute 'apply' - Stack Overflow
Videos
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
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())