Only the following pseudo-classes are implemented: nth-of-type.selenium


구글 크롬 개발자도구에서 셀렉터 카피에서 나오는 child 선택자인 nth-child 를 지원하지 않는다.

tr:nth ->nth-of-type 으로 바꿔준다. 


# F12를 눌러 copy -> copy selector 를 할 경우.


# 기존

soup.select('#tblSort > tbody > tr:nth-child(1) > td:nth-child(5)')


# 변경

soup.select('#tblSort > tbody > tr:nth-of-type(1) > td:nth-of-type(5)')


NLTK 라이브러리를 이용한 문장, 단어 나누기.

  • 기존의 제공되는 split()의 경우. 단어에 .이 포함되어 토큰이 형성. NLTK의 경우 나눠줌.
  • United States와 같은 단어를 한 토큰으로 만듬.


import nltk


sent = "This is an unstructed data analysis. Prof. Geum is happly to teach this. United States"

s = nltk.tokenize.sent_tokenize(sent)

print(s)


w = nltk.tokenize.word_tokenize(sent)

print(w)


# tokenize 와 split 차이.  .이 한 토큰에 같이 들어옴.

print(sent.split())

 

# 네임드 엔티티의 경우 단어를 잘끊어줌.

# 미국의 경우 합쳐서 한 토큰으로



import numpy as np

import queue


# Interval Time , Service time 변수  

lamb=float(5) 

mu = float(3) 

Wait_time = 0


# 큐생성

queue = queue.Queue()


# 처음 작업이 들어오는 경우.

Fisrt_Arrival = np.random.exponential(lamb)

First_Service = np.random.exponential(mu) 


# 처음 작업 출력.

print("First_Arrival : ", Fisrt_Arrival)

print("First_Service : ", First_Service)

print("---------------------------------------------------")


# 변수 통일을 위해 저장.

Next_Arrival = Fisrt_Arrival

Next_Service = First_Service


count = 0

# 카운트 만큼 실행.

while count < 10:

    count+=1

    print("count : ", count)

    

    # 작업 끝나는 시간 저장.

    # 처음 작업이 진행 될때.

    if(count == 1):

        # 처음 도착 시간 + 서비스 시간

        queue.put(Fisrt_Arrival+First_Service)

    else:

        # 이전 종료시간 + 서비스 시간

        queue.put(Finish_time+Next_Service)

    

    # 기다리는 시간이 없을 경우 새로운 작업 종료시간 갱신.

    if(Wait_time == 0):

        New_Finish_time = Next_Arrival + Next_Service

    

    # 도착 시간 갱신.

    while Next_Arrival < Next_Service:

        Next_Arrival += np.random.exponential(lamb)

    

    # 작업 종료 시간 가져오기.

    Finish_time = queue.get()

    # 웨이팅이 없을 시 작업 종료 시간 변경.

    if(Wait_time == 0):

        Finish_time = New_Finish_time

        

    # wait 시간 계산.

    Wait_time = Finish_time - Next_Arrival

    print("Finsh : ", Finish_time)

    

    # 서비스 시간 갱신.

    if queue.empty():

        Next_Service = Next_Arrival + np.random.exponential(mu)

    else:

        Next_Service = Next_Service + np.random.exponential(mu)

    

    # 웨이트 시간 출력 및 시간..

    print("Next_Arrival : ", Next_Arrival)

    # 웨이트 시간 없을때의 예외처리.

    if(Wait_time < 0):

        print("Wait Time : 0")

        Wait_time = 0

    else:

        print("Wait Time : ", Wait_time)

    print("Next_Service : ", Next_Service)

    print("---------------------------------------------------")



df.to_csv(r'C:\Users\User\Desktop\chromedriver_win32/proto.csv',encoding='ms949')


Linear Regression – 𝑭-Test, T-Test





import numpy as np

from scipy import stats

from sklearn.datasets import load_boston


X=data.data

n, p= X.shape

X_data = np.concatenate((np.ones(n).reshape(-1,1), X), axis=1)

X_T_X = np.matmul(X_data.T, X_data)

beta = np.matmul(np.matmul(np.linalg.inv(X_T_X), X_data.T), y.reshape(-1, 1))

y_hat = np.matmul(X_data, beta)


SSE = sum((y.reshape(-1, 1) - y_hat)**2)

SSR = sum((y_hat - np.mean(y_hat))**2)

SST = SSR + SSE

MSR = SSR / p

MSE = SSE / (n - p - 1)

f_value = MSR / MSE

p_value = 1- stats.f.cdf(f_value, p, n - p - 1)


print("===========================================================================================")

print("Factor          SS                DF            MS                 F-value            pr>F")

print("Model", "    ",SSR, "     ", p, "     ", MSR, "     ", f_value, "   ", p_value) 

print("Error", "    ",SSE, "     ", n - p - 1, "     ", MSE)

print("===========================================================================================")

print("Total", "    ",SSE + SSR, "     ", p + n - p - 1)




import numpy as np

from scipy import stats

from sklearn.datasets import load_boston


def ttest(X,y):

    X=data.data

    name = np.append('const',data.feature_names)

    n, p= X.shape

    X_data = np.concatenate((np.ones(n).reshape(-1,1), X), axis=1)

    X_T_X = np.matmul(X_data.T, X_data)

    beta = np.matmul(np.matmul(np.linalg.inv(X_T_X), X_data.T), y.reshape(-1, 1))

    y_hat = np.matmul(X_data, beta)


    SSE = sum((y.reshape(-1, 1) - y_hat)**2)

    SSR = sum((y_hat - np.mean(y_hat))**2)

    SST = SSR + SSE

    MSR = SSR / p

    MSE = SSE / (n - p - 1)


    se_matrix = MSE*(np.linalg.inv(np.matmul(X_data.T,X_data)))

    se_matrix = np.diag(se_matrix)

    se = np.sqrt(se_matrix)


    t_value = []

    for i in range(len(se_matrix)):

        t_value.append((beta[i] / np.sqrt(se_matrix[i])))


    p_value = ((1 - stats.t.cdf(np.abs(np.array(t_value)), n - p - 1)) * 2)

    print("======================================================================================================")

    print("Variable            coef                    se                          t               Pr>|t|")

    for i in range(0,14):

        print(name[i], "         ", beta[i], "       ", se[i], "       ", t_value[i], "       ", p_value[i])

    print("======================================================================================================")



## Do not change!

# load data

data=load_boston()

X=data.data

y=data.target


ttest(X,y)



+ Recent posts