--------Regresion_with_pandas.py---------------------------------------------------
#! /usr/bin/python
# coding: UTF-8
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
#main function
if __name__ == "__main__":
#-----Read CSV file with Pandas-----
df = pd.read_csv("cars.csv")
x_data = np.array(df["speed"])
y_data = np.array(df["dist"])
x_data = x_data.reshape(len(x_data),1)
y_data = y_data.reshape(len(y_data),1)
#-----Liner Regression--------------
#lm = linear_model.LinearRegression()
lm = Pipeline([('poly', PolynomialFeatures(degree=3)),('linear', LinearRegression())])
lm.fit(x_data, y_data)
y_pred= lm.predict(x_data)
#-----Plot data -------------------
plt.title("Cars : speed vs distance")
plt.xlabel("Speed")
plt.xlim([0,30])
plt.ylabel("Distance")
plt.ylim([-20,140])
plt.plot(x_data, y_data,"ro")
plt.plot(x_data, y_pred)
plt.show()
--------プログラム実行に必要なCSVファイル------------------------
cars.csv
--------結果---------------------------------------------------
0 件のコメント:
コメントを投稿