#define the target y = home_data.SalePrice #Create the list of features below feature_names = ['LotArea','YearBuilt','1stFlrSF','2ndFlrSF','FullBath','BedroomAbvGr','TotRmsAbvGrd'] # Select data corresponding to features in feature_names X = home_data[feature_names] from sklearn.model_selection import train_test_split # split data into training and validation data, for both features and target # The split is based on a random number generator. Supplying a numeric value to # the random_state argument guarantees we get the same split every time we # run this script. train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 1) from sklearn.tree import DecisionTreeRegressor #specify the model #For model reproducibility, set a numeric value for random_state when specifying the model iowa_model = DecisionTreeRegressor(random_state=1) # Fit the model iowa_model.fit(train_X, train_y) # get predicted prices on validation data val_predictions = iowa_model.predict(val_X) from sklearn.metrics import mean_absolute_error print(mean_absolute_error(val_y, val_predictions))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter