在机器学习(Python Scikit)中遇到维度问题



我对应用机器学习有点陌生,所以我试图自学如何使用 mldata.org 和Python scikit包中的任何数据进行线性回归。我测试了线性回归示例代码(http://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html),该代码与糖尿病数据集配合得很好。但是,我尝试将该代码与其他数据集一起使用,例如 mldata (http://mldata.org/repository/data/viewslug/global-earthquakes/) 上关于地震的数据集。但是,由于那里的尺寸问题,我无法这样做。

Warning (from warnings module):
  File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 55
    warnings.warn("Mean of empty slice.", RuntimeWarning)
RuntimeWarning: Mean of empty slice.
Warning (from warnings module):
  File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 65
    ret, rcount, out=ret, casting='unsafe', subok=False)
RuntimeWarning: invalid value encountered in true_divide
Traceback (most recent call last):
  File "/home/anthony/Documents/Programming/Python/Machine Learning/Scikit/earthquake_linear_regression.py", line 38, in <module>
    regr.fit(earthquake_X_train, earthquake_y_train)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/linear_model/base.py", line 371, in fit
    linalg.lstsq(X, y)
  File "/usr/lib/python2.7/dist-packages/scipy/linalg/basic.py", line 518, in lstsq
    raise ValueError('incompatible dimensions')
ValueError: incompatible dimensions

如何设置数据的维度?

数据大小:

earthquake_X.形状 (59209, 1, 4) earthquake_X_train.形状 (59189, 1) earthquake_y_test.形状 (3, 59209) 地震.目标.形状 (3, 59209)

代码:

# Code source: Jaques Grobler
# License: BSD 3 clause

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
#Experimenting with earthquake data
from sklearn.datasets.mldata import fetch_mldata
import tempfile
test_data_home = tempfile.mkdtemp()

# Load the diabetes dataset
earthquake = fetch_mldata('Global Earthquakes', data_home = test_data_home)

# Use only one feature
earthquake_X = earthquake.data[:, np.newaxis]
earthquake_X_temp = earthquake_X[:, :, 2]
# Split the data into training/testing sets
earthquake_X_train = earthquake_X_temp[:-20]
earthquake_X_test = earthquake_X_temp[-20:]
# Split the targets into training/testing sets
earthquake_y_train = earthquake.target[:-20]
earthquake_y_test = earthquake.target[-20:]
print "Splitting of data for preformance check completed"
# Create linear regression object
regr = linear_model.LinearRegression()
print "Created linear regression object"
# Train the model using the training sets
regr.fit(earthquake_X_train, earthquake_y_train)
print "Dataset trained"
# The coefficients
print('Coefficients: n', regr.coef_)
# The mean square error
print("Residual sum of squares: %.2f"
      % np.mean((regr.predict(earthquake_X_test) - earthquake_y_test) ** 2))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(earthquake_X_test, earthquake_y_test))
# Plot outputs
plt.scatter(earthquake_X_test, earthquake_y_test,  color='black')
plt.plot(earthquake_X_test, regr.predict(earthquake_X_test), color='blue',
         linewidth=3)
plt.xticks(())
plt.yticks(())
plt.show()

你的目标数组 ( earthquake_y_train ) 的形状不正确。而且实际上它是空的。

当你这样做时

earthquake_y_train = earthquake.target[:-20]

第一个轴中选择除最后 20 行之外的所有行。而且,根据您发布的数据,earthquake.target具有形状(3, 59209),因此没有行可供选择!

但即使有,它仍然是一个错误。为什么?因为Xy的第一个维度必须相同。 根据sklearn的文档,LinearRegression的拟合度期望X的形状[n_samples,n_features]和y - [n_samples,n_targets]。

为了修复它,将ys的定义更改为以下内容:

earthquake_y_train = earthquake.target[:, :-20].T
earthquake_y_test = earthquake.target[:, -20:].T

附言即使你解决了所有这些问题,你的脚本中仍然存在一个问题:plt.scatter不能使用"多维"ys。

相关内容

  • 没有找到相关文章

最新更新