数据:房价预测
绘图方式:二维 绘图库:seaborn
%matplotlib inline
import pandas as pd
import seaborn as sns
#import matplotlib.pyplot as pl
sns.set(context= "notebook", style= "whitegrid", palette="dark")
df0 = pd.read_csv('data0.csv', names = ['square', 'price'])
sns.lmplot('square', 'price', df0, height = 6, fit_reg = True)
绘图方式:三维 绘图库:matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
df1 = pd.read_csv('data1.csv', names = ['square', 'bedrooms', 'price'])
df1.head()
%matplotlib inline
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_xlabel('square')
ax.set_ylabel('bedrooms')
ax.set_zlabel('price')
#绘制3D散点图
ax.scatter3D(df1['square'], df1['bedrooms'], df1['price'], c = df1['price'], cmap ="Greens")
fangjia2.JPG
def normalize_feature(df):
return df.apply(lambda column: (column - column.mean()) / column.std())
df = normalize_feature(df1)
ax = plt.axes(projection = '3d')
ax.set_xlabel('square')
ax.set_ylabel('bedrooms')
ax.set_zlabel('price')
ax.scatter3D(df['square'], df['bedrooms'], df['price'], c = df['price'], cmap = 'Reds')
import numpy as np
ones = pd.DataFrame({'ones': np.ones(len(df))})#ones是n行1列的数据框,表示x0恒为1
df = pd.concat([ones, df], axis = 1) #根据列合并数据
#df.head()
newdata.JPG
网友评论