Você está na página 1de 9

EJ1SEMANA2ML

November 4, 2022

[1]: Machine Learning IPN - Trabajo 15 marzo 2021


Andres Nares Monroy

File "<ipython-input-1-7fb2e8e0bee3>", line 1


Machine Learning IPN - Trabajo 15 marzo 2021
^
SyntaxError: invalid syntax

[ ]:

[4]: import pandas as pd


#white_data = pd.read_csv("C:
,→\\Users\ASTRONAUTA\Documents\ATOM\winequality\winequality-white.csv", sep=";

,→")

#toefl = pd.read_csv("C:\\Users\ASTRONAUTA\Documents\admission_predic.csv")

[5]: #Cargar los DATOS

import pandas as pd
toefl = pd.read_csv("C:\\Users\ASTRONAUTA\Downloads\data.csv")
toefl.head(10)

[5]: Serial No. GRE Score TOEFL Score University Rating SOP LOR CGPA \
0 1 337 118 4 4.5 4.5 9.65
1 2 324 107 4 4.0 4.5 8.87
2 3 316 104 3 3.0 3.5 8.00
3 4 322 110 3 3.5 2.5 8.67
4 5 314 103 2 2.0 3.0 8.21
5 6 330 115 5 4.5 3.0 9.34
6 7 321 109 3 3.0 4.0 8.20
7 8 308 101 2 3.0 4.0 7.90
8 9 302 102 1 2.0 1.5 8.00
9 10 323 108 3 3.5 3.0 8.60

Research Chance of Admit

1
0 1 0.92
1 1 0.76
2 1 0.72
3 1 0.80
4 0 0.65
5 1 0.90
6 1 0.75
7 0 0.68
8 0 0.50
9 0 0.45

[6]: #Seleccionar las columnas de trabajo


X = pd.DataFrame({

"x1": toefl["GRE Score"],


"x2": toefl["TOEFL Score"],
"x3": toefl["University Rating"],
"x4": toefl["SOP"],
"x5": toefl["LOR"],
"x6": toefl["CGPA"],
"x7": toefl["Research"]

})

y = toefl["Chance of Admit"]

[7]: m = X.x1
plt.plot(X.x1,y)


,→---------------------------------------------------------------------------

NameError Traceback (most recent call␣


,→ last)

<ipython-input-7-5fe489efdc9b> in <module>
1 m = X.x1
----> 2 plt.plot(X.x1,y)
3

NameError: name 'plt' is not defined

2
[8]: #3. Visualiza cada eje respecto al valor de predicción (3 puntos)

import matplotlib.pyplot as plt


import numpy as np
m = X.x1
fig, axs = plt.subplots(7)
fig.suptitle('Vertically stacked subplots')
axs[0].plot(X.x1, y)
axs[1].plot(X.x2, y)
axs[2].plot(X.x3, y)
axs[3].plot(X.x4, y)
axs[4].plot(X.x5, y)
axs[5].plot(X.x6, y)
axs[6].plot(X.x7, y)

[8]: [<matplotlib.lines.Line2D at 0x184be6e0d60>]

[42]: y

[42]: 0 0.92
1 0.76

3
2 0.72
3 0.80
4 0.65

395 0.82
396 0.84
397 0.91
398 0.67
399 0.95
Name: Chance of Admit, Length: 400, dtype: float64

[36]: #4. Crea un regresor lineal (4 puntos)


import pandas
from sklearn import linear_model
from sklearn.metrics import r2_score
O = X[['x6','x7']]

regr = linear_model.LinearRegression()
regr.fit(X,y)
print(regr.coef_)

[ 0.00173741 0.00291958 0.00571666 -0.00330517 0.02235313 0.11893945


0.02452511]

[33]: y_predict = regr.predict(X)


y_predict
r2_score(y_predict,y )
#df_predict = pd.DataFrame[y_predict]
#predict = pd.DataFrame[[regr.predict(X)]]
#predict

[33]: 0.7554005844257312

[43]: #5. Descarta ejes (2 puntos)


#6. Visualización en 2D (4 puntos)
#7. Visualización en 3D (4 puntos)
#Descartando - x1 - x4
regressor = linear_model.LinearRegression()

regressor.fit(O,y)

v2a_predict = regressor.predict(O)

v2a_predict

print(regressor.coef_)
plt.scatter(X.x1, X.x2, c=v2a_predict)

4
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")

ax.view_init(0, 40)

ax.scatter(X.x6, X.x5, v2a_predict)

[0.19211109 0.03838883]

[43]: <mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x184c91d7ac0>

5
[45]: #EJEMPLOS QUE NO FUNCIONARON

[26]: from sklearn.linear_model import LinearRegression


rs = np.array([X.x6]).reshape((-1,1))
model = LinearRegression().fit(rs,y)
model.score(rs,y)
slope1 = model.coef_
predict1 = model.predict(rs)

print(slope1)
plt.scatter(rs,y)
plt.plot(rs,predict1,c = 'red')

[0.20884723]

[26]: [<matplotlib.lines.Line2D at 0x184c90c5ee0>]

6
[23]: rs2 = np.array([X.x7]).reshape((-1,1))
model2 = LinearRegression().fit(rs2,y)
model2.score(rs2,y)
slope2 = model2.coef_
print(slope2)
predict2 = model.predict(rs2)
#print(predict2)

plt.scatter(rs2,y)
plt.plot(rs2,predict2,color = 'red')

[0.15830218]

[23]: [<matplotlib.lines.Line2D at 0x184c8fe22e0>]

7
[97]: print('hola')

hola

[19]: from mpl_toolkits import mplot3d


import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')

ax.scatter(X.x6 ,X.x7 ,y , color = 'green' )

[19]: <mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x19b2ab09d60>

8
[ ]:

Você também pode gostar