import pandas as pd
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from IPython.display import Image
import warnings
warnings.filterwarnings('ignore')
#内嵌画图
%matplotlib inline
#这些数据的背后意义:某个妹纸想要找一个男朋友,她收集了婚恋网上面对她感兴趣男士的一些数据,
#第一个是一年的飞行里程,第二个是每年玩游戏的时间(10小时为单位),第三个是每年吃的冰淇淋量。
#第四个是结果:3表示很感兴趣,2表示感兴趣,1表示没兴趣。
data = pd.read_csv('data.csv')
print (data.shape)
data_change = data.rename(columns = {'Column1': 'fly','Column2':'game','Column3':'ice-cream','Column4': 'result'})
print (data_change.head())
'''
for col in np.take(data_change.columns,[0,1,2]):
data_change[col] /= data_change[col].max()
data_change.head()
'''
(691, 4) fly game ice-cream result 0 40920 8.326976 0.953952 3 1 14488 7.153469 1.673904 2 2 26052 1.441871 0.805124 1 3 75136 13.147394 0.428964 1 4 38344 1.669788 0.134296 1
'\nfor col in np.take(data_change.columns,[0,1,2]):\n data_change[col] /= data_change[col].max()\ndata_change.head()\n'
y = data_change.iloc[0:50, 3].values
X = data_change.iloc[0:50, [0,1,2]].values
#x_a = np.array(X)
#print (x_a)
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
#ax.view_init(0, -15)
ax.scatter(X[y == 3, 0], X[y == 3, 1], X[y == 3, 2], s=10, c='g')
ax.scatter(X[y == 2, 0], X[y == 2, 1], X[y == 2, 2], s=10, c='r')
ax.scatter(X[y == 1, 0], X[y == 1, 1], X[y == 1, 2], s=10, c='b')
plt.xlabel('fly')
plt.ylabel('game')
#plt.zlabel('ice')
Text(0.5, 0, 'game')