我正在尝试用Python实现一些线性回归模型。请参阅下面的代码,我用它来进行线性回归。
import pandas
salesPandas = pandas.DataFrame.from_csv('home_data.csv')
# check the shape of the DataFrame (rows, columns)
salesPandas.shape
(21613, 20)
from sklearn.cross_validation import train_test_split
train_dataPandas, test_dataPandas = train_test_split(salesPandas, train_size=0.8, random_state=1)
from sklearn.linear_model import LinearRegression
reg_model_Pandas = LinearRegression()
print type(train_dataPandas)
print train_dataPandas.shape
<class 'pandas.core.frame.DataFrame'>
(17290, 20)
print type(train_dataPandas['price'])
print train_dataPandas['price'].shape
<class 'pandas.core.series.Series'>
(17290L,)
X = train_dataPandas
y = train_dataPandas['price']
reg_model_Pandas.fit(X, y)
在我执行完上面的python代码后,出现以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-dc363e199032> in <module>()
3 X = train_dataPandas
4 y = train_dataPandas['price']
----> 5 reg_model_Pandas.fit(X, y)
C:Users...AppDataLocalContinuumAnaconda2libsite-packagessklearnlinear_modelbase.py in fit(self, X, y, n_jobs)
374 n_jobs_ = self.n_jobs
375 X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 376 y_numeric=True, multi_output=True)
377
378 X, y, X_mean, y_mean, X_std = self._center_data(
C:Users...AppDataLocalContinuumAnaconda2libsite-packagessklearnutilsvalidation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric)
442 X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
443 ensure_2d, allow_nd, ensure_min_samples,
--> 444 ensure_min_features)
445 if multi_output:
446 y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,
C:Users...AppDataLocalContinuumAnaconda2libsite-packagessklearnutilsvalidation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features)
342 else:
343 dtype = None
--> 344 array = np.array(array, dtype=dtype, order=order, copy=copy)
345 # make sure we actually converted to numeric:
346 if dtype_numeric and array.dtype.kind == "O":
ValueError: invalid literal for float(): 20140610T000000
train_dataPandas.info()的输出
<class 'pandas.core.frame.DataFrame'>
Int64Index: 17290 entries, 4058200630 to 1762600320
Data columns (total 20 columns):
date 17290 non-null object
price 17290 non-null int64
bedrooms 17290 non-null int64
bathrooms 17290 non-null float64
sqft_living 17290 non-null int64
sqft_lot 17290 non-null int64
floors 17290 non-null float64
waterfront 17290 non-null int64
view 17290 non-null int64
condition 17290 non-null int64
grade 17290 non-null int64
sqft_above 17290 non-null int64
sqft_basement 17290 non-null int64
yr_built 17290 non-null int64
yr_renovated 17290 non-null int64
zipcode 17290 non-null int64
lat 17290 non-null float64
long 17290 non-null float64
sqft_living15 17290 non-null int64
sqft_lot15 17290 non-null int64
dtypes: float64(4), int64(15), object(1)
memory usage: 2.8+ MB
根据您的数据,另一种可能的解决方案是在从文件读取日期时指定parse_dates
,如下所示:
import pandas
salesPandas = pandas.read_csv('home_data.csv', parse_dates=['date'])
这会很有帮助的原因是,当你传递要拟合的数据时,你可以将其分解为月、小时、天。这是假设您的大部分数据都集中在前面提到的那些数据上,而不是年份(即您的总独特年份约为3-4)
在这里,您可以使用Datetime-like Properties,并通过执行salesPandas['date'].dt.month
来调用月份,然后对于天和小时,只需相应地替换它。
因此,多亏了EdChum,到目前为止的解决方案如下:
- 首先我上传了数据
- salesPandas.info()向我展示了
Int64Index: 21613 entries, 7129300520 to 1523300157 Data columns (total 20 columns): date 21613 non-null object
这不好,因为sklearn不能将日期用作对象
- 如果我销售Pandas.head(),第一个元组的日期是
20141013T000000
你看到T了吗。。。不良
-
sklearn.lineral_model.LinearRegression().fit()希望拥有npy数组(Pandas是基于numpy构建的,因此DataFrame也是numpy数组)
-
因此,首先将对象转换为日期时间,然后将其转换为数字
salesPandas[date']=pandas.to_datetime(salesPandas[date'],format='%Y%m%dT%H%m%S')
salesPandas[date']=pandas.to_numeric(salesPandas[date'])
-
如果你当时
reg_model_Pandas.fit(X,y)
它工作于