在机器学习中存在尺寸问题(Python Scikit)

时间:2014-12-25 16:48:38

标签: python machine-learning scikit-learn linear-regression dimensions

我对应用机器学习有点新意,所以我试图教自己如何使用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.shape           (59209,1,4)       earthquake_X_train.shape           (59189,1)       earthquake_y_test.shape           (3,59209)       earthquake.target.shape           (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()

1 个答案:

答案 0 :(得分:1)

您的目标数组(earthquake_y_train)形状错误。而且实际上它是空的。

当你这样做时

earthquake_y_train = earthquake.target[:-20]

您选择第一轴中的所有行但最后20行。而且,根据您发布的数据,earthquake.target的形状为(3, 59209),因此无需选择行!

但即使有,但仍然是一个错误。为什么?因为Xy的第一维必须相同。根据sklearn的文档,LinearRegression's fit期望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

P.S。即使您解决了所有这些问题,您的脚本仍然存在问题:plt.scatter无法使用“多维”ys。

相关问题