<jupyter_start><jupyter_text>특징 공학  범주 특징<jupyter_code>data = [
    {'price' : 850000, 'rooms' : 4, 'neighborhood' : 'Queen Anne'},
    {'price' : 700000, 'rooms' : 3, 'neighborhood' : 'Fremont'},
    {'price' : 650000, 'rooms' : 3, 'neighborhood' : 'Wallingford'},
    {'price' : 600000, 'rooms' : 2, 'neighborhood' : 'Fremont'}]
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse = False, dtype = int)
vec.fit_transform(data)
vec.get_feature_names()
vec = DictVectorizer(sparse = True, dtype = int)
vec.fit_transform(data)<jupyter_output><empty_output><jupyter_text>텍스트 특징<jupyter_code>sample = ['problem of evil', 'evil queen', 'horizon problem']
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer()
X = vec.fit_transform(sample)
X
import pandas as pd
pd.DataFrame(X.toarray(), columns = vec.get_feature_names())
from sklearn.feature_extraction.text import TfidfVectorizer
vec = TfidfVectorizer()
X = vec.fit_transform(sample)
pd.DataFrame(X.toarray(), columns = vec.get_feature_names())
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])
y = np.array([4, 2, 1, 3, 7])
plt.scatter(x, y)
from sklearn.linear_model import LinearRegression
X = x[:, np.newaxis]
model = LinearRegression().fit(X, y)
yfit = model.predict(X)
plt.scatter(x, y)
plt.plot(x, yfit)
from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree = 3, include_bias = False)
X2 = poly.fit_transform(X)
print(X2)
model = LinearRegression().fit(X2, y)
yfit = model.predict(X2)
plt.scatter(x, y)
plt.plot(x, yfit)<jupyter_output><empty_output><jupyter_text>누락 데이터의 대체<jupyter_code>from numpy import nan
X = np.array([[nan, 0, 3], [3, 7, 9],[3, 5, 2], [4, nan, 6], [8, 8, 1]])
y = np.array([14, 16, -1, 8, -5])
from sklearn.impute import SimpleImputer as Imputer
imp = Imputer(strategy = 'mean')
X2 = imp.fit_transform(X)
X2
model = LinearRegression().fit(X2, y)
model.predict(X2)<jupyter_output><empty_output><jupyter_text>특징 파이프라인 1. 누락된 값을 평균으로 대체한다.2. 특징을 이차 형태로 전환한다.3. 선형 회귀를 적합시킨다.<jupyter_code>from sklearn.pipeline import make_pipeline
model = make_pipeline(Imputer(strategy = 'mean'), PolynomialFeatures(degree = 2), LinearRegression())
model.fit(X, y)
print(y)
print(model.predict(X)) # 모델의 모든 단계가 자동으로 적용<jupyter_output>[14 16 -1  8 -5]
[14. 16. -1.  8. -5.]
{'fraction_non_alphanumeric': 0.15374894692502106, 'fraction_numerical': 0.03580454928390901, 'mean_word_length': 5.785714285714286, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '86714', 'license': 'MIT', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1056, 'n_words': 224, 'path': '5. Machine Learning/5-3. Feature Engineering.ipynb', 'repo_name': 'Lee-HyeongBin/Data_Science_Handbook', 'size': '2374'}