1. 程式人生 > >分別使用1次,2次,4次多項式迴歸模型在比薩訓練樣本上進行擬合

分別使用1次,2次,4次多項式迴歸模型在比薩訓練樣本上進行擬合

import matplotlib.pyplot as plt

x_train = [[6], [8], [10], [14], [18]]
y_train = [[7], [9], [13], [17.5], [18]]

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)

import numpy as np
xx = np.linspace(0, 26, 100)#生成了1行100列的一個矩陣
#print(xx)
xx = xx.reshape(xx.shape[0], 1)#將1行100列的矩陣轉化成100行1列的矩陣形式
#print(xx)
yy = regressor.predict(xx)


from sklearn.preprocessing import PolynomialFeatures
#初始化二次多項式生成器
poly2 = PolynomialFeatures(degree=2)
x_train_poly2 = poly2.fit_transform(x_train)
regressor_poly2 = LinearRegression()
regressor_poly2.fit(x_train_poly2, y_train)
xx_poly2 = poly2.transform(xx)
yy_poly2 = regressor_poly2.predict(xx_poly2)


#初始化四次多項式生成器
poly4 = PolynomialFeatures(degree=4)
x_train_poly4 = poly4.fit_transform(x_train)
regressor_poly4 = LinearRegression()
#對四次多項式迴歸模型進行訓練
regressor_poly4.fit(x_train_poly4, y_train)
xx_poly4 = poly4.transform(xx)
yy_poly4 = regressor_poly4.predict(xx_poly4)


plt.scatter(x_train, y_train)

plt1, = plt.plot(xx, yy, label='Degree=1')
plt2, = plt.plot(xx, yy_poly2, label='Degree=2')
plt4, = plt.plot(xx, yy_poly4, label='Degree=4')

plt.axis([0, 25, 0, 25])
plt.xlabel('Diameter of Pizza')
plt.ylabel('Pirce of Pizza')
plt.legend(handles=[plt1, plt2, plt4])
plt.show()
#評估3種迴歸模型在訓練集上的R-squared值
print('The R-squared value of Polynomonal Regressor(Degree=1) performing on the training data is', regressor.score(x_train, y_train))
print('The R-squared value of Polynomonal Regressor(Degree=2) performing on the training data is', regressor_poly2.score(x_train_poly2, y_train))
print('The R-squared value of Polynomonal Regressor(Degree=4) performing on the training data is', regressor_poly4.score(x_train_poly4, y_train))


#評估3種迴歸模型在測試資料集上的效能表現

x_test = [[6], [8], [11], [16]]
y_test = [[8], [12], [15], [18]]

print("線性模型效能評估:",regressor.score(x_test, y_test))

x_test_poly2 = poly2.transform(x_test)
print("2次多項式迴歸模型的效能評估:",regressor_poly2.score(x_test_poly2, y_test))

x_test_poly4 = poly4.transform(x_test)
print("4次多項式迴歸模型的效能評估:",regressor_poly4.score(x_test_poly4, y_test))

執行結果:

The R-squared value of Polynomonal Regressor(Degree=1) performing on the training data is 0.910001596424
The R-squared value of Polynomonal Regressor(Degree=2) performing on the training data is 0.98164216396
The R-squared value of Polynomonal Regressor(Degree=4) performing on the training data is 1.0
線性模型效能評估: 0.809726797708
2次多項式迴歸模型的效能評估: 0.867544365635
4次多項式迴歸模型的效能評估: 0.809588079578
最終效果圖:



相關推薦

分別使用124多項式迴歸模型比薩訓練樣本進行

import matplotlib.pyplot as plt x_train = [[6], [8], [10], [14], [18]] y_train = [[7], [9], [13], [17.5], [18]] from sklearn.linear_mod

【Python】Python實現N級臺階可以走123步一共多少種樓梯方法

去面試的時候,筆試題有一個二選一,有一個是這個問題,當時選的另一個,現在實現下這個臺階問題。 如果只有一級臺階,那麼方法只有1種,如果是有二級臺階,那麼方法2種,如果三屆臺階,那麼實現方法有4種。 如果臺階數再增加,大於三屆臺階以後,可以認為是隻有一二三級臺階的一個重複實現,可以使用遞迴的方

java算法面試題:遞歸算法題21個人102個比第1個人大2依次遞推請用遞歸方式計算出第8個人多大?

