小编ekt*_*kta的帖子

从mysql中的另一列更新一个派生值的列

我试图从同一个tabl中的另一列更新列的值e-但这失败了"ERROR 1093(HY000):你不能指定目标表'tab_1'用​​于FROM子句中的更新"

我在Mysql中有什么

DT;                  date_custom
2012-10-31 17:00:22; 0
2012-09-31 17:00:21; 0
2012-07-31 17:00:25; 0
2012-10-31 17:43:56; 0
2012-11-31 17:44:09; 0
Run Code Online (Sandbox Code Playgroud)

我在相应的date_custom字段(列)中需要什么

2012-10-31 
2012-09-31 
2012-07-31 
2012-10-31 
2012-11-31 
Run Code Online (Sandbox Code Playgroud)

换句话说,我只是希望Mysql为列DT选取相应的行,而只需要DUMP派生值date_column.这应该是一对一的.我确实有一个唯一标识行的键组合,但如果我能识别出来,我不想使用它.

这是我尝试过但没有用的东西.

Before this I created this column - date_custom as below -:
alter table tab_1
add column date_custom int not null;

# simplistic
UPDATE tab_1 SET date_custom = (SELECT SUBSTRING_INDEX(DT," " ,1) FROM tab_1);
Run Code Online (Sandbox Code Playgroud)

我也意识到我不能同时修改一个列,同时尝试访问它 - 但由于这是不同的列,所以事情不应该在这里失败,对 - 或者我做错了什么?

