It seems you have no Values but values column, so need add [] because collision with values function.
Sample:
df = pd.DataFrame ({'values': [1,2,3,4,np.nan,8] })
print (df)
values
0 1.0
1 2.0
2 3.0
3 4.0
4 NaN
5 8.0
#return numpy array
print (df.values)
[[ 1.]
[ 2.]
[ 3.]
[ 4.]
[ nan]
[ 8.]]
#select column values
print (df['values'])
0 1.0
1 2.0
2 3.0
3 4.0
4 NaN
5 8.0
Name: values, dtype: float64
Your solution for me works nice, I also change .Values to ['Values'].
df1 = df.groupby('Label').filter(lambda x: x['Values'].count() > 1)
print (df1)
Label Values
0 A 1.0
1 A 2.0
3 C 4.0
4 C NaN
5 C 8.0
Alternative solution with transform and boolean indexing:
print (df.groupby('Label')['Values'].transform('count'))
0 2.0
1 2.0
2 1.0
3 2.0
4 2.0
5 2.0
Name: Values, dtype: float64
print (df.groupby('Label')['Values'].transform('count') > 1)
0 True
1 True
2 False
3 True
4 True
5 True
Name: Values, dtype: bool
print (df[df.groupby('Label')['Values'].transform('count') > 1])
Label Values
0 A 1.0
1 A 2.0
3 C 4.0
4 C NaN
5 C 8.0
Also check What is the difference between size and count in pandas?
Answer from jezrael on Stack Overflowscikit learn - AttributeError: 'numpy.ndarray' object has no attribute 'columns' - Data Science Stack Exchange
python - How do I count the occurrence of a certain item in an ndarray? - Stack Overflow
pandas - 'numpy.ndarray' object has no attribute 'plot' - Data Science Stack Exchange
AttributeError: 'numpy.ndarray' object has no attribute 'pop'
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]
Write a function to calculate accumulated GPA (omit grade <5).
def gpa_of_pass(marks, credits):
gpa_list=[]
for i in range(len(marks)):
for j in range(len(marks[i])):
if marks[i][j] < 5:
marks=marks[i].pop(j)
for i in marks:
for j in i:
gpa_list.append(np.dot(i,credits)/sum(credits))
return gpa_list
marks = np.array([
[8.0, 9.0, 10.0],
[4.0, 9.0, 8.0],
[8.0, 3.0, 8.0],
[10.0, 9.0, 5.0],
[9.0, 9.0, 4.0]
])
credits = np.array([2, 2, 1])
gpa_of_pass(marks, credits)I ran a for loop to remove the grade <5 before calculating the GPA but I got the error in the title. Would you please have any suggestions? Thank you so much!
Well, according to the documentation, ndarray simply has no count method.
The code you have posted contradicts the error-message you give us. In your code you create a simple Python list, but your error message indicates that you are actually using a numpy ndarray.
First off all, your array is formatted weirdly, there should be commas between the numbers. Second, you are creating a numpy.ndarray from the numpy package, not a native python list. Use a python list, and it should work.
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!!
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