国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【機(jī)器學(xué)習(xí)】Linear Regression

這篇具有很好參考價(jià)值的文章主要介紹了【機(jī)器學(xué)習(xí)】Linear Regression。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

1、問(wèn)題描述

一套 1000 平方英尺 (sqft) 的房屋售價(jià)為300,000美元,一套 2000 平方英尺的房屋售價(jià)為500,000美元。這兩點(diǎn)將構(gòu)成我們的數(shù)據(jù)或訓(xùn)練集。面積單位為 1000 平方英尺,價(jià)格單位為 1000 美元。

Size (1000 sqft) Price (1000s of dollars)
1.0 300
2.0 500

希望通過(guò)這兩個(gè)點(diǎn)擬合線性回歸模型,以便可以預(yù)測(cè)其他房屋的價(jià)格。例如,面積為 1200 平方英尺的房屋價(jià)格是多少。

首先導(dǎo)入所需要的庫(kù)

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('./deeplearning.mplstyle')

以下代碼來(lái)創(chuàng)建x_train和y_train變量。數(shù)據(jù)存儲(chǔ)在一維 NumPy 數(shù)組中。

# x_train is the input variable (size in 1000 square feet)
# y_train is the target (price in 1000s of dollars)
x_train = np.array([1.0, 2.0])
y_train = np.array([300.0, 500.0])
print(f"x_train = {x_train}")
print(f"y_train = {y_train}")

2、表示說(shuō)明

使用 m 來(lái)表示訓(xùn)練樣本的數(shù)量。 (x ( i ) ^{(i)} (i), y ( i ) ^{(i)} (i)) 表示第 i 個(gè)訓(xùn)練樣本。由于 Python 是零索引的,(x ( 0 ) ^{(0)} (0), y ( 0 ) ^{(0)} (0)) 是 (1.0, 300.0) , (x ( 1 ) ^{(1)} (1), y ( 1 ) ^{(1)} (1)) 是 (2.0, 500.0).

3、數(shù)據(jù)繪圖

使用 matplotlib 庫(kù)中的scatter()函數(shù)繪制這兩個(gè)點(diǎn)。 其中,函數(shù)參數(shù)markerc 將點(diǎn)顯示為紅叉(默認(rèn)為藍(lán)點(diǎn))。使用matplotlib庫(kù)中的其他函數(shù)來(lái)設(shè)置要顯示的標(biāo)題和標(biāo)簽。

# Plot the data points
plt.scatter(x_train, y_train, marker='x', c='r')
# Set the title
plt.title("Housing Prices")
# Set the y-axis label
plt.ylabel('Price (in 1000s of dollars)')
# Set the x-axis label
plt.xlabel('Size (1000 sqft)')
plt.show()

【機(jī)器學(xué)習(xí)】Linear Regression,機(jī)器學(xué)習(xí),機(jī)器學(xué)習(xí),線性回歸,人工智能

4、模型函數(shù)

線性回歸的模型函數(shù)(這是一個(gè)從 x 映射到 y 的函數(shù))可以表示為 f w , b ( x ( i ) ) = w x ( i ) + b (1) f_{w,b}(x^{(i)}) = wx^{(i)} + b \tag{1} fw,b?(x(i))=wx(i)+b(1)

計(jì)算 f w , b ( x ( i ) ) f_{w,b}(x^{(i)}) fw,b?(x(i)) 的值,可以將每個(gè)數(shù)據(jù)點(diǎn)顯示地寫(xiě)為:

對(duì)于 x ( 0 ) x^{(0)} x(0), f_wb = w * x[0] + b
對(duì)于 x ( 1 ) x^{(1)} x(1), f_wb = w * x[1] + b

對(duì)于大量的數(shù)據(jù)點(diǎn),這可能會(huì)變得笨拙且重復(fù)。 因此,可以在for 循環(huán)中計(jì)算輸出,如下面的函數(shù)compute_model_output 所示。

def compute_model_output(x, w, b):
    """
    Computes the prediction of a linear model
    Args:
      x (ndarray (m,)): Data, m examples 
      w,b (scalar)    : model parameters  
    Returns
      y (ndarray (m,)): target values
    """
    m = x.shape[0]
    f_wb = np.zeros(m)
    for i in range(m):
        f_wb[i] = w * x[i] + b
        
    return f_wb

調(diào)用 compute_model_output 函數(shù)并繪制輸出

w = 100
b = 100

tmp_f_wb = compute_model_output(x_train, w, b,)

# Plot our model prediction
plt.plot(x_train, tmp_f_wb, c='b',label='Our Prediction')

# Plot the data points
plt.scatter(x_train, y_train, marker='x', c='r',label='Actual Values')