# using self joins on subquery
UPDATE tab_1
SET tab_1.date_custom =
(
    SELECT SUBSTRING_INDEX(a.DT," …
Run Code Online (Sandbox Code Playgroud)

mysql sql select

11
推荐指数
2
解决办法
1万
查看次数

在Python中建模时检测多线性或具有线性组合的列:LinAlgError

我正在为具有34个因变量的logit模型建模数据,并且它继续抛出奇异矩阵误差,如下所示:

Traceback (most recent call last):
  File "<pyshell#1116>", line 1, in <module>
    test_scores  = smf.Logit(m['event'], train_cols,missing='drop').fit()
  File "/usr/local/lib/python2.7/site-packages/statsmodels-0.5.0-py2.7-linux-i686.egg/statsmodels/discrete/discrete_model.py", line 1186, in fit
    disp=disp, callback=callback, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/statsmodels-0.5.0-py2.7-linux-i686.egg/statsmodels/discrete/discrete_model.py", line 164, in fit
    disp=disp, callback=callback, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/statsmodels-0.5.0-py2.7-linux-i686.egg/statsmodels/base/model.py", line 357, in fit
    hess=hess)
  File "/usr/local/lib/python2.7/site-packages/statsmodels-0.5.0-py2.7-linux-i686.egg/statsmodels/base/model.py", line 405, in _fit_mle_newton
    newparams = oldparams - np.dot(np.linalg.inv(H),
  File "/usr/local/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 445, in inv
    return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
  File "/usr/local/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 328, in solve
    raise LinAlgError, 'Singular matrix'
LinAlgError: Singular matrix
Run Code Online (Sandbox Code Playgroud)

当我对这种方法进行简化以将矩阵减少到其独立列时就是这样

def independent_columns(A, tol = …
Run Code Online (Sandbox Code Playgroud)

numpy singular python-2.7 statsmodels logistic-regression

8
推荐指数
1
解决办法
4324
查看次数

关闭 Selenium 中的通用弹出窗口

使用 selenium webdriver 抓取页面时,会出现一个“弹出窗口”。

在打开页面时,http://www.fanatics.com/search/red%20shoes - 我看到一个带有 xpath '//*[@id="mm_DesktopInterstitialImage"]' 的弹出窗口 - 但我不想成为使用 xpath 关闭此警报,并具有可以解除/关闭警报的通用内容。这是我到目前为止尝试过的-:

from selenium import webdriver
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

chromedriver = "/usr/local/CHROMEDRIVER"
desired_capabilities=DesiredCapabilities.CHROME
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver,desired_capabilities=desired_capabilities)
url='http://www.fanatics.com/search/red%20shoes'
driver.get(url)
#driver.set_page_load_timeout(300)
driver.implicitly_wait(60)
alert = driver.switch_to_alert()
alert.dismiss
handle=driver.window_handles
print handle
#[u'CDwindow-B1E9C127-719D-ACAA-19DE-1A6FA265A4FF']
Run Code Online (Sandbox Code Playgroud)

从我从相关示例中了解到,人们通常切换窗口句柄,但我的句柄只有一个元素。即,driver.switch_to_window(driver.window_handles[1])然后driver.close()最后再次切换,使用driver.switch_to_window(driver.window_handles[1]) 我还使用了隐式等待,因为我不确定是否正在读取警报窗口完全 - 但这也不起作用。如果可能的话,我不想对 xpath 进行硬编码。

我究竟做错了什么 ?

相关,但对我不起作用:Selenium python 如何关闭弹出窗口?

python selenium webdriver popupwindow selenium-webdriver

4
推荐指数
2
解决办法
2万
查看次数

将列表/数组作为参数/返回类型传递和返回到 Redshift 中的 UDF

我有一堆指标,它们消耗列的整个浮点值列表(想想我在其上进行一些异常值分析的一系列订单值,因此需要整个值数组)。

我可以将整个列表作为参数传递吗?如果我完全用 python 来做这件事,那就需要太多的数据处理了。想法?

# Redshift UDF - the red part is invalid signature & needs a fill 
create function Median_absolute_deviation(y <Pass a list, but how? >,threshold float) 

--INPUTS: 
--a list of order values, -- a threshold
 RETURNS <return a list, but how? > 

STABLE 
AS $
    import numpy as np

    m = np.median(y)
    abs_dev = np.abs(y - m)
    left_mad = np.median(abs_dev[y<=m])
    right_mad = np.median(abs_dev[y>=m])
    y_mad = np.zeros(len(y))
    y_mad[y < m] = left_mad
    y_mad[y > m] = right_mad
    modified_z_score = …
Run Code Online (Sandbox Code Playgroud)

user-defined-functions amazon-redshift udf data-munging

4
推荐指数
1
解决办法
3850
查看次数

使用patsy.dmatrices在patsy中的交互效果为":"提供重复的列,如"+"或"*"

我有一个包含列的数据框,我打算将它们视为分类变量.

第一列是国家,其中包含SGP,AUS,MYS等值.第二列是时间,其中包含24小时格式的值,如00,11,14,15等.事件是二进制变量,有1/0标志.我理解为了对它们进行分类,我需要在运行Logistic回归之前使用patsy.这个,我使用dmatrices构建.

用例:考虑country&time_day的交互效果(以及其他属性说"操作系统")

f= 'event_int ~ time_day:country'
y,X = patsy.dmatrices(f, df, return_type='dataframe')
X.columns
Index([u'Intercept', u'country[T.HKG]', u'country[T.IDN]', u'country[T.IND]', u'country[T.MYS]', u'country[T.NZL]', u'country[T.PHL]', u'country[T.SGP]', u'time_day[T.02]:country[AUS]', u'time_day[T.03]:country[AUS]', u'time_day[T.04]:country[AUS]', u'time_day[T.05]:country[AUS]', u'time_day[T.06]:country[AUS]', u'time_day[T.07]:country[AUS]', u'time_day[T.08]:country[AUS]', u'time_day[T.09]:country[AUS]', u'time_day[T.10]:country[AUS]', u'time_day[T.11]:country[AUS]', u'time_day[T.12]:country[AUS]', u'time_day[T.NA]:country[AUS]', u'time_day[T.02]:country[HKG]', u'time_day[T.03]:country[HKG]', u'time_day[T.04]:country[HKG]', u'time_day[T.05]:country[HKG]', u'time_day[T.06]:country[HKG]', u'time_day[T.07]:country[HKG]', u'time_day[T.08]:country[HKG]', u'time_day[T.09]:country[HKG]', u'time_day[T.10]:country[HKG]', u'time_day[T.11]:country[HKG]', u'time_day[T.12]:country[HKG]', u'time_day[T.NA]:country[HKG]', u'time_day[T.02]:country[IDN]', u'time_day[T.03]:country[IDN]', u'time_day[T.04]:country[IDN]', u'time_day[T.05]:country[IDN]', u'time_day[T.06]:country[IDN]', u'time_day[T.07]:country[IDN]', u'time_day[T.08]:country[IDN]', u'time_day[T.09]:country[IDN]', u'time_day[T.10]:country[IDN]', u'time_day[T.11]:country[IDN]', u'time_day[T.12]:country[IDN]', u'time_day[T.NA]:country[IDN]', u'time_day[T.02]:country[IND]', u'time_day[T.03]:country[IND]', u'time_day[T.04]:country[IND]', u'time_day[T.05]:country[IND]', u'time_day[T.06]:country[IND]', u'time_day[T.07]:country[IND]', u'time_day[T.08]:country[IND]', u'time_day[T.09]:country[IND]', u'time_day[T.10]:country[IND]', u'time_day[T.11]:country[IND]', u'time_day[T.12]:country[IND]', u'time_day[T.NA]:country[IND]', u'time_day[T.02]:country[MYS]', u'time_day[T.03]:country[MYS]', u'time_day[T.04]:country[MYS]', u'time_day[T.05]:country[MYS]', u'time_day[T.06]:country[MYS]', u'time_day[T.07]:country[MYS]', u'time_day[T.08]:country[MYS]', u'time_day[T.09]:country[MYS]', u'time_day[T.10]:country[MYS]', …
Run Code Online (Sandbox Code Playgroud)

python-2.7 statsmodels logistic-regression

3
推荐指数
1
解决办法
3600
查看次数

在 patsy 中从 DesignMatrix 获取名称

from patsy import *
from pandas import *
dta =  DataFrame([["lo", 1],["hi", 2.4],["lo", 1.2],["lo", 1.4],["very_high",1.8]], columns=["carbs", "score"])
dmatrix("carbs + score", dta)
DesignMatrix with shape (5, 4)
Intercept  carbs[T.lo]  carbs[T.very_high]  score
        1            1                   0    1.0
        1            0                   0    2.4
        1            1                   0    1.2
        1            1                   0    1.4
        1            0                   1    1.8
Terms:
'Intercept' (column 0), 'carbs' (columns 1:3), 'score' (column 3)
Run Code Online (Sandbox Code Playgroud)

问题:我是否可以不读取此 DesignMatrix 给出的名称,而不是使用 Designinfo 指定列的“名称”(这基本上使我的代码的可重用性降低),以便稍后将其输入到 DataFrame 中,而无需知道预先确定“参考水平/对照组”水平是多少?

IE。当我做 dmatrix("C(carbs, Treatment(reference='lo')) + Score", dta)

"""
# How can I get …
Run Code Online (Sandbox Code Playgroud)

python python-2.7 pandas statsmodels patsy

2
推荐指数
1
解决办法
1801
查看次数

在打印分组的实例计数后,在python数据框中展平组的结果

我需要按值计算数据框中两列的实例.我通过使用组和大小获得相同,但我想吐出1.每个列组合中的平坦值2."最后计数"列的名称(另请参阅下面的我想要的内容).

    df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD'), [1.1, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3,4.5,4.6,4.7,4.7,4.8], ['x/y/z','x/y','x/y/z/n','x/u','x','x/u/v','x/y/z','x','x/u/v/b','-','x/y','x/y/z','x','x/u/v/w'],['1','3','3','2','4','2','5','3','6','3','5','1','1','1']]).T
    df.columns = ['col1','col2','col3','col4','col5']
    df.groupby(['col5', 'col2']).size()
# this gives 
col5  col2   <Note that this is unnamed>
1     A       1
      D       3
2     B       2
3     A       3
      C       1
4     B       1
5     B       2
6     B       1
dtype: int64

What I want -:
    col5  col2   count_instances_of_this_combination
    1     A       1
    1     D       3
    2     B       2
    3     A       3
    3     C       1
    4     B …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

2
推荐指数
1
解决办法
2177
查看次数

feature_importances_ 在 ExtraTreesClassifier 中显示为 NoneType :TypeError: 'NoneType' 对象不可迭代

我正在尝试为给定的数据集选择重要的特征(或至少了解哪些特征解释了更多的可变性)。为此,我同时使用了 ExtraTreesClassifier 和 GradientBoostingRegressor - 然后使用:-

clf = ExtraTreesClassifier(n_estimators=10,max_features='auto',random_state=0) # stops after 10 estimation passes, right ?
clf.fit(x_train, y_train)
feature_importance=clf.feature_importances_  # does NOT work - returns NoneType for feature_importance
Run Code Online (Sandbox Code Playgroud)

发布这个我真的很想绘制它们(用于视觉表示) - 甚至是初步的,只是查看重要性的相对顺序和相应的索引

# Both of these do not work as the feature_importance is of NoneType
feature_importance = 100.0 * (feature_importance / feature_importance.max())
indices = numpy.argsort(feature_importance)[::-1]
Run Code Online (Sandbox Code Playgroud)

我发现令人困惑的是 - 如果我要使用如下所示的 GradientBoostingRegressor,我确实得到了 feature_importance 及其索引。我究竟做错了什么 ?

#Works with GradientBoostingRegressor
params = {'n_estimators': 100, 'max_depth': 3, 'learning_rate': 0.1, 'loss': 'lad'}
clf = …
Run Code Online (Sandbox Code Playgroud)

machine-learning python-2.7 nonetype scikit-learn

1
推荐指数
1
解决办法
894
查看次数