在图表上绘制多维数据
Plotting multidimensional data on a graph
我有一个包含 1700 行的数据,每行有 9 个房屋特征和一个包含与这些特征对应的价格的数组。我已经根据这些数据建立了一个线性回归模型,但我想将其可视化。我可以使用 PCA 以某种方式将这 9 个特征转换为单个特征吗?我已经试过了,但我仍然收到错误消息,提示 x、y 需要具有相同的大小。
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from sklearn.decomposition import PCA
clf = linear_model.LinearRegression()
pca = PCA(n_components = 9)
# features contains 1700 rows of 9 feature data, y contains 1700 price values
x_train, x_test, y_train, y_test = train_test_split(features, y)
pca.fit(x_train)
x_train = pca.transform(x_train)
x_test = pca.transform(x_test)
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
plt.scatter(x_train, y_train)
plt.show()
错误发生在plt.scatter()函数,feature.shape = (17000, 9), y.shape = (17000, 1)
您得到的错误是由于 plt.scatter
的输入形状不相等造成的。 x_train
是一个17000行9列的数组。而 y_train
是一个 17000 行和一列的数组。
您可以通过索引 x_train
和仅 select 一个单独的列来修复该错误。
x_train[:,0]
select 是 x_train
的第一列。
另一种方法是将 n_components
设置为 1。n_components 确定有多少特征 pca
returns。将其设置为 1 returns 1 将其设置为 9 returns 9.
我有一个包含 1700 行的数据,每行有 9 个房屋特征和一个包含与这些特征对应的价格的数组。我已经根据这些数据建立了一个线性回归模型,但我想将其可视化。我可以使用 PCA 以某种方式将这 9 个特征转换为单个特征吗?我已经试过了,但我仍然收到错误消息,提示 x、y 需要具有相同的大小。
from sklearn.model_selection import train_test_split
from sklearn import linear_model
from sklearn.decomposition import PCA
clf = linear_model.LinearRegression()
pca = PCA(n_components = 9)
# features contains 1700 rows of 9 feature data, y contains 1700 price values
x_train, x_test, y_train, y_test = train_test_split(features, y)
pca.fit(x_train)
x_train = pca.transform(x_train)
x_test = pca.transform(x_test)
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
plt.scatter(x_train, y_train)
plt.show()
错误发生在plt.scatter()函数,feature.shape = (17000, 9), y.shape = (17000, 1)
您得到的错误是由于 plt.scatter
的输入形状不相等造成的。 x_train
是一个17000行9列的数组。而 y_train
是一个 17000 行和一列的数组。
您可以通过索引 x_train
和仅 select 一个单独的列来修复该错误。
x_train[:,0]
select 是 x_train
的第一列。
另一种方法是将 n_components
设置为 1。n_components 确定有多少特征 pca
returns。将其设置为 1 returns 1 将其设置为 9 returns 9.