Python 2023. 4. 20. 08:31
import pandas as pd
pd.options.display.float_format = '{:.5f}'.format

'Python' 카테고리의 다른 글

[python] string contains  (0) 2023.02.16
[python] list value rank  (0) 2022.09.08
[python] 포아송 분포  (0) 2022.08.24
pandas dataframe show max columns  (0) 2022.08.18
dataframe split date  (0) 2022.06.24
posted by 초코렛과자
:
Python 2023. 2. 16. 07:41

contains 사용시 __contains__ 로

str_list = ['abc','ggg','eft','abs','asd']

for v in str_list:
	if v.__contains__('a'):
    	print(v)

 

'Python' 카테고리의 다른 글

[python] jupyter notebook 지수표현(e) 없애기  (0) 2023.04.20
[python] list value rank  (0) 2022.09.08
[python] 포아송 분포  (0) 2022.08.24
pandas dataframe show max columns  (0) 2022.08.18
dataframe split date  (0) 2022.06.24
posted by 초코렛과자
:
Python 2022. 9. 8. 07:50

scipy.stats를 이용한 list value rank 만들기

import scipy.stats as ss
target = [1,42,12]
list(ss.rankdata(target))
# [1,3,2]

 

'Python' 카테고리의 다른 글

[python] jupyter notebook 지수표현(e) 없애기  (0) 2023.04.20
[python] string contains  (0) 2023.02.16
[python] 포아송 분포  (0) 2022.08.24
pandas dataframe show max columns  (0) 2022.08.18
dataframe split date  (0) 2022.06.24
posted by 초코렛과자
:
Python 2022. 8. 24. 09:52

통계분석 기초에 대한 리마인드를 위해, 누구나 파이썬 통계분석 이라는 책을 보고 있는데 유용한 코드가 나와서 기록

포아송 분포는 scipy.stats.poisson으로 호출이 가능하지면, 수식을 보기위해 기록

 

import numpy as np
from scipy import stats

# 기댓값
def E(X, g=lambda x:x):
    x_set, f = X
    return np.sum([g(x_k) * f(x_k) for x_k in x_set])
# 분산
def V(X, g=lambda x:x):
    x_set,f = X
    mean = E(X,g)
    return np.sum([(g(x_k)-mean)**2 * f(x_k) for x_k in x_set])
# 확률변수를 인수로 가지며, 확률변수가 확률의 성질을 만족하는지 확인하고,
# 기댓값과 분산을 계산하여 반환하는 함수
def check_prob(X):
    x_set, f = X
    prob = np.array([f(x_k) for x_k in x_set])
    assert np.all(prob >= 0), 'minus probability'
    prob_sum = np.round(np.sum(prob),6)
    assert prob_sum == 1, f'sum of probability{prob_sum}'
    print(f'expected value {E(X):.4}')
    print(f'variance{V(X):.4}')
    
#---------------------------------------------#

# 포아송 분포
from scipy.special import factorial

def Poi(lam):
    x_set = np.arange(20)
    def f(x):
        if x in x_set:
            return np.power(lam,x) / factorial(x)*np.exp(-lam)
        else:
            return 0
    return x_set, f
lam = 3
X = Poi(lam)
check_prob(X)

 

'Python' 카테고리의 다른 글

[python] string contains  (0) 2023.02.16
[python] list value rank  (0) 2022.09.08
pandas dataframe show max columns  (0) 2022.08.18
dataframe split date  (0) 2022.06.24
dataframe value counts to dataframe  (0) 2022.06.22
posted by 초코렛과자
:
Python 2022. 8. 18. 10:32
import pandas as pd
pd.options.display.max_columns = 300

 

'Python' 카테고리의 다른 글

[python] list value rank  (0) 2022.09.08
[python] 포아송 분포  (0) 2022.08.24
dataframe split date  (0) 2022.06.24
dataframe value counts to dataframe  (0) 2022.06.22
loc을 이용한 data 변경  (0) 2022.06.07
posted by 초코렛과자
:
Python 2022. 6. 24. 08:35
import pandas as pd
df = pd.read_csv("data.csv")
pd.to_datetime(df['date+col']).dt.date
pd.to_datetime(df['date+col']).dt.month
pd.to_datetime(df['date+col']).dt.hour

'Python' 카테고리의 다른 글

[python] 포아송 분포  (0) 2022.08.24
pandas dataframe show max columns  (0) 2022.08.18
dataframe value counts to dataframe  (0) 2022.06.22
loc을 이용한 data 변경  (0) 2022.06.07
Isolation Forest와 One-Class SVM  (0) 2022.06.03
posted by 초코렛과자
:
Python 2022. 6. 22. 13:35
df[['col1','col2']].value_counts().rename_axis(['col1','col2']).to_frame('counts')

 

'Python' 카테고리의 다른 글

pandas dataframe show max columns  (0) 2022.08.18
dataframe split date  (0) 2022.06.24
loc을 이용한 data 변경  (0) 2022.06.07
Isolation Forest와 One-Class SVM  (0) 2022.06.03
[python] sklearn RandomizedSearchCV  (0) 2022.05.30
posted by 초코렛과자
:
Python 2022. 6. 7. 15:00
df = pd.read_csv("")

df.loc[df['column'] == 'target', 'column'] = 'change_target'

 

'Python' 카테고리의 다른 글

dataframe split date  (0) 2022.06.24
dataframe value counts to dataframe  (0) 2022.06.22
Isolation Forest와 One-Class SVM  (0) 2022.06.03
[python] sklearn RandomizedSearchCV  (0) 2022.05.30
[python] dataframe column index 가져오기  (0) 2022.05.30
posted by 초코렛과자
:
Python 2022. 6. 3. 16:00

https://sarah0518.tistory.com/77

 

Isolation Forest와 One-Class SVM

우리의 현실 데이터는 Imbalanced 된 data가 많이 존재하죠. 그래서 오늘은 Imbalanced 된 데이터를 분석하는 모델링에 대해서 알아보려고 합니다. 1. Isolation Forest 2. One Class SVM 위의 두개에 대해서 알아.

sarah0518.tistory.com

 

설명이 잘 되어있음

posted by 초코렛과자
:
Python 2022. 5. 30. 11:50

GridSearch와 동일한 방식으로 사용하지만, 모든 조합을 시도하지 않고 각 반복마다 임의의 값만 대입해 지정한 횟수만큼 평가

 

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV

X = []
Y = []

xtrain, xtest, ytrain, ytest = train_test_split(X,Y,random_state=1234)
rf = RandomForestClassifier()

n_estimators = [int(x) for x in np.linspace(start = 200, stop = 2000, num = 10)]
max_features = ['auto', 'sqrt']
max_depth = [int(x) for x in np.linspace(10, 110, num = 11)]
max_depth.append(None)
min_samples_split = [2, 5, 10]
min_samples_leaf = [1, 2, 4]
bootstrap = [True, False]

random_grid = {'n_estimators': n_estimators,
               'max_features': max_features,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf,
               'bootstrap': bootstrap}
print(random_grid)

rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 3, verbose=2, random_state=42, n_jobs = -1)
# Fit the random search model
rf_random.fit(xtrain,ytrain)

best_random = rf_random.best_estimator_

best_random.score(xtest,ytest)
posted by 초코렛과자
: