linear Regression/k-Nearest Neighbors regression ML model by python

14 篇文章 0 订阅

Learned by the book Hands-On Machine Learning with Scikit-Learn and TensorFlow,chapter 1.It is a linearRegression ML model demo code.
The relate datasets are below:
handson-ml/datasets/lifesat/
The code below:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import linear_model


def prepare_country_stats(oecd_bli, gdp_per_capita):
    return sample_data

#load the data
oecd_bli = pd.read_csv('oecd_bli_2015.csv',thousands=',')
#filter the items whose 'INQUALITY' is 'TOT'
oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
#reshape the dataset,row ='Country',col='Indicator',values='Value'
oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")

gdp_per_capita = pd.read_csv('gdp_per_capita.csv',thousands=',',delimiter='\t',encoding='latin1',na_values='n/a')
#rename the col '2015' to 'GDP per capita'
gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
gdp_per_capita.set_index("Country", inplace=True)
#merge the two dataset,index  is same: 'Country'
full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita, left_index=True, right_index=True)
full_country_stats.sort_values(by="GDP per capita", inplace=True)

#seperate the dataset to two parts
remove_indices = [0, 1, 6, 8, 33, 34, 35]
keep_indices = list(set(range(36)) - set(remove_indices))

sample_data = full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]
missing_data = full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[remove_indices]

#prepare the data
country_stats = prepare_country_stats(oecd_bli,gdp_per_capita)
x = np.c_[country_stats['GDP per capita']]
y = np.c_[country_stats['Life satisfaction']]

#visualize the data
country_stats.plot(kind='scatter',x='GDP per capita',y='Life satisfaction')
plt.show()

#select a linear model
lin_reg_model = linear_model.LinearRegression()
#train the model
lin_reg_model.fit(x,y)

#make a prediction of a country's satisfaction with the GDP
print(lin_reg_model.predict([[9000]]))

You can see the chart and the prediction result:
这里写图片描述

Also you can use the way of k-Nearest Neighbors regression to predict the satisfaction value.just replace:

clf = sklearn.linear_model.LinearRegression()

to:

#the value of neighbors here is 3
clf = sklearn.neighbors.KNeighborsRegressor(n_neighbors=3)

It will choose the number of the countries whose GDP is closest to the one you want to predict(here is 9000).Then compute the average value which will be the prediction result.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值