# Set the title
plt.title("Housing Prices")
# Set the y-axis label
plt.ylabel('Price (in 1000s of dollars)')
# Set the x-axis label
plt.xlabel('Size (1000 sqft)')
plt.legend()
plt.show()

【機(jī)器學(xué)習(xí)】Linear Regression,機(jī)器學(xué)習(xí),機(jī)器學(xué)習(xí),線性回歸,人工智能
很明顯, w = 100 w = 100 w=100 b = 100 b = 100 b=100 不會(huì)產(chǎn)生適合數(shù)據(jù)的直線。

根據(jù)學(xué)過(guò)的數(shù)學(xué)知識(shí),可以容易求出 w = 200 w = 200 w=200 b = 100 b = 100 b=100

5、預(yù)測(cè)

現(xiàn)在我們已經(jīng)有了一個(gè)模型,可以用它來(lái)做出房屋價(jià)格的預(yù)測(cè)。來(lái)預(yù)測(cè)一下 1200 平方英尺的房子的價(jià)格。由于面積單位為 1000 平方英尺,所以 x x x 是1.2。

w = 200                         
b = 100    
x_i = 1.2
cost_1200sqft = w * x_i + b    

print(f"${cost_1200sqft:.0f} thousand dollars")

輸出的結(jié)果是:$340 thousand dollars

總結(jié)

  • 線性回歸建立一個(gè)特征和目標(biāo)之間關(guān)系的模型
    • 在上面的例子中,特征是房屋面積,目標(biāo)是房?jī)r(jià)。
    • 對(duì)于簡(jiǎn)單線性回歸,模型有兩個(gè)參數(shù) w w w b b b ,其值使用訓(xùn)練數(shù)據(jù)進(jìn)行擬合。
    • 一旦確定了模型的參數(shù),該模型就可以用于對(duì)新數(shù)據(jù)進(jìn)行預(yù)測(cè)。

附錄

deeplearning.mplstyle 源碼:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-624117.html

# see https://matplotlib.org/stable/tutorials/introductory/customizing.html
lines.linewidth: 4
lines.solid_capstyle: butt

legend.fancybox: true

# Verdana" for non-math text,
# Cambria Math

#Blue (Crayon-Aqua) 0096FF
#Dark Red C00000
#Orange (Apple Orange) FF9300
#Black 000000
#Magenta FF40FF
#Purple 7030A0

axes.prop_cycle: cycler('color', ['0096FF', 'FF9300', 'FF40FF', '7030A0', 'C00000'])
#axes.facecolor: f0f0f0 # grey
axes.facecolor: ffffff  # white
axes.labelsize: large
axes.axisbelow: true
axes.grid: False
axes.edgecolor: f0f0f0
axes.linewidth: 3.0
axes.titlesize: x-large

patch.edgecolor: f0f0f0
patch.linewidth: 0.5

svg.fonttype: path

grid.linestyle: -
grid.linewidth: 1.0
grid.color: cbcbcb

xtick.major.size: 0
xtick.minor.size: 0
ytick.major.size: 0
ytick.minor.size: 0

savefig.edgecolor: f0f0f0
savefig.facecolor: f0f0f0

#figure.subplot.left: 0.08
#figure.subplot.right: 0.95
#figure.subplot.bottom: 0.07

#figure.facecolor: f0f0f0  # grey
figure.facecolor: ffffff  # white

## ***************************************************************************
## * FONT                                                                    *
## ***************************************************************************
## The font properties used by `text.Text`.
## See https://matplotlib.org/api/font_manager_api.html for more information
## on font properties.  The 6 font properties used for font matching are
## given below with their default values.
##
## The font.family property can take either a concrete font name (not supported
## when rendering text with usetex), or one of the following five generic
## values:
##     - 'serif' (e.g., Times),
##     - 'sans-serif' (e.g., Helvetica),
##     - 'cursive' (e.g., Zapf-Chancery),
##     - 'fantasy' (e.g., Western), and
##     - 'monospace' (e.g., Courier).
## Each of these values has a corresponding default list of font names
## (font.serif, etc.); the first available font in the list is used.  Note that
## for font.serif, font.sans-serif, and font.monospace, the first element of
## the list (a DejaVu font) will always be used because DejaVu is shipped with
## Matplotlib and is thus guaranteed to be available; the other entries are
## left as examples of other possible values.
##
## The font.style property has three values: normal (or roman), italic
## or oblique.  The oblique style will be used for italic, if it is not
## present.
##
## The font.variant property has two values: normal or small-caps.  For
## TrueType fonts, which are scalable fonts, small-caps is equivalent
## to using a font size of 'smaller', or about 83%% of the current font
## size.
##
## The font.weight property has effectively 13 values: normal, bold,
## bolder, lighter, 100, 200, 300, ..., 900.  Normal is the same as
## 400, and bold is 700.  bolder and lighter are relative values with
## respect to the current weight.
##
## The font.stretch property has 11 values: ultra-condensed,
## extra-condensed, condensed, semi-condensed, normal, semi-expanded,
## expanded, extra-expanded, ultra-expanded, wider, and narrower.  This
## property is not currently implemented.
##
## The font.size property is the default font size for text, given in points.
## 10 pt is the standard value.
##
## Note that font.size controls default text sizes.  To configure
## special text sizes tick labels, axes, labels, title, etc., see the rc
## settings for axes and ticks.  Special text sizes can be defined
## relative to font.size, using the following values: xx-small, x-small,
## small, medium, large, x-large, xx-large, larger, or smaller


