100 Days Of ML Code:Day 7/11-KNN
2018-08-08 16:46
316 查看
100天机器学习挑战汇总文章链接在这儿。
目录
Step1:数据预处理
因为用的是同一个数据集,这一步与Day6逻辑回归做的完全一致。
[code]import pandas as pd import numpy as np df = pd.read_csv('Social_Network_Ads.csv') # print(df) X = df.iloc[:, 2:4].values Y = df.iloc[:, 4].values # print(X) # print(Y) from sklearn.cross_validation import train_test_split X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0) # print(X_train) # feature scaling from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.fit_transform(X_test) # print(X_train) # print(X_test)
Step2:将KNN应用于训练集
KNeighborsClassifier的指导页面在这儿。
[code]from sklearn.neighbors import KNeighborsClassifier k =5 # k is the number of nearest neighbor neigh = KNeighborsClassifier(n_neighbors=k) neigh.fit(X_train, Y_train)
标答中对KNN分类器的设定是:
[code]classifier = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
这两个parameters的含义是:
p : integer, optional (default = 2)
Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.
metric : string or callable, default ‘minkowski’
the distance metric to use for the tree. The default metric is minkowski, and with p=2 is equivalent to the standard Euclidean metric. See the documentation of the DistanceMetric class for a list of available metrics.
Step3:预测
[code]Y_pred = neigh.predict(X_test) # print(Y_pred) from sklearn.metrics import confusion_matrix cm = confusion_matrix(Y_test, Y_pred) # print(cm)
阅读更多
相关文章推荐
- 100 Days Of ML Code:Day9/10/12/14 - SVM(支持向量机)
- CodeForces April Fools Day Contest 2018 B-A Map of the Cat
- 6/11/2009 Words of the Day
- 1 of 100 day
- 记2015年11月14日全球编程静修日(Global Day of Coderetreat)
- Code snippet of the day : persist the scroll position of an ASP.NET page
- Performance of Every Day Things by Jeffrey Richter PPT and Code
- 30 Days of .NET [Windows Mobile Applications] - Day 01: Minutes to Midnight Countdown(午夜倒数器)
- The power of variable names -- Code complete reading notes(11)
- Code snippet of the day : Exporting DataGrid Data to Excel...
- 深圳APAC Day of CodeRetreat笔记
- 2 of 100 days
- enbdding Fonts for wp from:http://www.jeffblankenburg.com/2010/10/24/31-days-of-windows-phone-day-24-embedding-fonts/
- 30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(里程耗油计算程序)
- ML基石_11_HazardOfOverfitting
- QClub 太原,2014年11月15日 Global Day of Coderetreat
- Code snippet of the day : Get Chinese Spell in C#
- 15 Days of jQuery(Day 15) --- 拖拽效果和选择器
- Educational Codeforces Round 11 D. Number of Parallelograms
- Writing a full JSON Serializer in 100 lines of C# code