import numpy as np
from sklearn import linear_model # scikit-learn ¸ðµâÀ» °¡Á®¿Â´Ù
regr = linear_model.LinearRegression()
X = [[164], [179], [162], [170]] # ´ÙÁßȸ±Í¿¡µµ »ç¿ëÇϵµ·Ï ÇÔ
y = [53, 63, 55, 59] # y = f(X)ÀÇ °á°ú
regr.fit(X, y)
regr.fit(X, y)
coef = regr.coef_ # Á÷¼±ÀÇ ±â¿ï±â
intercept = regr.intercept_ # Á÷¼±ÀÇ ÀýÆí
score = regr.score(X, y) # ÇнÀµÈ Á÷¼±ÀÌ µ¥ÀÌÅ͸¦ ¾ó¸¶³ª Àß µû¸£³ª
print("y =", coef, "* X + ", intercept)
print("The score of this line for the data: ", score)
input_data = [ [180], [185] ]
regr.predict([[169]])
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model # scikit-learn ¸ðµâÀ» °¡Á®¿Â´Ù
regr = linear_model.LinearRegression()
X = [[164], [179], [162], [170]] # ¼±Çüȸ±ÍÀÇ ÀÔ·ÂÀº 2Â÷¿øÀ¸·Î ¸¸µé¾î¾ß ÇÔ
y = [53, 63, 55, 59] # y = f(X)ÀÇ °á°ú°ª
regr.fit(X, y)
# ÇнÀ µ¥ÀÌÅÍ¿Í y °ªÀ» »êÆ÷µµ·Î ±×¸°´Ù.
plt.scatter(X, y, color='black')
# ÇнÀ µ¥ÀÌÅ͸¦ ÀÔ·ÂÀ¸·Î ÇÏ¿© ¿¹Ãø°ªÀ» °è»êÇÑ´Ù.
y_pred = regr.predict(X)
# ÇнÀ µ¥ÀÌÅÍ¿Í ¿¹Ãø°ªÀ¸·Î ¼±±×·¡ÇÁ·Î ±×¸°´Ù.
# °è»êµÈ ±â¿ï±â¿Í y ÀýÆíÀ» °¡Áö´Â Á÷¼±ÀÌ ±×·ÁÁø´Ù
plt.plot(X, y_pred, color='blue', linewidth=3)
plt.show()
import numpy as np
from sklearn import linear_model
regr = linear_model.LinearRegression()
# ³²ÀÚ´Â 0, ¿©ÀÚ´Â 1
X = [[164, 1], [167, 1], [165, 0], [170, 0], [179, 0], [163, 1], [159, 0], [166, 1]] # ÀԷµ¥ÀÌÅ͸¦ 2Â÷¿øÀ¸·Î ¸¸µé¾î¾ß ÇÔ
y = [43, 48, 47, 66, 67, 50, 52, 44] # y °ªÀº 1Â÷¿ø µ¥ÀÌÅÍ
regr.fit(X, y) # ÇнÀ
print('°è¼ö :', regr.coef_ )
print('ÀýÆí :', regr.intercept_)
print('Á¡¼ö :', regr.score(X, y))
print('ÀºÁö¿Í µ¿¹ÎÀÌÀÇ ÃßÁ¤ ¸ö¹«°Ô :', regr.predict([[166, 1], [166, 0]]))
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn import datasets
# ´ç´¢º´ µ¥ÀÌÅÍ ¼¼Æ®¸¦ sklearnÀÇ µ¥ÀÌÅÍÁýÇÕÀ¸·ÎºÎÅÍ ÀоîµéÀδÙ.
diabetes = datasets.load_diabetes()
print('shape of diabetes.data: ', diabetes.data.shape)
print(diabetes.data)
print('ÀԷµ¥ÀÌÅÍÀÇ Æ¯¼ºµé')
print(diabetes.feature_names)
print('target data y:', diabetes.target.shape)
print(diabetes.target)
X = diabetes.data[:, 2]
print(X)
X = diabetes.data[:, np.newaxis, 2]
print(X)
regr.fit(X, diabetes.target) # ÇнÀÀ» ÅëÇÑ ¼±Çüȸ±Í ¸ðµ¨À» »ý¼º
print(regr.coef_, regr.intercept_)
# ÇнÀ µ¥ÀÌÅÍ¿Í Å×½ºÆ® µ¥ÀÌÅ͸¦ ºÐ¸®ÇÑ´Ù.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target,
test_size=0.2)
# ÇнÀ µ¥ÀÌÅÍ¿Í Å×½ºÆ® µ¥ÀÌÅ͸¦ ºÐ¸®ÇÑ´Ù.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(diabetes.data[:,np.newaxis,2],
diabetes.target,
test_size=0.2)
regr = LinearRegression()
regr.fit(X_train, y_train)
score = regr.score(X_train, y_train)
print(score)
score = regr.score(X_test, y_test)
print(score)
X_train, X_test, y_train, y_test = train_test_split(diabetes.data, diabetes.target,
test_size=0.2)
regr.fit(X_train, y_train)
y_pred = regr.predict(X_test) # Å×½ºÆ® µ¥ÀÌÅÍ·Î ¿¹ÃøÇغ¸ÀÚ.
print(y_pred)
print(y_test)
import numpy as np
from sklearn import linear_model # scikit-learn ¸ðµâÀ» °¡Á®¿Â´Ù
from sklearn import datasets
import matplotlib.pyplot as plt
regr = linear_model.LinearRegression()
# ÇнÀ µ¥ÀÌÅÍ¿Í Å×½ºÆ® µ¥ÀÌÅ͸¦ ºÐ¸®ÇÑ´Ù.
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(diabetes.data,
diabetes.target,
test_size=0.2)
regr.fit(X_train, y_train)
print(regr.coef_, regr.intercept_)
y_pred = regr.predict(X_test)
plt.scatter(y_pred, y_test)
plt.show()
plt.scatter(y_pred, y_test, color='black')
x = np.linspace(0, 330, 100) # ƯÁ¤ ±¸°£ÀÇ Á¡
plt.plot(x, x, linewidth = 3, color = 'blue')
plt.show()
from sklearn.metrics import mean_squared_error
... # ÀÌÀü Àý¿¡¼ ±¸ÇÑ ¼±Çüȸ±Í ¸ðµ¨ÀÇ Äڵ带 »ðÀÔÇÔ
print('Mean squared error:', mean_squared_error(y_test, y_pred))
from sklearn.datasets import load_iris
iris = load_iris()
print(iris.data)
iris.data.shape
print(iris.feature_names) # 4°³ÀÇ Æ¯Â¡ À̸§À» Ãâ·ÂÇÑ´Ù.
# Á¤¼ö´Â ²ÉÀÇ Á¾·ù¸¦ ³ªÅ¸³½´Ù.: 0 = setosa, 1=versicolor, 2=virginica
print(iris.target)
# (80:20)À¸·Î ºÐÇÒÇÑ´Ù.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train,X_test,y_train,y_test = train_test_split(iris.data, iris.target,test_size=0.2)
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
num_neigh = 1
knn = KNeighborsClassifier(n_neighbors = num_neigh)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
scores = metrics.accuracy_score(y_test, y_pred)
print('n_neighbors°¡ {0:d}À϶§ Á¤È®µµ: {1:.3f}'.format(num_neigh, scores))
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
iris = load_iris()
knn = KNeighborsClassifier(n_neighbors=6)
knn.fit(iris.data, iris.target)
classes = {0:'setosa', 1:'versicolor', 2:'virginica'}
# ¾ÆÁ÷ º¸Áö ¸øÇÑ »õ·Î¿î µ¥ÀÌÅ͸¦ Á¦½ÃÇغ¸ÀÚ.
X = [[3,4,5,2],
[5,4,2,2]]
y = knn.predict(X)
print(classes[y[0]])
print(classes[y[1]])
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns # ½Ã°¢È¸¦ À§ÇÏ¿© Seaborn ¶óÀ̺귯¸®¸¦ ÀÌ¿ëÇÔ
from sklearn.datasets import load_boston
boston = load_boston()
df = pd.DataFrame(boston.data, columns=boston.feature_names)
print(df.head())
df['MEDV'] = boston.target
print( df.isnull().sum() )
sns.set(rc={'figure.figsize':(12,10)})
correlation_matrix = df.corr().round(2)
sns.heatmap(data=correlation_matrix, annot=True)
plt.show()
sns.pairplot(df[["MEDV", "RM", "AGE", "CHAS", "B"]])
plt.show()
X = df[['LSTAT', 'RM']]
y = df['MEDV']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
lin_model = LinearRegression()
lin_model.fit(X_train, y_train)
y_test_predict = lin_model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, y_test_predict))
print('RMSE =', rmse)