Machine Learning using python in 5 lines

MLOnce when I asked my professor about machine learning he told the following:

Lets say you have a function f(a,b) = ax+by = z . Then there are sophisticated ways to learn a and b from a data set corresponding to [x,y,z]. This is (fitting in) machine learning.

So Here I am writing for someone who wants to use machine learning quickly in python:

First, let me write the general steps:

  1. Make the data in the right shape
  2. Create the learning object
  3. Fit the data using 1 & 2
  4. Predict on a new test data

Corresponding code


from sklearn.ensemble import RandomForestClassifier 

[X,y] = extract_from_file('data_sheet.txt')#assuming you know #how to write this 

function rf_classifier_object = RandomForestClassifier(max_depth=2, random_state=0)

rf_classifier_object.fit(X,y) #The Learning step
rf_classifier_object.predict(X_test)#This will predict the new X_test data

Now you may replace ‘RF’ with any classifier or regressor as you like.  Let me know if you have any questions.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s