鍍金池/ 問答/ 數(shù)據(jù)分析&挖掘問答
陌上花 回答

盡少調(diào)用 plt.scatter 方法便可大幅提升性能.

詳解
假設(shè) WX_b 為 M N 矩陣, mx 為 M 1 矩陣, 下面代碼

for i in range(WX_b.shape[0]):
    for j in range(WX_b.shape[1]):
        plt.scatter(mx[i], WX_b[i][j])

可以優(yōu)化成

plt.scatter(mx.repeat(WX_b.shape[1], axis=1), WX_b)

jupyter 示例代碼

%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

WX_b = np.random.randn(30, 5)
mx = np.random.randn(WX_b.shape[0], 1)

def func1():
    for i in range(WX_b.shape[0]):
        for j in range(WX_b.shape[1]):
            plt.scatter(mx[i], WX_b[i][j])
            
def func2():
    plt.scatter(mx.repeat(WX_b.shape[1], axis=1), WX_b)
    
%time func1()
%time func2()

參考結(jié)果: func2 運行時間大約是 func1 的 5%.

解夏 回答

為啥要用1000臺服務(wù)器,是為了IP分散么?如果是這個目的建議改用代理池

孤星 回答

你的帳號需要付款

慢半拍 回答

3D圖形好像沒有填充方法,需要先轉(zhuǎn)換成3D Polygon。下面是我根據(jù)SO上的一個回答寫的一個測試,你自己體會一下吧。
注:這個方法來自SO,原帖地址:https://stackoverflow.com/que...,不明白的可以去原貼查看。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection

size = 40 
x = [[0] * size] * size
y = list(map(sorted, np.random.rand(size, size)))
z = list(map(sorted, np.random.rand(size, size)))
vect = []
for i in range(size):
    vect.append(list(zip(x[i], y[i], z[i])))
poly3dCollection = Poly3DCollection(vect)

fig = plt.figure()
ax = Axes3D(fig)
ax.add_collection3d(poly3dCollection)
ax.set_xlim([-1, 1])
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")

plt.show()

圖片描述

別瞎鬧 回答

python 基礎(chǔ)有待加強

#df = ts.get_tick_data('601688',date='begin.strftime("%Y-%m-%d")') 
df = ts.get_tick_data('601688',date=begin.strftime("%Y-%m-%d")) 
淡墨 回答

最好不要使用for循環(huán)。

df1['group'] = df1['add'].apply(lambda x: df2.loc[[y in x for y in df2['key_word']],'group']).stack().reset_index(name='group')['group']

更新:
另一種方法,處理未收錄的情況

def match_group(x):
    for y in df2['key_word']:
        if y in x:
            return y
    return '未收錄'

df1['group'] = df1['add'].apply(match_group)
老梗 回答
我用數(shù)據(jù)庫查詢工具,db.linkbase.find({"spider_name":"test_spider"})大約耗時122秒

是122秒還是122ms?122秒的話相當慢了。
如果不確定是不是MongoDB方的問題,可以先用profiler看一下執(zhí)行的查詢到底花了多少時間。從你上面的信息來看,我比較傾向于不是數(shù)據(jù)庫這一端慢。

離觴 回答

不知道為什么,.show() 方法下載的圖片就不行。
使用以下代碼就好了

import matplotlib.pyplot as plt
plt.figure(1, facecolor = '#FF9966')
plt.savefig('test.png', facecolor=fig.get_facecolor())
懶豬 回答

需要先申請個應(yīng)用。平臺選擇你對應(yīng)的應(yīng)用平臺。你需要引導用戶登錄微博,并對你的應(yīng)用授權(quán),然后你就可以得到一個 access_token。然后使用 access_token 去訪問各種api。
移動客戶端接入

毀憶 回答

HTTP 400 錯誤 - 請求無效 (Bad request) 可能是你的cookie有問題(可能是:語法無效、字段錯誤、字符錯誤等...),主要還是發(fā)請求錯誤

赱丅呿 回答

python 3.5環(huán)境 把chromedriver的路徑改成自己的就可以運行了

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

