0% found this document useful (0 votes)
7 views14 pages

Dav Py R

The document provides a comprehensive guide on various regression techniques, time series analysis, and text processing using Python and R. It covers simple and multiple linear regression, ARIMA modeling for time series forecasting, and natural language processing techniques such as stopword removal, lemmatization, stemming, and vectorization. Each section includes code examples and visualizations to demonstrate the concepts effectively.

Uploaded by

tanmaygawadegc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Dav Py R

The document provides a comprehensive guide on various regression techniques, time series analysis, and text processing using Python and R. It covers simple and multiple linear regression, ARIMA modeling for time series forecasting, and natural language processing techniques such as stopword removal, lemmatization, stemming, and vectorization. Each section includes code examples and visualizations to demonstrate the concepts effectively.

Uploaded by

tanmaygawadegc
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#Simple linear regression (Salary_experience)

import pandas as pd

import seaborn as sns

import [Link] as plt

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

from [Link] import mean_squared_error, r2_score

# Load data

df = pd.read_csv("D:\FAMT\CSE 6SEM\DAV\[Link]")

X = df[['YearsExperience']]

y = df['Salary']

# Train-test split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

model = LinearRegression().fit(X_train, y_train)

# Predict & Evaluate

y_pred = [Link](X_test)

print("Coefficients:", model.coef_)

print("Intercept:", model.intercept_)

print("R^2 Score:", r2_score(y_test, y_pred))

# Visualization

[Link](x='YearsExperience', y='Salary', data=df, ci=None)

[Link]('Simple Linear Regression: Salary vs Experience')

[Link]()
#Multiple Linear Regression in Python(Student_Performace Dataset)
import pandas as pd

import seaborn as sns

import [Link] as plt

from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

from [Link] import r2_score

# Correct file path

df = pd.read_csv("D:\FAMT\CSE 6SEM\DAV\Student_Performance.csv")

# Encode categorical column

df['Extracurricular Activities'] = df['Extracurricular Activities'].map({'Yes': 1, 'No': 0})

# Features and target

X = df[['Hours Studied', 'Previous Scores', 'Extracurricular Activities', 'Sleep Hours', 'Sample Question
Papers Practiced']]

y = df['Performance Index']

# Train-test split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train model

model = LinearRegression().fit(X_train, y_train)

# Predictions

y_pred = [Link](X_test)

print("Coefficients:", model.coef_)

print("R^2 Score:", r2_score(y_test, y_pred))


# Visualization

[Link](df, x_vars=[Link], y_vars='Performance Index', kind='reg', height=4)

[Link]('Multiple Linear Regression: Student Performance', y=1.02)

[Link]()

#Time Series Analysis to find trend,seasonality,cycle,stationary test in Python(Monthly_Value


Dataset)

import pandas as pd

import [Link] as plt

import seaborn as sns

from [Link] import seasonal_decompose

from [Link] import adfuller

# Load dataset

df = pd.read_csv(r"D:\FAMT\CSE 6SEM\DAV\Month_Value_1.csv")

# Convert 'Period' to datetime and set as index

df['Period'] = pd.to_datetime(df['Period'])

df.set_index('Period', inplace=True)

# Handle missing values in Revenue

df['Revenue'] = df['Revenue'].fillna(method='ffill') # or use .dropna() if preferred

# Plot Revenue time series

[Link](figsize=(10, 5))

[Link](data=df['Revenue'])

[Link]('Monthly Revenue Time Series')

[Link]("Period")

[Link]("Revenue")

plt.tight_layout()
[Link]()

# Seasonal decomposition

decomp = seasonal_decompose(df['Revenue'], model='additive', period=12)

[Link]()

plt.tight_layout()

[Link]()

# Augmented Dickey-Fuller test

adf_result = adfuller(df['Revenue'])

print("ADF Statistic:", adf_result[0])

print("p-value:", adf_result[1])

# Implement ARIMA model to find values of (p,d,q) to predict 20 future values.(Monthly_Value


Dataset)
import pandas as pd

import numpy as np

import [Link] as plt

import seaborn as sns

from [Link] import adfuller

from [Link] import plot_acf, plot_pacf

from [Link] import ARIMA

import warnings

[Link]("ignore")

df=pd.read_csv("/content/Month_Value_1.csv")

