Skip to content

Instantly share code, notes, and snippets.

@amaarora
Created December 29, 2018 04:17
Show Gist options
  • Save amaarora/33c21b9b7d092bcc03f9c27958145cf5 to your computer and use it in GitHub Desktop.
Save amaarora/33c21b9b7d092bcc03f9c27958145cf5 to your computer and use it in GitHub Desktop.
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
# In[2]:
x1 = np.linspace(0,1, num=50);
x2 = np.linspace(0,1, num=50);
# In[3]:
x1
# In[4]:
x1.shape
# In[5]:
np.random.seed(42)
y = 2*x1 + 0.1*x2 + np.random.uniform(-0.1, 0.1, x1.shape)
# In[6]:
df = pd.DataFrame()
df['x1']=x1; df['x2']=x2; df['y']=y;
df.head()
# In[7]:
m = RandomForestRegressor(n_estimators=10)
# In[8]:
X,y = df.drop('y', axis=1), df['y']
# In[9]:
X.head()
# In[10]:
y.head()
# In[11]:
X_train, X_val = X[:40], X[40:]
y_train, y_val = y[:40], y[40:]
# In[12]:
m.fit(X_train, y_train)
# In[13]:
m.feature_importances_
# In[14]:
fi = pd.DataFrame(m.feature_importances_, index = X_train.columns, columns=['importance']); fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment