Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save petrosDemetrakopoulos/bc2fe2b29a10ae4513d0578aea71885b to your computer and use it in GitHub Desktop.
Save petrosDemetrakopoulos/bc2fe2b29a10ae4513d0578aea71885b to your computer and use it in GitHub Desktop.
Random forest classifier for car accidents
def train_random_forest():
le = preprocessing.LabelEncoder()
le.fit(dataset['Cause'])
dataset['Cause'] = le.transform(dataset['Cause'])
le.fit(dataset['Severity'])
dataset['Severity'] = le.transform(dataset['Severity'])
print(le.classes_)
X = dataset[['Cause', 'Weekday', 'Year', 'Month', 'Point of accident']]
y = dataset['Severity']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment