我想执行一些速度比较测试Python vs R和挣扎与问题 - 线性回归sklearn与分类变量。如何使用LinearRegression与分类变量sklearn
代码R:
# Start the clock!
ptm <- proc.time()
ptm
test_data = read.csv("clean_hold.out.csv")
# Regression Model
model_liner = lm(test_data$HH_F ~ ., data = test_data)
# Stop the clock
new_ptm <- proc.time() - ptm
代码的Python:
import pandas as pd
import time
from sklearn.linear_model import LinearRegression
from sklearn.feature_extraction import DictVectorizer
start = time.time()
test_data = pd.read_csv("./clean_hold.out.csv")
x_train = [col for col in test_data.columns[1:] if col != 'HH_F']
y_train = ['HH_F']
model_linear = LinearRegression(normalize=False)
model_linear.fit(test_data[x_train], test_data[y_train])
,但它不适合我
return X.astype(np.float32 if X.dtype == np.int32 else np.float64) ValueError: could not convert string to float: Bee True
工作,我尝试了另一种方法
test_data = pd.read_csv("./clean_hold.out.csv").to_dict()
v = DictVectorizer(sparse=False)
X = v.fit_transform(test_data)
不过,我逮住另一个错误:
File "C:\Anaconda32\lib\site-packages\sklearn\feature_extraction\dict_vectorizer.py", line 258, in transform Xa[i, vocab[f]] = dtype(v) TypeError: float() argument must be a string or a number
我不明白的Python应该怎么解决这个问题?数据的
例子: http://screencast.com/t/hYyyu7nU9hQm
如果没有您的源数据,我们将很难调试任何东西。您也可以考虑CrossValidated是否更适合。 – TARehman
添加了非常小的数据部分。请指教 – SpanishBoy
我认为你可能从中受益http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example-在我们的例子中,数据的截图并不是超级有用的。 – TARehman