O‘relly 机器学习实战 基于Scikit-learn 、Keras和Tensorflow 学习记录-第一章

@[TOC](这里写自定义目录标题)

#第一章 机器学习概述

 

案例1.1: 使用Scikit-learn 训练并运行一个线性模型

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.linear_model
import sys
sys.path.append("..")

def prepare_country_stats(oecd_bli, gdp_per_capita):
    oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"]
    oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")
    gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
    #gdp_per_capita.set_index("Country", inplace=True)
    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)
    remove_indices = [0, 1, 6, 8, 33, 34, 35]
    keep_indices = list(set(range(36)) - set(remove_indices))
    return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]

# load dataset
path = r"C:\Users\congy\PycharmProjects\machine_learningProject\datasets"

oecd_bli = pd.read_csv(filepath_or_buffer=path + r"\lifesat\oecd_bli_2015.csv", thousands=',')
# print(oecd_bli)
gdp_per_capita = pd.read_csv(filepath_or_buffer=path +r"\lifesat\gdp_per_capita.csv",delimiter='\t',thousands=',',
                              encoding='ISO-8859-1', na_values="n/a")
print(gdp_per_capita.keys())
# print(gdp_per_capita)

# prepare the data
country_stats = prepare_country_stats(oecd_bli, gdp_per_capita)

# 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
model = sklearn.linear_model.LinearRegression()

# Train the model
model.fit(X, y)

# Make a prediction for Cyprus
X_new = [[22587]]  # Cyprus' GDP per capita
print(model.predict(X_new)) # outputs [[ 5.96242338]]

运行,然后崩溃了:

Traceback (most recent call last):
  File "C:/Users/congy/PycharmProjects/machine_learningProject/LinearRegression/example2.py", line 31, in <module>
    country_stats = prepare_country_stats(oecd_bli, gdp_per_capita)
  File "C:/Users/congy/PycharmProjects/machine_learningProject/LinearRegression/example2.py", line 18, in prepare_country_stats
    return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]
  File "C:\ProgramData\Anaconda3\envs\NLP_task\lib\site-packages\pandas\core\indexing.py", line 879, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\ProgramData\Anaconda3\envs\NLP_task\lib\site-packages\pandas\core\indexing.py", line 1487, in _getitem_axis
    return self._get_list_axis(key, axis=axis)
  File "C:\ProgramData\Anaconda3\envs\NLP_task\lib\site-packages\pandas\core\indexing.py", line 1472, in _get_list_axis
    raise IndexError("positional indexers are out-of-bounds") from err
IndexError: positional indexers are out-of-bounds

修改:

encoding='latin1'
gdp_per_capita.set_index("Country", inplace=True) #不能注释掉
# Prepare the data
country_stats = prepare_country_stats(oecd_bli, gdp_per_capita) #写了两遍......!!
# Code example
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn.linear_model
import os

datapath = os.path.join("datasets", "lifesat", "")


def prepare_country_stats(oecd_bli, gdp_per_capita):
    oecd_bli = oecd_bli[oecd_bli["INEQUALITY"] == "TOT"]
    oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator", values="Value")
    gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True)
    gdp_per_capita.set_index("Country", inplace=True)
    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)
    remove_indices = [0, 1, 6, 8, 33, 34, 35]
    keep_indices = list(set(range(36)) - set(remove_indices))
    return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices]


# Load the data
#oecd_bli = pd.read_csv(datapath + "oecd_bli_2015.csv", thousands=',')
#gdp_per_capita = pd.read_csv(datapath + "gdp_per_capita.csv", thousands=',', delimiter='\t',encoding = 'latin1', na_values = "n/a")
path = r"C:\Users\congy\PycharmProjects\machine_learningProject\datasets"

oecd_bli = pd.read_csv(filepath_or_buffer=path + r"\lifesat\oecd_bli_2015.csv", thousands=',')
# print(oecd_bli)
gdp_per_capita = pd.read_csv(filepath_or_buffer=path +r"\lifesat\gdp_per_capita.csv", thousands=',',delimiter='\t',
                              encoding='latin1', na_values= "n/a")

# 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
model = sklearn.linear_model.LinearRegression()

# Train the model
model.fit(X, y)

# Make a prediction for Cyprus
X_new = [[22587]] # Cyprus' GDP per capita
print(model.predict(X_new))  # outputs [[ 5.96242338]]

OK

结果:

Index(['Country', 'Subject Descriptor', 'Units', 'Scale',
       'Country/Series-specific Notes', '2015', 'Estimates Start After'],
      dtype='object')
[[5.96242338]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值