[Link](20)

# Convert 'Period' to datetime and set as index

df['Period'] = pd.to_datetime(df['Period'], format='%d.%m.%Y')

df.set_index('Period', inplace=True)

df = df.sort_index()
# Interpolate missing values

df['Revenue'] = df['Revenue'].interpolate(method='time')

# Plot Revenue over time

[Link](figsize=(12, 6))

[Link](x=[Link], y=df['Revenue'])

[Link]('Revenue Over Time')

[Link]('Date')

[Link]('Revenue')

[Link](True)

[Link]()

# ADF Test for stationarity

def adf_test(series):

result = adfuller(series)

print(f"ADF Statistic: {result[0]}")

print(f"p-value: {result[1]}")

print("Critical Values:")

for key, value in result[4].items():

print(f" {key}: {value}")

if result[1] <= 0.05:

print("The series is stationary.")

else:

print("The series is NOT stationary.")

print("ADF Test on Original Data")

adf_test(df['Revenue'])

# Differencing to make data stationary

df['Revenue_diff'] = df['Revenue'].diff()

df = [Link]()

print("\nADF Test on Differenced Data")

adf_test(df['Revenue_diff'])
# ACF and PACF plots

fig, ax = [Link](1, 2, figsize=(14, 5))

plot_acf(df['Revenue_diff'], ax=ax[0])

ax[0].set_title("Autocorrelation Function (ACF)")

plot_pacf(df['Revenue_diff'], ax=ax[1])

ax[1].set_title("Partial Autocorrelation Function (PACF)")

[Link]()

# Fit ARIMA Model (p=2, d=1, q=1)

model = ARIMA(df['Revenue'], order=(2, 1, 1))

model_fit = [Link]()

print(model_fit.summary())

# Forecast for next 12 months

forecast_steps = 12

forecast = model_fit.forecast(steps=forecast_steps)

# Generate future dates

future_dates = pd.date_range(start=[Link][-1], periods=forecast_steps + 1, freq='MS')[1:]

forecast_df = [Link]({'Date': future_dates, 'Forecasted_Revenue': [Link]})

# Plot Forecast

[Link](figsize=(12, 6))

[Link]([Link], df['Revenue'], label="Actual Revenue", color='blue')

[Link](forecast_df['Date'], forecast_df['Forecasted_Revenue'], label="Forecasted Revenue", color='red',


linestyle='dashed')

[Link]("ARIMA Forecast for Revenue")

[Link]("Date")

[Link]("Revenue")

[Link]()

[Link](True)

[Link]()
#Python program to [Link] stopwords,[Link],[Link],[Link] punctuations5.
Bow [Link]

import nltk

import string

from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer

from [Link] import stopwords

from [Link] import WordNetLemmatizer, PorterStemmer

# Download necessary NLTK datasets

[Link]('stopwords')

[Link]('punkt')

[Link]('wordnet')

# Sample text data

