카테고리 없음

2023-11-21 주성분분석 머신러닝 사용한 실습과제

20230919 2023. 11. 21. 17:28

가제 : 붓꽃 데이터 군집화 예제 


밑에 참고한링크가있습니다. 

 

주성분 분석을 사용하기위ㅜ하여 데이터를 가져와야한다. 

from sklearn.datasets import load_iris

를 사용해서 데이터를 갖고오자  그리고 

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA

PCA 클래스와 시각화 k 평균 차원축소 군집화를 사용하기 위한 모듈도 가지고오자 

 

iris = load_iris()
iris_df = pd.DataFrame(data=iris.data,columns=['sepal_length','sepal_width','petal_length','petal_width'])
iris_2d = iris_df.values.reshape(-1, 4)

iris 에다가 load_iris() 를 지정하고 

판다 를사용하여  테이터프레임 컬럼 함수를 지정해주자 iris df 에 지정해주자 

그리고 iris_2d 는 차원 배열을 정리해준다. 그냥 reshape 집어넣는경우 수가 안맞기에 values 를 넣어주었다.

 


from sklearn.decomposition import PCA
pca = PCA(n_components=2) #얘도 차원 두개로 축소  
pca.fit(iris_df)

print(pca.components_.shape) #2,4

 

그리고 차원을 축소시킨다. 

출력시켜서 보면 줄어든걸 볼수있다. 

여기선 책에서 본대로하면 iris_2d 를 써야하나 d

행렬 차원을 바꿔놨기에 df 를 집어넣는다. 

이제 pca를 사용하여 배열데이터를 줄여본다. 반으로 줄었다. 

이제 데이터를 재구성 시켜보았다. 이름 하나 새로추가해서 바꿔주고 바꿔놓은 pca 를 집어넣어주자 

 그리고 분산도를 확인하기위해 넘파이를 사용하여 분산도를 살펴본다. 분산도가높으면 정확도도 높다

그래프로도 출력해보았다.

k-평균화 하여 군집화한뒤 정확도를 높여주자 그러니 
(array([0, 1, 2], dtype=int32), array([61, 50, 39])) 가 나왔다 경고문은 
FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning 
이렇게나오는데 어차피 init 과 auto 는 여기서 알아서 해주기때문에 안넣었다.

 

 

 

 

 

 

이제 산점도를 사용하여 출력한다. 뭉쳐있는게 많을수록 정확하다는 의미가 있다. 

 

필자는 점색상을 바꿔보려했으나 

 

 

한가지 색밖에 안나온다 선생님께 여쭤보니 반복시켜서 출력 하라고 하셨길래 

그래서 반복시키는걸로 바꾸고 그냥 색상 빼버렸다. 

 

------------------------------------------------------------------------------------------------------------------------------------------------------

혼공 머신러닝+딥러닝 책 참고하였습니다. 그리고 책말고 자잘한오류해결하기위해 참고한 링크입니다.

https://stackoverflow.com/questions/71158544/list-object-has-no-attribute-reshape

 

'list' object has no attribute 'reshape'

I've been following a video tutorial on python, and I received an attribute error regardless typing the exact same code as the video. import numpy as np A = np.array([[56.0, 0.0, 4,4, 68.0], ...

stackoverflow.com

 

https://stackoverflow.com/questions/64154762/builtin-function-or-method-object-has-no-attribute-reshape-what-does-this-m

 

"builtin_function_or_method' object has no attribute 'reshape'" what does this mean?

I'm a novice, so this question may be somewhat obvious for someone. import numpy as np print("array") array = np.arange(8) matrix = np.array.reshape(2,4) print(matrix) The result is this.

stackoverflow.com

 

https://stackoverflow.com/questions/53723928/attributeerror-series-object-has-no-attribute-reshape

 

AttributeError: 'Series' object has no attribute 'reshape'

I'm using sci-kit learn linear regression algorithm. While scaling Y target feature with: Ys = scaler.fit_transform(Y) I got ValueError: Expected 2D array, got 1D array instead: After that I

stackoverflow.com

https://wikidocs.net/92110

 

18. Matplotlib 산점도 그리기

![](https://wikidocs.net/images/page/92110/scatter_plot_00.png) **산점도 (Scatter plot)**는 두 변수의 상관…

wikidocs.net

https://lifelong-education-dr-kim.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%AC-Matplotlib-pyplot%EC%9D%98-scatter%EC%82%B0%EC%A0%90-%ED%95%A8%EC%88%98-%EC%9D%B8%EC%9E%90-%EB%B0%8F-%EC%82%AC%EC%9A%A9%EB%B2%95-%EC%84%A4%EB%AA%85

 

파이썬 Matplotlib pyplot의 scatter(산점) 함수 인자 및 사용법 설명

파이썬 프로그래밍을 데이터 분석에 사용하다 보면 데이터를 가시화해야 하는 경우가 있습니다. 일반적으로 많이 사용하는 함수는 2차원의 직선을 그려주는 plot일 겁니다. 사실 이는 직선이라

lifelong-education-dr-kim.tistory.com

https://yololife-sy.medium.com/python-reshape-1-1-%EC%97%90%EC%84%9C-1%EC%9D%98-%EC%9D%98%EB%AF%B8-97b713be5230

 

[Python] reshape(-1,1)에서 -1의 의미?

arange()를 이용해서 1차원 배열 만들기

yololife-sy.medium.com

https://firework-ham.tistory.com/32

 

[python] numpy Shape 이란? Shape다루기

이번 포스팅은 numpy의 Shape 다루기를 정리해보겠습니다. 머신러닝에서 행렬의 차원을 shape라는 개념으로 표현합니다. numpy에서도 차원을 다루기 위한 방법으로 shape을 제공하는데 어떻게 사용하

firework-ham.tistory.com