我正在研究随机森林算法,并使用this post的指令构建分类器
我有以下代码段
import pandas as pd
import numpy as np
# Import train_test_split function
from sklearn.model_selection import train_test_split
data=pd.DataFrame({
'sepal length':iris.data[:,0],
'sepal width':iris.data[:,1],
'petal length':iris.data[:,2],
'petal width':iris.data[:,3],
'species':iris.target}
X=data[['sepal length', 'sepal width', 'petal length', 'petal width']] # Features
y=data['species'] # Labels
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3) # 70% training and 30% test
我正在使用从iris
导入的sklearn
数据集。但是,每当我运行此代码时,都会出现以下错误:
File "<ipython-input-11-71013f105652>", line 12
X=data[['sepal length', 'sepal width', 'petal length', 'petal width']] # Features
^
SyntaxError: invalid syntax
我认为此错误是由于我初始化了X
列表而以错误的方式充当Feature
的事实。
问题是它是代码中的错字
答案 0 :(得分:2)
似乎您缺少pd.DataFrame()的结尾“})
from sklearn import datasets
iris = datasets.load_iris()
data = pd.DataFrame({
'sepal length':iris.data[:,0],
'sepal width':iris.data[:,1],
'petal length':iris.data[:,2],
'petal width':iris.data[:,3],
'species':iris.target
})