【发布时间】:2021-02-22 00:59:22
【问题描述】:
我试图按照this post 的建议解决方案在我的数据集上安装 PCA。该代码适用于形状为(150, 8) 的iris 数据,如下所示:
array([[ 1.7837721 , -1.23464679, 4.27808537, ..., 0.63061657,
-1.79849625, -1.41574397],
[-0.35396307, -0.13400175, 3.91751182, ..., -0.58928302,
-0.15735542, -0.99157312],
[-0.20380491, -1.06074392, 4.65814864, ..., 2.19686369,
0.14920164, 2.33371106],
...,
[-1.05079672, 1.46836264, 5.41970214, ..., 0.32847349,
0.27133141, 1.01266607],
[ 0.19569856, 0.57739573, 3.84749973, ..., 0.02400556,
-0.08193678, 0.51223263],
[ 0.04905765, 0.66314259, 6.22608157, ..., 0.60076934,
-0.56890579, -0.23642103]])
但是,使用我的形状为(3475, 29) 的数据发现错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-292-5661ffbde57b> in <module>
38 # data = array([randn(8) for k in range(150)])
39 data[:50, 2:4] += 5
---> 40 data[50:, 2:5] += 5
41
42 """ visualize """
TypeError: can only concatenate list (not "int") to list
我的数据(形状(3475, 29))如下所示:
array([[58.5, 27.0, 88.5, ..., nan, 0.0, -3.0],
[58.5, 27.0, 88.5, ..., nan, 0.0, -3.0],
[47.0, 45.0, 92.0, ..., 1.6, -0.649519052838329,
-1.1249999999999998],
...,
[46.0, 44.5, 98.0, ..., 2.5, 0.0, -1.3],
[46.0, 40.0, 98.0, ..., 2.5, 0.0, -1.3],
[46.5, 44.5, 76.5, ..., 17.767857142857142, -0.4788774197473401,
-1.4219984343829701]], dtype=object)
使用的代码:
# SO - doug - my data
from numpy import array, dot, mean, std, empty, argsort
from numpy.linalg import eigh, solve
from numpy.random import randn
from matplotlib.pyplot import subplots, show
def cov(X):
"""
Covariance matrix
note: specifically for mean-centered data
note: numpy's `cov` uses N-1 as normalization
"""
return dot(X.T, X) / X.shape[0]
# N = data.shape[1]
# C = empty((N, N))
# for j in range(N):
# C[j, j] = mean(data[:, j] * data[:, j])
# for k in range(j + 1, N):
# C[j, k] = C[k, j] = mean(data[:, j] * data[:, k])
# return C
def pca(data, pc_count = None):
"""
Principal component analysis using eigenvalues
note: this mean-centers and auto-scales the data (in-place)
"""
data -= mean(data, 0)
data /= std(data, 0)
C = cov(data)
E, V = eigh(C)
key = argsort(E)[::-1][:pc_count]
E, V = E[key], V[:, key]
U = dot(data, V) # used to be dot(V.T, data.T).T
return U, E, V
""" test data """
# data = array([randn(8) for k in range(150)])
data = my_data1 # Using my own data
data[:50, 2:4] += 5
data[50:, 2:5] += 5
""" visualize """
trans = pca(data, 3)[0]
fig, (ax1, ax2) = subplots(1, 2)
ax1.scatter(data[:50, 0], data[:50, 1], c = 'r')
ax1.scatter(data[50:, 0], data[50:, 1], c = 'b')
ax2.scatter(trans[:50, 0], trans[:50, 1], c = 'r')
ax2.scatter(trans[50:, 0], trans[50:, 1], c = 'b')
show()
做什么
data[:50, 2:4] += 5
data[50:, 2:5] += 5
做吗?
我尝试将这两行替换为
data = [data[:50, 2:4] += 5]
data = [data[50:, 2:5] += 5]
基于this answer,它返回
File "<ipython-input-296-5d80e1852b4e>", line 42
data = [data[:50, 2:4] += 5]
^
SyntaxError: invalid syntax
任何建议表示赞赏!
【问题讨论】:
-
“……做什么?”它将 5 添加到从 0,2 到 49,3 的每个值,以及从 50,2 到 N,4 的每个值,其中 N 是最后一列。这意味着该范围内的东西不仅仅是一个整数。如果您不知道它的作用,为什么要对您的数据这样做?
-
看到
objectdtype 了吗?您没有正确的二维数值数组。它可能包含列表,而不仅仅是数字 -
嗨@Tim,谢谢,我想先测试它是否适用于我的数据集并从那里改进。