Created
June 8, 2021 18:35
-
-
Save petrosDemetrakopoulos/bc2fe2b29a10ae4513d0578aea71885b to your computer and use it in GitHub Desktop.
Random forest classifier for car accidents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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