Skip to content

Instantly share code, notes, and snippets.

@InhoChoi
Last active August 26, 2015 10:29
Show Gist options
  • Save InhoChoi/6850a9dcc28f394d7f3e to your computer and use it in GitHub Desktop.
Save InhoChoi/6850a9dcc28f394d7f3e to your computer and use it in GitHub Desktop.
Smaple Random Forest
from sklearn.ensemble import RandomForestClassifier
from numpy import genfromtxt, savetxt
def main():
#create the training & test sets, skipping the header row with [1:]
dataset = genfromtxt(open('Data/train.csv','r'), delimiter=',', dtype='f8')[1:]
target = [x[0] for x in dataset]
train = [x[1:] for x in dataset]
test = genfromtxt(open('Data/test.csv','r'), delimiter=',', dtype='f8')[1:]
#create and train the random forest
#multi-core CPUs can use: rf = RandomForestClassifier(n_estimators=100, n_jobs=2)
rf = RandomForestClassifier(n_estimators=100)
rf.fit(train, target)
savetxt('Data/submission2.csv', rf.predict(test), delimiter=',', fmt='%f')
if __name__=="__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment