<jupyter_start><jupyter_text>Prediction Final grade of students based on various factors.* address, internet use, alcohol consumption, romantic life, etc Prepared by <NAME><jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
sns.set_style('darkgrid')
%matplotlib inline
student=pd.read_csv('student-por.csv',sep=';')
student.head()
student.Fjob.unique()
student.dtypes
student=student.dropna()
plt.figure(1,figsize=(8,6))
sns.countplot(x='sex',data=student,palette='Set2',hue='internet')
plt.title('Count plot how many students use internet based on sex')
plt.show()
plt.figure(1,figsize=(12,8))
sns.violinplot(x='sex',y='G3',data=student,hue='address',split=True,palette='Set3')
plt.title('Score distribution based on sex and address')
plt.show()
student.columns
student.shape
plt.figure(1,figsize=(18,8))
sns.violinplot(x='sex',y='G3',data=student,hue='absences')
plt.title('score distribution based on bunk of classes')
plt.show()
plt.figure(1,figsize=(22,8))
sns.heatmap(student.corr(),annot=True)
def absent(x):
  if x<=6:
    return 'low'
  elif x>6 and x<=12:
    return 'med'
  elif x>12:
    return 'high'
student.absences=student.absences.apply(absent,convert_dtype=True)
df=student.copy()
features=['school', 'sex', 'address', 'famsize', 'Pstatus', 'Medu', 'Fedu',
       'Mjob', 'Fjob', 'reason', 'guardian', 'traveltime', 'studytime',
       'failures', 'schoolsup', 'famsup', 'paid', 'activities', 'nursery',
       'higher', 'internet', 'romantic', 'famrel', 'freetime', 'goout', 'Dalc',
       'Walc', 'health', 'absences']
features1=['school', 'sex','age', 'address', 'famsize', 'Pstatus', 'Medu', 'Fedu',
       'Mjob', 'Fjob', 'reason', 'guardian', 'traveltime', 'studytime',
       'failures', 'schoolsup', 'famsup', 'paid', 'activities', 'nursery',
       'higher', 'internet', 'romantic', 'famrel', 'freetime', 'goout', 'Dalc',
       'Walc', 'health', 'absences','G1','G2']
df.head()
df.shape[0]
y=df[['G3']].copy()
y.head()
X=df[features1].copy()
X.head()
X=pd.get_dummies(X,columns=features)
X1=df[features1].copy()
X1.head()
X1.shape
from sklearn.tree import DecisionTreeRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from math import sqrt
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=324)
X_train.shape
y_test.shape


regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_prediction = regressor.predict(X_test)
y_prediction
y_test.describe()
RMSE = sqrt(mean_squared_error(y_true = y_test, y_pred = y_prediction))
print(RMSE)<jupyter_output>1.4751369615164729<jupyter_text>So far linear regressor has least RMSE. Linear regressor RMSE=1.4751369615164729<jupyter_code>regressor = DecisionTreeRegressor()
regressor.fit(X_train, y_train)
y_prediction1 = regressor.predict(X_test)
y_prediction1
y_prediction12=pd.DataFrame(y_prediction1,columns=['prediction1'])

y_prediction12.shape
y_prediction2.shape
y_prediction2=pd.DataFrame(y_prediction,columns=['prediction_linear'])
RMSE_D = sqrt(mean_squared_error(y_true = y_test, y_pred = y_prediction1))
print(RMSE_D)<jupyter_output>1.7212758923758906<jupyter_text>So far linear regressor has least RMSE. Decision tress regressor RMSE=1.7212758923758906<jupyter_code>y_test.shape
X_test1.head()
X_test1=X_test.copy()
X_test1['score']=(X_test['G1']+X_test['G2'])/2
sns.set_style('darkgrid')
plt.figure(1,figsize=(18,6))
sns.lineplot(x=X_test1.age,y=y_test['G3'],color='red',label='Test data',markers=True,dashes=False,ci=20)
sns.lineplot(x=X_test1.age,y=y_prediction2.prediction_linear,color='blue',label='Predicted linear regresser data',ci=20,)
sns.lineplot(x=X_test1.age,y=y_prediction12.prediction1,color='green',label='Predicted Decision tree data',ci=20)
plt.title("Plotting Prediction data vs Test data to see deviation.")
plt.ylabel('Final Score: G3')
plt.xlim(15,18)
plt.ylim(10,15)
plt.legend()
plt.show()
plt.figure(1,figsize=(20,14))
plt.subplot(231)
sns.boxplot(y='G3',data=y_test,color='skyblue')
plt.title('original test data')
plt.subplot(232)
sns.boxplot(y='prediction1',data=y_prediction12,color='orange')
plt.title('prediction decision regressor')
plt.subplot(233)
sns.boxplot(y='prediction_linear',data=y_prediction2,color='pink')
plt.title('prediction linear regresser')
plt.subplot(234)
sns.violinplot(y='G3',data=y_test,color='orange')
plt.title('original test data')
plt.subplot(235)
sns.violinplot(y='prediction1',data=y_prediction12,color='pink')
plt.title('prediction decision regressor')
plt.subplot(236)
sns.violinplot(y='prediction_linear',data=y_prediction2,color='skyblue')
plt.title('prediction linear regresser')
plt.show()<jupyter_output><empty_output><jupyter_text>So far linear regressor has least RMSE. Linear regressor RMSE=1.4751369615164729 Decision tress regressor RMSE=1.7212758923758906<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16172344689378756, 'fraction_numerical': 0.04408817635270541, 'mean_word_length': 8.561302681992338, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '89748', 'license': 'MIT', 'n_tokens_mistral': 2079, 'n_tokens_neox': 1894, 'n_words': 325, 'path': 'Student_predict_final_score_regression.ipynb', 'repo_name': 'shadab4150/Student-records-analysis-and-Predicting-Final-score', 'size': '4998'}