0% found this document useful (0 votes)
6 views7 pages

Scikit-learn Linear Regression Examples

ML

Uploaded by

Raminder Chopra
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)
6 views7 pages

Scikit-learn Linear Regression Examples

ML

Uploaded by

Raminder Chopra
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

ML - scikit-learn

🧮 1. Predict Student Marks (Linear Regression)


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4], [5]]) # Study hours


y = [Link]([40, 50, 60, 70, 80]) # Marks

model = LinearRegression()
[Link](X, y)

hours = int(input("Enter study hours: "))


pred = [Link]([[hours]])
print(f"Predicted marks: {pred[0]:.2f}")

💰 2. Predict Employee Salary (Linear Regression)


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4], [5]]) # Years of Experience


y = [Link]([30000, 35000, 40000, 45000, 50000])

model = LinearRegression()
[Link](X, y)

yrs = int(input("Enter years of experience: "))


print("Predicted Salary:", [Link]([[yrs]])[0])

🏠 3. Predict House Rent


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4]]) # Rooms


y = [Link]([5000, 8000, 10000, 13000])
model = LinearRegression()
[Link](X, y)

r = int(input("Enter number of rooms: "))


print("Predicted Rent:", [Link]([[r]])[0])

🌡️ 4. Predict Temperature by Month


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4], [5], [6]])


y = [Link]([15, 18, 22, 28, 33, 35])

model = LinearRegression()
[Link](X, y)

m = int(input("Enter month number (1–12): "))


print(f"Predicted temperature: {[Link]([[m]])[0]:.1f}°C")

🚗 5. Predict Car Price by Age


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [3], [5], [7]])


y = [Link]([800000, 600000, 400000, 200000])

model = LinearRegression()
[Link](X, y)

age = int(input("Enter car age in years: "))


print("Predicted Price:", [Link]([[age]])[0])

🧠 6. Predict Diabetes (Logistic Regression)


from sklearn.linear_model import LogisticRegression
import numpy as np

X = [Link]([[80], [100], [120], [140], [160]]) # Sugar Level


y = [Link]([0, 0, 0, 1, 1]) # 1 = Diabetic
model = LogisticRegression()
[Link](X, y)

sugar = int(input("Enter sugar level: "))


print("Diabetic" if [Link]([[sugar]])[0] else "Normal")

📚 7. Predict Exam Pass/Fail


from sklearn.linear_model import LogisticRegression
import numpy as np

X = [Link]([[1], [2], [3], [4], [5]])


y = [Link]([0, 0, 0, 1, 1])

model = LogisticRegression()
[Link](X, y)

hrs = int(input("Enter study hours: "))


print("Pass" if [Link]([[hrs]])[0] else "Fail")

🧍‍♂️ 8. Predict Gender by Height (KNN)


from [Link] import KNeighborsClassifier
import numpy as np

X = [Link]([[160], [165], [170], [175], [180], [185]])


y = ['F', 'F', 'M', 'M', 'M', 'M']

model = KNeighborsClassifier(n_neighbors=3)
[Link](X, y)

h = int(input("Enter height in cm: "))


print("Gender:", [Link]([[h]])[0])

🍎 9. Predict Fruit Type by Weight (KNN)


from [Link] import KNeighborsClassifier
import numpy as np

X = [Link]([[100], [150], [200], [250]]) # weight in grams


y = ['Apple', 'Apple', 'Orange', 'Orange']

model = KNeighborsClassifier(n_neighbors=1)
[Link](X, y)

w = int(input("Enter fruit weight: "))


print("Predicted Fruit:", [Link]([[w]])[0])

🌻 10. Predict Flower Type (Iris Dataset)


from [Link] import load_iris
from [Link] import KNeighborsClassifier

iris = load_iris()
model = KNeighborsClassifier(n_neighbors=3)
[Link]([Link], [Link])

data = list(map(float, input("Enter 4 features (sepals, petals):


").split()))
pred = [Link]([data])
print("Predicted Flower:", iris.target_names[pred][0])

⚡ 11. Spam Message Detection


from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB

texts = ["buy cheap now", "earn money fast", "meeting at 5", "project
deadline"]
y = [1, 1, 0, 0] # 1 = spam

cv = CountVectorizer()
X = cv.fit_transform(texts)

model = MultinomialNB()
[Link](X, y)

msg = input("Enter a message: ")


print("Spam" if [Link]([Link]([msg]))[0] else "Not
Spam")
🧾 12. Predict Movie Rating (Linear Regression)
from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4], [5]]) # Viewer satisfaction


y = [Link]([2.0, 3.0, 3.5, 4.0, 5.0])

model = LinearRegression()
[Link](X, y)

rating = int(input("Enter viewer rating: "))


print("Predicted IMDb Rating:", [Link]([[rating]])[0])

🎮 13. Predict Game Score


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4]])


y = [Link]([100, 200, 300, 400])

model = LinearRegression()
[Link](X, y)

level = int(input("Enter game level: "))


print("Predicted score:", [Link]([[level]])[0])

🧃 14. Predict Calories Burned (Linear Regression)


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[10], [20], [30], [40]])


y = [Link]([100, 200, 300, 400])

model = LinearRegression()
[Link](X, y)

mins = int(input("Enter minutes exercised: "))


print("Calories burned:", [Link]([[mins]])[0])
🧢 15. Predict Product Sales
from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[100], [200], [300], [400]])


y = [Link]([10, 25, 35, 50]) # sales count

model = LinearRegression()
[Link](X, y)

ads = int(input("Enter ad spend: "))


print("Predicted sales:", [Link]([[ads]])[0])

📉 16. Predict Stock Price


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[1], [2], [3], [4], [5]])


y = [Link]([100, 105, 110, 120, 125])

model = LinearRegression()
[Link](X, y)

day = int(input("Enter day number: "))


print("Predicted stock price:", [Link]([[day]])[0])

🧾 17. Predict Credit Approval (Logistic Regression)


from sklearn.linear_model import LogisticRegression
import numpy as np

X = [Link]([[300], [400], [500], [600], [700]])


y = [Link]([0, 0, 1, 1, 1])

model = LogisticRegression()
[Link](X, y)

score = int(input("Enter credit score: "))


print("Approved" if [Link]([[score]])[0] else "Rejected")
🩺 18. Predict Blood Pressure Category
from sklearn.linear_model import LogisticRegression
import numpy as np

X = [Link]([[90], [110], [130], [150]])


y = [Link]([0, 0, 1, 1]) # 0=Normal, 1=High

model = LogisticRegression()
[Link](X, y)

bp = int(input("Enter systolic BP: "))


print("High BP" if [Link]([[bp]])[0] else "Normal")

🎓 19. Predict Student Grade (Regression)


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[60], [70], [80], [90]])


y = [Link]([6, 7, 8, 9]) # CGPA

model = LinearRegression()
[Link](X, y)

marks = int(input("Enter marks: "))


print("Predicted CGPA:", [Link]([[marks]])[0])

💻 20. Predict Electricity Bill


from sklearn.linear_model import LinearRegression
import numpy as np

X = [Link]([[100], [200], [300], [400]])


y = [Link]([500, 900, 1300, 1800])

model = LinearRegression()
[Link](X, y)

units = int(input("Enter units consumed: "))


print("Predicted bill: ₹", [Link]([[units]])[0])

You might also like