text_data = ["Artificial intelligence (AI) involves creating intelligent agents, which are systems that can
reason, learn, and act autonomously. AI research draws upon computer science, mathematics, psychology,
neuroscience, and other fields.."]

# 1. Remove stopwords

stop_words = set([Link]('english'))

def remove_stopwords(text):

words = [Link]()

return ' '.join([word for word in words if [Link]() not in stop_words])

# Remove stopwords for each text in the dataset

stopwords_removed_data = [remove_stopwords(text) for text in text_data]

print("After Removing Stopwords:")

print(stopwords_removed_data)

# 2. Lemmatization

lemmatizer = WordNetLemmatizer()

def lemmatize_text(text):

words = [Link]()

return ' '.join([[Link](word) for word in words])

# Apply Lemmatization

lemmatized_data = [lemmatize_text(text) for text in stopwords_removed_data]

print("\nAfter Lemmatization:")

print(lemmatized_data)
# 3. Stemming

stemmer = PorterStemmer()

def stem_text(text):

words = [Link]()

return ' '.join([[Link](word) for word in words])

# Apply Stemming

stemmed_data = [stem_text(text) for text in lemmatized_data]

print("\nAfter Stemming:")

print(stemmed_data)

# 4. Remove punctuation

def remove_punctuation(text):

return [Link]([Link]('', '', [Link]))

# Apply Punctuation Removal

punctuation_removed_data = [remove_punctuation(text) for text in stemmed_data]

print("\nAfter Removing Punctuation:")

print(punctuation_removed_data)

# 5. Bag of Words (BoW)

vectorizer = CountVectorizer()

def get_bow_representation(data):

return vectorizer.fit_transform(data)

# Get BoW Representation

bow_data = get_bow_representation(punctuation_removed_data)

print("\nBag of Words (BoW) Representation:")

print(bow_data.toarray()) # Display BoW as array

# 6. TF-IDF (Term Frequency-Inverse Document Frequency)

tfidf_vectorizer = TfidfVectorizer()

def get_tfidf_representation(data):

return tfidf_vectorizer.fit_transform(data)

# Get TF-IDF Representation

tfidf_data = get_tfidf_representation(punctuation_removed_data)

print("\nTF-IDF Representation:")

print(tfidf_data.toarray()) # Display TF-IDF as array


#Simple Linear Regression in R(Salary_Experience Dataset)(IN R YOU HAVE TO PROMPT)

# Load necessary library


library(ggplot2)

# Read the dataset


df <- [Link]("D:\\FAMT\\CSE 6SEM\\DAV\\[Link]")

# View data
head(df)
str(df)

# Set seed for reproducibility


[Link](123)

# Split data into training (80%) and testing (20%)


sample_index <- sample(1:nrow(df), 0.8 * nrow(df))
train_data <- df[sample_index, ]
test_data <- df[-sample_index, ]

# Build the linear model using training data


model <- lm(Salary ~ YearsExperience, data = train_data)

# Model summary
summary(model)

# Plot with training data


ggplot(train_data, aes(x = YearsExperience, y = Salary)) +
geom_point(color = "blue", size = 3) +
geom_smooth(method = "lm", se = TRUE, color = "red") +
labs(
title = "Salary vs Years of Experience (Training Data)",
x = "Years of Experience",
y = "Salary"
)

# (Optional) Predict on test data


predicted_salary <- predict(model, newdata = test_data)

# (Optional) Combine actual and predicted for viewing


results <- [Link](
Actual = test_data$Salary,
Predicted = predicted_salary
)

print(results)
# Multiple Linear Regression in R(Student_Performace Dataset)

# Load libraries
library(ggplot2)
library(caTools)

# Read your dataset


df <- [Link]("D:\\FAMT\\CSE 6SEM\\DAV\\Student_Performance.csv")

# View structure and sample rows


str(df)
head(df)

# Check for missing values


colSums([Link](df))

# Summary
summary(df)

# Train-test split
[Link](42)
split <- [Link](df$[Link], SplitRatio = 0.6)
trainData <- subset(df, split == TRUE)
testData <- subset(df, split == FALSE)

# Multiple Linear Regression model


model <- lm([Link] ~ [Link] + [Link] + [Link] +
[Link] + [Link], data = trainData)

# Model summary
summary(model)

# Predict on test data


testData$Predicted_Performance <- predict(model, testData)

# Evaluation
rmse <- sqrt(mean((testData$[Link] - testData$Predicted_Performance)^2))
r_squared <- summary(model)$[Link]
cat("RMSE:", rmse, "\n")
cat("R-squared:", r_squared, "\n")

# Plot: Actual vs Predicted (displayed in RStudio)


ggplot(testData, aes(x = [Link], y = Predicted_Performance)) +
geom_point(color = "blue", alpha = 0.6) +
geom_abline(intercept = 0, slope = 1, color = "red", linetype = "dashed") +
labs(title = "Actual vs Predicted Performance Index",
x = "Actual Performance Index", y = "Predicted Performance Index")
#Time Series Analysis to find trend,seasonality,cycle,stationary test in R(Monthly_Value Dataset)
# Load necessary libraries
library(tseries)
library(forecast)
library(ggplot2)

# Load the dataset


df <- [Link]("D:\\FAMT\\CSE 6SEM\\DAV\\Month_Value_1.csv")

# View structure and first few rows


str(df)
head(df)

# Check if there are any missing values in 'Revenue'


sum([Link](df$Revenue))
# If there are missing values, print out the rows with NA values
missing_rows <- df[[Link](df$Revenue), ]
print(missing_rows)

# Ensure the 'Revenue' column is numeric


df$Revenue <- [Link](df$Revenue)

# Check again for missing values after conversion


sum([Link](df$Revenue))

# Remove rows with NA in the 'Revenue' column


df <- [Link](df)

# Now, create the time series object


if(nrow(df) > 0) {
# Assuming the data is monthly, starting from January 2015
ts_data <- ts(df$Revenue, start=c(2015, 1), frequency=12)

# Plot the time series data


plot(ts_data, main="Monthly Revenue Time Series", xlab="Period", ylab="Revenue", col="blue")

# Decompose the time series (Trend, Seasonal, Random)


decomp <- decompose(ts_data)
plot(decomp)

# Trend, Seasonality, and Residuals (Random) separately


plot(decomp$trend, main="Trend Component", col="blue")
plot(decomp$seasonal, main="Seasonal Component", col="green")
plot(decomp$random, main="Residuals (Random)", col="red")

# Stationarity test: Augmented Dickey-Fuller Test


adf_test <- [Link](ts_data)
cat("ADF Test p-value:", adf_test$[Link], "\n")

# If p-value < 0.05, the series is stationary


if (adf_test$[Link] < 0.05) {
cat("The time series is stationary.\n")
} else {
cat("The time series is not stationary.\n")
}

# Differencing to make the time series stationary (if needed)


differenced_ts <- diff(ts_data)
plot(differenced_ts, main="Differenced Time Series", col="purple")

# ACF and PACF plots for checking seasonality and stationarity


acf(differenced_ts, main="ACF of Differenced Time Series", [Link]=12)
pacf(differenced_ts, main="PACF of Differenced Time Series", [Link]=12)

# Fit an ARIMA model to the time series


arima_model <- [Link](ts_data)
summary(arima_model)

# Forecast for the next 12 months


forecast_values <- forecast(arima_model, h=12)
plot(forecast_values)

# Print forecasted values


cat("Forecast for the next 12 months:\n")
print(forecast_values)
} else {
cat("No valid data available for creating the time series.\n")
}

#TO PLOT INDIVIDUALLY

# Decompose the time series (Trend, Seasonal, and Random)


decomp <- decompose(ts_data)

# Plot the Trend Component


plot(decomp$trend, main="Trend Component", col="blue", lwd=2)

# Plot the Seasonal Component


plot(decomp$seasonal, main="Seasonal Component", col="green", lwd=2)

# Plot the Residuals (Random Component)


plot(decomp$random, main="Residuals (Random)", col="red", lwd=2)
#Implement ARIMA model in R to find values of (p,d,q) to predict 20 future values.(Monthly_Value
Dataset)

