<jupyter_start><jupyter_text>機器學習100天——第2天：簡單線性迴歸 (Simple Linear Regression)---  第一步：數據前處理這裡的步驟觀念可詳見[Day 1](http://localhost:8888/notebooks/Documents/GitHub/100-Days-Of-ML-Code/ipynb_zh-TW/Day%201%20-%20Data_Preprocessing.ipynb)，相較Day 1這裡多導入了matplotlib，是一個python上的2D繪圖庫，作為後面結果視覺化而用。<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataset = pd.read_csv('../datasets/studentscores.csv')
print(dataset)<jupyter_output>Hours  Scores
0     2.5      21
1     5.1      47
2     3.2      27
3     8.5      75
4     3.5      30
5     1.5      20
6     9.2      88
7     5.5      60
8     8.3      81
9     2.7      25
10    7.7      85
11    5.9      62
12    4.5      41
13    3.3      42
14    1.1      17
15    8.9      95
16    2.5      30
17    1.9      24
18    6.1      67
19    7.4      69
20    2.7      30
21    4.8      54
22    3.8      35
23    6.9      76
24    7.8      86<jupyter_text>.iloc可以很靈活地從data frame中選擇想要的元素，第一個參數為列，第二個為行<jupyter_code>X = dataset.iloc[ : , :1 ].values #全列，第一行
Y = dataset.iloc[ : , 1 ].values #全列，第二行
print('X:')
print(X)
print('Y:')
print(Y)<jupyter_output>X:
[[ 2.5]
 [ 5.1]
 [ 3.2]
 [ 8.5]
 [ 3.5]
 [ 1.5]
 [ 9.2]
 [ 5.5]
 [ 8.3]
 [ 2.7]
 [ 7.7]
 [ 5.9]
 [ 4.5]
 [ 3.3]
 [ 1.1]
 [ 8.9]
 [ 2.5]
 [ 1.9]
 [ 6.1]
 [ 7.4]
 [ 2.7]
 [ 4.8]
 [ 3.8]
 [ 6.9]
 [ 7.8]]
Y:
[21 47 27 75 30 20 88 60 81 25 85 62 41 42 17 95 30 24 67 69 30 54 35 76 86]<jupyter_text>同Day 1 原版為導入sklearn.cross_validation但sklearn未來會將此更新，故這裡為sklearn.model_selection才不會噴緊告或error。  切1/4為test<jupyter_code>from sklearn.model_selection import train_test_split 
X_train, X_test, Y_train, Y_test = train_test_split( X, Y, test_size = 1/4, random_state = 0)<jupyter_output><empty_output><jupyter_text>第二步：訓練集使用簡單線性回歸模型來訓練從sklearn的線性模型類中調用線性迴歸模型  創建一個線性回歸對象regressor，並對訓練集利用fit()方法進行訓練<jupyter_code>from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor = regressor.fit(X_train, Y_train)<jupyter_output><empty_output><jupyter_text>第三步：預測結果利用predict()方法對測試集進行預測<jupyter_code>Y_pred = regressor.predict(X_test)<jupyter_output><empty_output><jupyter_text>第四步：視覺化結果視覺化- 使用scatter做散點圖- 使用plot對訓練的預測作曲線圖<jupyter_code>plt.scatter(X_train , Y_train, color = 'red')
plt.plot(X_train , regressor.predict(X_train), color ='blue')
print('訓練集結果:')
plt.show()
plt.scatter(X_test , Y_test, color = 'red')
plt.plot(X_test , regressor.predict(X_test), color ='blue')
print('測試集結果:')
plt.show()<jupyter_output>訓練集結果:
{'fraction_non_alphanumeric': 0.13509127789046654, 'fraction_numerical': 0.10912778904665314, 'mean_word_length': 3.1655405405405403, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '266956', 'license': 'MIT', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1342, 'n_words': 210, 'path': 'ipynb_zh-TW/Day 2 - Simple_Linear_Regression.ipynb', 'repo_name': 'jeremywangyuan/100-Days-Of-ML-Code-TW', 'size': '2465'}