Run block 1: basic random forest feature importance
s2 · run_code migrateReplace deprecated load_boston with fetch_california_housing and fix print syntax for Python 3.
@@ -1,13 +1,12 @@
-from sklearn.datasets import load_boston
+from sklearn.datasets import fetch_california_housing
from sklearn.ensemble import RandomForestRegressor
import numpy as np
-#Load boston housing dataset as an example
-boston = load_boston()
-X = boston["data"]
-Y = boston["target"]
-names = boston["feature_names"]
+# Load california housing dataset as an example
+california = fetch_california_housing(as_frame=False)
+X = california["data"]
+Y = california["target"]
+names = california["feature_names"]
rf = RandomForestRegressor()
rf.fit(X, Y)
-print "Features sorted by their score:"
-print sorted(zip(map(lambda x: round(x, 4), rf.feature_importances_), names),
- reverse=True)
+print("Features sorted by their score:")
+print(sorted(zip(map(lambda x: round(x, 4), rf.feature_importances_), names), reverse=True))
Sources
- How to fix "SyntaxError: Missing parentheses in call to ‘print’" in Python - DEV Community
The error “SyntaxError: Missing parentheses in call to ‘print’. Did you mean print(…)?” in Python happens when you use an old-style print statement (e.g., `print 'some value'`) in Python 3.
- Python SyntaxError: Missing parentheses in call to ‘print’ | Career Karma
The Python “SyntaxError: Missing parentheses in call to ‘print’” error is raised when you try to print a value to the console without enclosing that value in parenthesis.
- Fix: "SyntaxError: Missing Parentheses in Call to 'print'" in Python - GeeksforGeeks
The `SyntaxError: Missing Parentheses in Call to 'print'` is a common error when transitioning from Python 2 to Python 3. It occurs because `print` is a function in Python 3, requiring parentheses around its arguments. By understanding this change and updating your code accordingly, you can avoid this error and ensure your Python code runs smoothly in the latest version of the language.