본문 바로가기
728x90

데이터분석7

Linear Regression Gradient Descent 선형회귀 표현식 Y는 종속변수 값 θ₀ 는 bias θ₁,…,θₙ 회귀계수 x₁, x₂,…,xₙ 는 독립변수 위의 식을 아래와 같이 벡터로 표현이 가능하다. θ는 model의 파라미터 벡터 x는 Xo=1인 입력 벡터 # data 생성 np.random.seed(321) x_old = np.random.rand(1000, 1) # intercept항 1추가 y = 2 + 5 * x_old + np.random.rand(1000, 1) # plot plt.scatter(x_old, y, s=10) plt.xlabel('x') plt.ylabel('y') plt.ylim(2, 8) plt.show() # intercept 추가 x = np.c_[np.ones(x_old.shape[0]), x_old] 비용 함.. 2020. 12. 30.
RMSE, Grid Search python 구현 RMSE # RMSE 공식을 구현한 함수를 생성합니다. # 이 함수는 예측값(predict)과 정답(actual)을 인자로 받습니다. def rmse(predict, actual): # predict와 actual을 numpy array로 변환합니다. # 이렇게 하면 수학 연산을 편하게 할 수 있습니다. predict = np.array(predict) actual = np.array(actual) # 공식에 쓰여진대로 predict와 actual을 빼서 차이를 구합니다. # 이 차이를 distance라는 이름의 새로운 변수에 할당합니다. distance = predict - actual # 공식에 쓰여진대로 distance를 제곱합니다. # 이 결과를 square_distance라는 이름의 새로운 변수.. 2020. 11. 12.
Logistic Regression 데이터 불러오기(Setosa 제거 binary classfication 진행) import pandas as pd import numpy as np iris = pd.read_csv('iris_short2.csv') numpy 배열로 만들기 iris_np = iris.values Species순서로 정렬되어있기 때문에 random shuffle 진행 np.random.shuffle(iris_np) 독립변수 / 종속변수 iris_features = iris_np[:,:-1] # 독립변수 iris_labels = iris_np[:,-1] # 종속변수 종속변수 label Encoding from sklearn import preprocessing le = preprocessing.LabelEncoder() i.. 2020. 11. 12.
seaborn 시각화 python 모듈 불러오기 import seaborn as sns import matplotlib.pyplot as plt %matplotlib inline seaborn안에 tips데이터 불러오기 # tips 데이터 불러오기 tips = sns.load_dataset('tips') # 데이터 확인 tips.head() relplot scatter plot # x : x축 column # y : y축 column # data : 사용할 data frame sns.relplot(x="total_bill", y="tip", data=tips) # hue(명목형) : 해당 변수를 기준으로 색깔 다르게 표시 sns.relplot(x="total_bill", y="tip", hue='sex', data=tips) # hue(.. 2020. 11. 12.
데이터 전처리 python 모듈 불러오기 import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns %matplotlib inline 데이터 불러오기 boston = pd.read_csv("data/boston_missing.csv") 데이터 shape 확인 boston.shape # (506, 20) 데이터 column 확인 boston.info() 요약 통계량 확인 * describe() 함수를 사용 * 수치형 변수에 관해서만 결과를 보여준다 * 결측치는 제거하고 계산해준다 boston.describe() 상관분석 * 수치형 변수중에서 각각 2개의 column씩 상관관계를 보여준다 * 값은 -1 ~ 1사이에서 존재한다 *.. 2020. 11. 12.
python 복사 단순 객체복사 vs shallow copy vs deep copy 코드를 짜다보면 객체 복사를 하는 경우가 많다 아마 이 글을 읽는 사람은 deep copy의 존재를 알고있기 때문에 들어왔는데 복사의 개념이 머리속에 없는 사람들은 단순 객체 복사만을 사용하고 있는 경우가 많을 것이다. 1. 단순 객체 복사 a = [x for x in range(10)] b = a # shallow copy print(b) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b[0] = 10 print(b) # [10, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(a) # [10, 1, 2, 3, 4, 5, 6, 7, 8, 9] 변수 b에 a를 그냥 바로 담았다. b의 값을 변경하는 경우 원본 데이터 a도 바뀌어버렸다. a와 b가 동일한 객체를 참조하기 때문이다. .. 2020. 10. 19.
728x90