import pandas as pd
import seaborn as sns
sns.set(context = "notebook", style = "whiteqrid", palette = "dark")
df0 = pd.read_csv('data0.csv', names = ['square' , 'price' ])
sns.lmplot('square' , 'price', df0, height = 6, fit_req = True)
from mpl_toolkits import mplot3d
import pandas as pd
import matplotlib.pyplot as plt
df1 = pd.read_csv('data0.csv', names = ['square' , 'bedroom' , 'price' ])
df1.head()
fig = plt.figure()
ax = plt.axes(projection='3d')//设置为3D格式
ax.set_xlabel('square')
ax.set_ylabel('bedroom')
ax.set_zlabel('price')
ax.scatter3D(df1['square'] , df1['bedroom'] , df1['price'] , c = df1[ 'price' ] , cmap='Greens')//前三个参数为设置坐标,c表示关于哪一个参数颜色的深浅有变化,cmap表示设置颜色
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['bedroooms'], df['price'],c=df['price'], cmap='Reds')
NumPy是一个BSD开源协议许可的,面向Python用户的基础科学计算库,在多维数组上实现了线性代数、傅立叶变换和其他丰富的函数运算。
import pandas as pd
import numpy as np
def normalize_feature(df):
return df.apply(lambda column: (column - column.mean() / column.std()))
df = normalize_feature(pd.read_csv('datal.csv', names=['square', 'bedroom', 'price']))
ones = pd.DataFrame({'ones': np.ones(len(df))})#ones是n行1列的数据框,表示x0恒为1
df = pd.concat([ones,df], axis=1)#根据列合并数据
X_data = np.array(df[df.columns[0:3]])#取的0-2列,即前三列;左闭右开的区间
y_data = np.array(df[df.column[-1]]).reshape(len(df), 1)#-1就是最后一列
print(X_data.shape, type(X_data))
print(y_data.shape, type(y_data))