chrome_opt = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
chrome_opt.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(executable_path="E:\selenium\chromedriver.exe", chrome_options=chrome_opt)
browser.set_window_position(-10, 0)
pageNum = 1
countNum = 1
while True:

print("\n當前頁數(shù):%d"%pageNum)
browser.get("http://app2.sfda.gov.cn/datasearchp/gzcxSearch.do?page=%d&searchcx=&optionType=V1&paramter0=null&paramter1=null&paramter2=null&formRender=cx"%pageNum)
browser.execute_script("var q=document.documentElement.scrollTop=270")
allProduct = WebDriverWait(browser, 10).until(lambda x: x.find_elements_by_xpath('/html/body/center/table[4]/tbody/tr[2]/td/center/table/tbody/tr[3]/td/table[1]/tbody/tr'))
for product in allProduct:
    if (product.get_attribute("name") != None):
        cpmc = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[1]')).text
        gcyp = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[2]/table/tbody/tr/td[2]/table/tbody/tr/td[2]/font')).text
        jkyp = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[2]/table/tbody/tr/td[4]/table/tbody/tr/td[2]/font')).text
        ypgg = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[2]/table/tbody/tr/td[6]/table/tbody/tr/td[2]/font')).text
        print("編號:%s, 產(chǎn)品名稱:%s, 國產(chǎn)藥品:%s, 進口藥品:%s, 藥品廣告:%s"%(countNum, cpmc, gcyp, jkyp, ypgg))
        countNum += 1
if (len(allProduct) != 30) : break
pageNum += 1
笨小蛋 回答

循環(huán)引用問題,已解決

心癌 回答

可能有反爬蟲手段,selenium還是有些特征的,比如全局對象中會有一些特殊屬性。

悶油瓶 回答

因為你用 Apache 部署的時候,Python 環(huán)境是全局的,你的 conda 環(huán)境并沒有配置進去

看看這個答案能否解決你的問題

https://stackoverflow.com/que...

毀憶 回答
from selenium import webdriver
import time
with open('../password.txt', 'r') as r:
    username, password = r.readline().split(',')

chrome = webdriver.Chrome()
chrome.get('https://www.itjuzi.com/user/login')
time.sleep(2)
chrome.find_element_by_xpath('//*[@id="create_account_email"]').send_keys(username)
chrome.find_element_by_xpath('//*[@id="create_account_password"]').send_keys(password)
chrome.find_element_by_xpath('//*[@id="login_btn"]').click()

最簡單的登陸方式

夢囈 回答

var nest_result=[];
function arrNestToNonNest(arr){

for(let item of arr){
    nest_result.shift({"id":item.id,"title":item.title})
    if(item.hasOwnProperty("child")){
        arrNestToNonNest(item.child)
    }
}

}
arrNestToNonNest(data)

影魅 回答
var array1 = [{ id: '1' }, { id: '2' }, { id: '3' }];
var array2 = [{ id: '1' }];
function filter (...args) {
  var map = {}
  args.forEach(arr=>{
    arr.forEach(v=>{
      map[v.id] = v
    })
  })
  return Object.entries(map).map(v=>v[1])
}

……我覺得Ls答案沒毛病啊,明明就是個匯總值,為什么要循環(huán)執(zhí)行echo?


不太明白你這里的數(shù)據(jù)結(jié)構(gòu)是怎么規(guī)劃的……這里應(yīng)該是一個二維數(shù)組結(jié)構(gòu)吧。
另外看你上邊用了number_format,這個輸出是個字符串,建議你在所有計算結(jié)束后頁面渲染前再用,因為計算機的原生浮點數(shù)在計算時會存在精度誤差(可以自己搜一下“IEEE754精度誤差”),另外類型也不對。

萌吟 回答

又不是 UA 的事。微信的環(huán)境有自己的東西,你要訪問的頁面,隨便一行 HAHAHA.XXX() 的調(diào)用,就可以判斷是不是微信環(huán)境。(因為瀏覽器是自己的, window 下有哪些 js api 都是自己控制的)