# Load necessary libraries


library(forecast)
library(tseries)

# Step 1: Load the data


df <- [Link]("D:\\FAMT\\CSE 6SEM\\DAV\\Month_Value_1.csv")

# Step 2: Display the first few rows


head(df)

# Step 3: Check for missing values


sum([Link](df$Revenue))

# Step 4: Remove missing values


df <- [Link](df)

# Step 5: Convert Period to Date format


df$Period <- [Link](paste(df$Period, "01", sep = "-"), format = "%Y-%m-%d")

# Step 6: Create time series object (monthly data starting from Jan 2015)
ts_data <- ts(df$Revenue, start = c(2015, 1), frequency = 12)

# Step 7: Plot original time series


plot(ts_data, main = "Monthly Revenue Data", ylab = "Revenue", xlab = "Time", col = "blue")

# Step 8: Stationarity check (ADF Test)


adf_test <- [Link](ts_data)
print(adf_test)

# Step 9: Apply differencing if needed


if (adf_test$[Link] > 0.05) {
ts_data_diff <- diff(ts_data)
plot(ts_data_diff, main = "Differenced Revenue Data", col = "green")
} else {
ts_data_diff <- ts_data
print("Data is already stationary. No differencing applied.")
}

# Step 10: Fit ARIMA model


model <- [Link](ts_data_diff)
summary(model)

# Step 11: Forecast next 12 months


forecast_data <- forecast(model, h = 12)

# Step 12: Plot the forecast


plot(forecast_data, main = "Forecasted Revenue", ylab = "Revenue", xlab = "Time", col = "darkgreen")

You might also like