Reputation: 15
Hi I am new to Machine Learning and working on a fun project based on Crime Prediction. The following code block is returning an error. I am using the datasets provided on the UCI ML Repo.
df_d=pd.read_csv('communities-crime-full.csv')
df
df['highCrime'] = np.where(df['ViolentCrimesPerPop']>0.1, 1, 0)
Y = df['highCrime']
# print('total len is ',len(Y))
initial=pd.read_csv('communities-crime-full.csv')
initial = initial.drop('communityname', 1)
initial = initial.drop('ViolentCrimesPerPop', 1)
initial = initial.drop('fold', 1)
initial = initial.drop('state', 1)
initial = initial.drop('community', 1)
initial = initial.drop('county', 1)
skipinitialspace = True
feature_name=list(initial)
#initial=initial.convert_objects(convert_numeric=True)
initial=initial.apply(pd.to_numeric(errors='coerce').isnull())
New_data=initial.fillna(initial.mean())
# print('before...')
# print(initial)
# print('after...')
# print(New_data)
clf = tree.DecisionTreeClassifier(max_depth=3)
# clf = tree.DecisionTreeClassifier()
clf = clf.fit(New_data, Y)
clf
fold=df['fold']
scores = cross_val_score(clf, New_data, Y,fold,'accuracy',10)
print('cross_val_accuracy is ',scores)
print('cross_val_accuracy_avg is ',np.array(scores).mean())
scores = cross_val_score(clf, New_data, Y,fold,'precision',10)
print('cross_val_precision is ',scores)
print('cross_val_precision_avg is ',np.array(scores).mean())
scores = cross_val_score(clf, New_data, Y,fold,'recall',10)
print('cross_val_recall is ',scores)
print('cross_val_recall_avg is ',np.array(scores).mean())
I receive the following error
TypeError Traceback (most recent call last)
<ipython-input-53-9f206c43d444> in <module>()
17 feature_name=list(initial)
18 #initial=initial.convert_objects(convert_numeric=True)
---> 19 initial=initial.apply(pd.to_numeric(errors='coerce').isnull())
20 New_data=initial.fillna(initial.mean())
21 # print('before...')
TypeError: to_numeric() missing 1 required positional argument: 'arg'
Upvotes: 0
Views: 1273
Reputation: 35636
Since you're trying to use pd.to_numeric
as function reference, you can't call it with kwargs this way.
Either:
initial = initial.apply(pd.to_numeric, errors='coerce')
Or Use lambda:
initial = initial.apply(lambda x: pd.to_numeric(x, errors='coerce'))
Upvotes: 1