else oid 算法題 body println 算法 ring swift java算法 package com.swift; public class Digui_Return { public static void main(String[] arg

假設你現在正在爬樓梯樓梯有 nn 級。每次你只能爬 1 級或者 2那麼你有多少種方法爬到樓梯的頂部?

假設你現在正在爬樓梯,樓梯有 nn 級。每次你只能爬 1 級或者 2 級,那麼你有多少種方法爬到樓梯的頂部? 輸入格式 第一行輸入一個整數n(1≤n≤50),代表樓梯的級數。 輸出格式 輸出爬到樓梯頂部的方法總數。 樣例輸入 複製 5 樣例輸出 複製 8 Fibonacc

[Al]演算法:有n級階梯每次走1步或2最多有多少種走法

@Filename : floor.c * @Author : Mr.Zhong * @Date : 2018-11-02 * @Description: n級階梯,每次走一步或2步,最多有多少種走法 * @Analysis :

IBM: 告別1.0迎接2.0企業諮詢進入技術驅動新時代

這是一個充滿不確定性的時代。技術的日新月異,使得我們不得不推翻之前已經建立的各種成熟的商業模式、

10個臺階每次只能1個或者2一共有多少種走法

思路來得太慢,好想拿小錘錘捶自己喲~~~ 第一種,遞迴思路。 如果你上10個臺階,可以分解成下面兩種情況: ● 上9個臺階,最後上1個臺階。假設這種情況下,上前面9個臺階的方法數為m。 ● 上8個臺

125元10元20元,50元組合成100元

100元換零錢1元,2元,5元,10元,20元,50元有多少種組合方案 一道筆試題,當時就懵逼了。。。 找到遞推公式之後,其實也不難 F(N,M)=F(N,M-1)+F(N-VAL[M],M)

N級臺階(比如100級)每次可走1步或者2求總共有多少種走法?

走臺階演算法(本質上是斐波那契數列)在面試中常會遇到,描述就如題目那樣:總共100級臺階(任意級都行),小明每次可選擇走1步、2步或者3步,問走完這100級臺階總共有多少種走法? 一、 題目分析 這個問題本質上是斐波那契數列,假設只有一個臺階,那麼只有一種跳法,那

用java實現簡單快速的webservice客戶端/資料採集器(支援soap1.1和soap1.2標準支援utf-8編碼)

前言: 用了cxf,axis等各種wbeservice實現庫,簡單試用了一下動態呼叫的方式,很不滿意,完全無法滿足業務的需要,所以自己實現了一個webservice採集客戶端,方便動態呼叫外部webservice介面。 一、實現的功能 1、soap1.1客戶端(soap1.

把一元錢換成125分的硬幣。有多少種換法

#include<stdio.h> main() { int a,b,c,cnt=0; for(a=0;a<=100;a++){ for(b=0;b<=50;b++){ for(c=

程式設計實現 125元100元紙幣組成800元共有多少種情況。?

#include <iostream> using namespace std; void main() { //int[] a={1,2,5,10,20,50};

125元10元20元和50元的紙幣組成100元共有多少種情況

  static void Main(string[] args)        {            int count = 0;            //1元組成的情況,最多有100種            for (int a = 0; a <= 100;

銀行提供了整存整取定期儲蓄業務存期分為123年5年到期取本息之和年利率如下 12.25% 22.7% 3年 3.24% 5年

public class Test_10 {/*銀行提供了整存整取定期儲蓄業務,存期分為1年,2年,3年,5年,到期取本息之和,年利率如下     1年   2.25%     2年   2.7%     3年   3.24%       5年   3.6%      

寒假作業2.25G - iBoard

bubuko mos user 技術分享 offer name most only not 題目: After years of success with a single-button mouse, a well known computer company has

Google的一道面試題的推廣(扔雞蛋不破的層數23個n個雞蛋呢)

1題目描述 google面試題:幾年前的Google的面試題在論壇炒得很火,今年又被人人網當作面試題了,題目如下:“有一個100層高的大廈,你手中有兩個相同的雞蛋。從這個大廈的某一層扔下雞蛋就會碎,用你手中的這兩個雞蛋,找出一個最優的策略,來得知那個臨界層面。” 人人網面試題:原題來自:2014人人網研發

tcp三握手建立連線和4揮手斷開連線

[LAST_ACK -> CLOSED] 伺服器收到了對FIN的ACK, 徹底關閉連線. 客戶端狀態改變: 1.[CLOSED -> SYN_SENT] 客戶端呼叫connect, 傳送同步報文段; 2.[SYN_SENT -> ESTABLISHED] connect呼叫成功, 則進入ES

TCP3握手連線協議和4握手斷開連線協議

   TCP/IP 狀態機,如下圖所示:       在TCP/IP協議中,TCP協議提供可靠的連線服務,採用三次握手建立一個連線,如圖1所示。 (SYN包表示標誌位syn=1,ACK包表示標誌位ack=1,SYN+ACK包表示標誌位syn=1,ack=1)  (1) 第一

23種設計模式之抽象工廠模式(建立型2 AbstractFactoryc++實現)

#include<stdlib.h>#include<stdio.h>#include<iostream>usingnamespace std;classFruit {public:virtual void sayName() = 0;};