font.family:  sans-serif
font.style:   normal
font.variant: normal
font.weight:  normal
font.stretch: normal
font.size:    8.0

font.serif:      DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif
font.sans-serif: Verdana, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
font.cursive:    Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, Comic Neue, Comic Sans MS, cursive
font.fantasy:    Chicago, Charcoal, Impact, Western, Humor Sans, xkcd, fantasy
font.monospace:  DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace


## ***************************************************************************
## * TEXT                                                                    *
## ***************************************************************************
## The text properties used by `text.Text`.
## See https://matplotlib.org/api/artist_api.html#module-matplotlib.text
## for more information on text properties
#text.color: black

到了這里,關(guān)于【機(jī)器學(xué)習(xí)】Linear Regression的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • [github-100天機(jī)器學(xué)習(xí)]day3 multiple linear regression--代碼版本問(wèn)題已修正

    [github-100天機(jī)器學(xué)習(xí)]day3 multiple linear regression--代碼版本問(wèn)題已修正

    https://github.com/LiuChuang0059/100days-ML-code/blob/master/Day3_Multiple_Linear_regression/README.md 一個(gè)線性方程適配觀測(cè)數(shù)據(jù),建立特征和響應(yīng)之間的關(guān)系。可以用來(lái)找出預(yù)測(cè)結(jié)果在哪個(gè)因素影響最大 ,不同變量如何關(guān)聯(lián)。 y = b 0 + b 1 x 1 + b 2 x 2 + . . . + b n x n y = b_0+b_1x_1+b_2x_2+...+b_nx_n y = b 0 ?

    2024年02月12日
    瀏覽(21)
  • 解密人工智能:線性回歸 | 邏輯回歸 | SVM

    解密人工智能:線性回歸 | 邏輯回歸 | SVM

    前些天發(fā)現(xiàn)了一個(gè)巨牛的人工智能學(xué)習(xí)網(wǎng)站,通俗易懂,風(fēng)趣幽默,忍不住分享一下給大家。點(diǎn)擊跳轉(zhuǎn)到網(wǎng)站。 機(jī)器學(xué)習(xí)算法是一種基于數(shù)據(jù)和經(jīng)驗(yàn)的算法,通過(guò)對(duì)大量數(shù)據(jù)的學(xué)習(xí)和分析,自動(dòng)發(fā)現(xiàn)數(shù)據(jù)中的模式、規(guī)律和關(guān)聯(lián),并利用這些模式和規(guī)律來(lái)進(jìn)行預(yù)測(cè)、分類(lèi)或優(yōu)化

    2024年01月20日
    瀏覽(99)
  • 機(jī)器學(xué)習(xí):邏輯回歸(Logistic Regression)

    Logistic Regression. 本文目錄: 邏輯回歸模型 Logistic函數(shù) 交叉熵?fù)p失 梯度下降法 核邏輯回歸 邏輯回歸(logistic regression) 是一種二分類(lèi)模型,其思想是通過(guò)引入一個(gè)函數(shù)將線性回歸的輸出限制在 [ 0 , 1 ] [0,1] [ 0 , 1

    2024年02月09日
    瀏覽(21)
  • 機(jī)器學(xué)習(xí)11:邏輯回歸-Logistic Regression

    目錄 1.計(jì)算概率 2.損失和正則化 2.1 邏輯回歸的損失函數(shù) 2.2 邏輯回歸中的正則化 3.參考文獻(xiàn)

    2024年02月11日
    瀏覽(29)
  • 人工智能基礎(chǔ)_機(jī)器學(xué)習(xí)003_有監(jiān)督機(jī)器學(xué)習(xí)_sklearn中線性方程和正規(guī)方程的計(jì)算_使用sklearn解算八元一次方程---人工智能工作筆記0042

    人工智能基礎(chǔ)_機(jī)器學(xué)習(xí)003_有監(jiān)督機(jī)器學(xué)習(xí)_sklearn中線性方程和正規(guī)方程的計(jì)算_使用sklearn解算八元一次方程---人工智能工作筆記0042

    然后我們?cè)賮?lái)看看,如何使用sklearn,來(lái)進(jìn)行正規(guī)方程的運(yùn)算,當(dāng)然這里 首先要安裝sklearn,這里如何安裝sklearn就不說(shuō)了,自己查一下 首先我們還是來(lái)計(jì)算前面的八元一次方程的解,但是這次我們不用np.linalg.solve這個(gè) 解線性方程的方式,也不用 直接 解正規(guī)方程的方式: 也就是上面這種

    2024年02月08日
    瀏覽(29)
  • 一文詳解人工智能:線性回歸、邏輯回歸和支持向量機(jī)(SVM)

    在人工智能領(lǐng)域,線性回歸、邏輯回歸和支持向量機(jī)是常見(jiàn)的機(jī)器學(xué)習(xí)算法。本文將詳細(xì)介紹這三種算法的原理和應(yīng)用,并提供相應(yīng)的代碼示例。 線性回歸是一種用于建立變量之間線性關(guān)系的回歸分析方法。它通過(guò)擬合一個(gè)線性模型來(lái)預(yù)測(cè)連續(xù)變量的值。線性回歸的目標(biāo)是找

    2024年02月03日
    瀏覽(47)
  • 人工智能-線性回歸的從零開(kāi)始實(shí)現(xiàn)

    人工智能-線性回歸的從零開(kāi)始實(shí)現(xiàn)

    在了解線性回歸的關(guān)鍵思想之后,我們可以開(kāi)始通過(guò)代碼來(lái)動(dòng)手實(shí)現(xiàn)線性回歸了。 在這一節(jié)中,我們將從零開(kāi)始實(shí)現(xiàn)整個(gè)方法, 包括數(shù)據(jù)流水線、模型、損失函數(shù)和小批量隨機(jī)梯度下降優(yōu)化器。 雖然現(xiàn)代的深度學(xué)習(xí)框架幾乎可以自動(dòng)化地進(jìn)行所有這些工作,但從零開(kāi)始實(shí)現(xiàn)

    2024年02月08日
    瀏覽(28)
  • 【深入探究人工智能】邏輯函數(shù)|線性回歸算法|SVM

    【深入探究人工智能】邏輯函數(shù)|線性回歸算法|SVM

    ??博客主頁(yè):小智_x0___0x_ ??歡迎關(guān)注:??點(diǎn)贊??收藏??留言 ??系列專(zhuān)欄:小智帶你閑聊 ??代碼倉(cāng)庫(kù):小智的代碼倉(cāng)庫(kù) 機(jī)器學(xué)習(xí)算法是一種基于數(shù)據(jù)和經(jīng)驗(yàn)的算法,通過(guò)對(duì)大量數(shù)據(jù)的學(xué)習(xí)和分析,自動(dòng)發(fā)現(xiàn)數(shù)據(jù)中的模式、規(guī)律和關(guān)聯(lián),并利用這些模式和規(guī)律來(lái)進(jìn)行預(yù)測(cè)

    2024年02月08日
    瀏覽(93)
  • 【人工智能】簡(jiǎn)單線性回歸模型介紹及python實(shí)現(xiàn)

    【人工智能】簡(jiǎn)單線性回歸模型介紹及python實(shí)現(xiàn)

    簡(jiǎn)單線性回歸是人工智能和統(tǒng)計(jì)學(xué)中一個(gè)基本的預(yù)測(cè)技術(shù),用于分析兩個(gè)連續(xù)變量之間的線性關(guān)系。在簡(jiǎn)單線性回歸中,我們?cè)噲D找到一個(gè)線性方程來(lái)最好地描述這兩個(gè)變量之間的關(guān)系。 變量 :簡(jiǎn)單線性回歸涉及兩個(gè)變量 - 自變量(independent variable)和因變量(dependent vari

    2024年01月17日
    瀏覽(20)
  • 【人工智能】多元線性回歸模型舉例及python實(shí)現(xiàn)方式

    【人工智能】多元線性回歸模型舉例及python實(shí)現(xiàn)方式

    比如你做了一個(gè)企業(yè)想要招人,但是不知道月薪應(yīng)該定在多少,你做了一個(gè)月薪和收入的調(diào)研,包括年限、學(xué)歷、地區(qū)和月薪 做一個(gè)月薪=w1 年限+w2 學(xué)歷+w3*城市+…+b的工作年限和薪資的多元線性模型,然后找出最適合線性模型的直線-成本函數(shù)、梯度下降方式,來(lái)預(yù)估你可以

    2024年02月19日
    瀏覽(28)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包